Module:NUMBEROFSECTIONS and Module:NUMBEROFSECTIONS/sandbox: Difference between pages
Appearance
(Difference between pages)
Content deleted Content added
Fred Gandt (talk | contribs) slight improvement for handling unexpected-and-wanted whitespace around the # delimiters; %s? --> %s* i.e. possible single whitespace char --> zero or more whitespace chars |
m 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. |
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 |
local pages = mw.text.split(frame.args[1], '%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['level'], '%s*') |
local levels = mw.text.split(frame.args['level'], '%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( |
total = total + count(haystack, needles) |
||
end |
end |
||
end |
end |