Latest revision as of 21:15, 4 February 2023 view sourceFred Gandt (talk | contribs)Extended confirmed users, New page reviewers, Template editors15,118 edits slight improvement for handling unexpected-and-wanted whitespace around the # delimiters; %s? --> %s* i.e. possible single whitespace char --> zero or more whitespace chars |
Latest revision as of 03:35, 6 May 2020 edit Johnuniq (talk | contribs)Autopatrolled, Administrators86,673 editsm Johnuniq moved page Module:NUMBEROF/sandbox to Module:NUMBEROFSECTIONS/sandbox without leaving a redirect: implement Template talk:NUMBEROF#Requested move 3 May 2020 a little early to allow final development of the separate NUMBEROF and NUMBEROFSECTIONS functions |
Line 1: |
Line 1: |
|
local p = {} |
|
local p = {} |
|
|
|
|
-- Unescape functionality grabbed from https://stackoverflow.com/a/14899740/1832568 |
|
|
local function unescape(str) |
|
|
str = string.gsub(str, '&#(%d+);', string.char) |
|
|
str = string.gsub(str, '&#x(%d+);', function(n) return string.char(tonumber(n, 16)) end) |
|
|
return str |
|
|
end |
|
|
|
|
|
|
-- Counting function accepting a string haystack and table of needles |
|
-- Counting function accepting a string haystack and table of needles |
Line 22: |
Line 15: |
|
|
|
|
|
-- Function takes any number of # delimited page names and section level numbers |
|
-- Function takes any number of # delimited page names and section level numbers |
|
function p.main(frame) |
|
function p.sections(frame) |
|
local total = 0 |
|
local total = 0 |
|
local needles = {} |
|
local needles = {} |
|
local haystack = '' |
|
local haystack = '' |
|
-- Separate page names from # delimited string into table |
|
-- Separate page names from # delimited string into table |
|
local pages = mw.text.split(unescape(frame.args), '%s*#%s*') |
|
local pages = mw.text.split(frame.args, '%s?#%s?') |
|
-- Separate whitespace delimited section level numbers into table |
|
-- Separate whitespace delimited section level numbers into table |
|
local levels = mw.text.split(frame.args, '%s*') |
|
local levels = mw.text.split(frame.args, '%s*') |
Line 43: |
Line 36: |
|
--[[ pass the raw markup and needles to count |
|
--[[ pass the raw markup and needles to count |
|
and add the return to total ]] |
|
and add the return to total ]] |
|
total = total + count('\n' .. haystack, needles) |
|
total = total + count(haystack, needles) |
|
end |
|
end |
|
end |
|
end |