Module:NUMBEROFSECTIONS: Difference between revisions

Content deleted Content added
check for nil vals
improved code in general BUT changed delimitation method for page args
Line 4:
local function count(haystack, needles)
local number = 0
local index = 1
local needle = needles[index]
-- While we have needles to look for
whilefor index, needle in ipairs(needles) do
-- find them all in our haystack
for m in string.gmatch(haystack, needle) do
number = number + 1
end
index = index + 1
needle = needles[index]
end
return number
end
 
-- Function acceptingtakes any number of # delimited page names and section level numbers
function p.sections(frame)
local index = 1
local total = 0
local page = ""
local pages = {}
local needles = {}
local haystack = ""
-- Separate page names from # delimited string into table
-- Iterate through the args to find the page names and section levels
forlocal key,pages value= in pairsmw.text.split(frame.args)[1], do'%s?#%s?')
-- Separate whitespace delimited section level numbers into table
-- If the param specifies the section levels required
if local keylevels == mw.text.split(frame.args['level'], then'%s*')
-- Iterate through levels
-- for every section level number
for level in mw.text.gsplit(valuetable.concat(levels), ""'') do
-- and add the level needle to our table of needles
if level ~= " " then
needles[#needles + 1] = '\n'..string.rep("'="', tonumber(level)) .. "'[^=]"'
-- add the needle to our table of needles
needles[#needles + 1] = "\n" ..
string.rep("=", tonumber(level)) .. "[^=]"
end
end
-- Otherwise, add the page name to our table
else
pages[#pages + 1] = value
end
end
-- For each page name in our tablepages
for index, page =in ipairs(pages[index]) do
while page do
-- get the raw markup
haystack = mw.title.new(page)
Line 52 ⟶ 36:
total = total + count(haystack:getContent(), needles)
end
index = index + 1
page = pages[index]
end
--[[ then return how many sections of the required level