Difference between revisions of "Module:NUMBEROFSECTIONS"

From blackwiki
Jump to navigation Jump to search
blackwiki>Fred Gandt
(to be expanded - pushing to module space early due to strange sandbox results)
 
blackwiki>Fred Gandt
(a few simple comments and preparedness for expansion)
Line 1: Line 1:
 
local p = {}
 
local p = {}
  
local function count(raw, seclevs)
+
-- Counting function accepting a string haystack and table of needles
local nos = 0
+
local function count(haystack, needles)
 +
local number = 0
 
local index = 1
 
local index = 1
local lev = seclevs[index]
+
local needle = needles[index]
while lev do
+
-- While we have needles to look for
for m in string.gmatch(raw, "\n" .. lev .. "[^=]") do
+
while needle do
nos = nos + 1
+
-- find them all in our haystack
 +
for m in string.gmatch(haystack, needle) do
 +
number = number + 1
 
end
 
end
 
index = index + 1
 
index = index + 1
lev = seclevs[index]
+
needle = needles[index]
 
end
 
end
return nos
+
return number
 
end
 
end
  
 +
-- Function accepting a page name and section level numbers
 
function p.sections(frame)
 
function p.sections(frame)
local levels = {}
+
local needles = {}
local level = frame.args[2]
+
local levels = frame.args[2]
 
local title = mw.title.new(frame.args[1])
 
local title = mw.title.new(frame.args[1])
for value in mw.text.gsplit(level, "") do
+
-- For every section level number
if value ~= " " then
+
for level in mw.text.gsplit(levels, "") do
levels[#levels + 1] = string.rep("=", tonumber(value))
+
if level ~= " " then
 +
-- add the needle to our table of needles
 +
needles[#needles + 1] = "\n" ..
 +
string.rep("=", tonumber(level)) .. "[^=]"
 
end
 
end
 
end
 
end
return count(title:getContent(), levels)
+
-- then return how many needles are in our haystack
 +
return count(title:getContent(), needles)
 
end
 
end
  
 
return p
 
return p

Revision as of 15:52, 24 March 2016

Usage

sections
{{ #invoke:NUMBEROFSECTIONS|main| page name [ # page name [ # ... ] ] | level = section level number(s) }}
  1. Multiple page names (at least one required) are the # delimited names of any Wikipedia pages (including namespaces).
  2. section level(s) (required) is any group of numerals between 1 and 6 (inclusive) e.g. 435 or 5 3 4 equates to:
sections with a level 3 ( "===" ), 4 ( "====" ) or 5 ( "=====" ) heading.
{{#invoke:NUMBEROFSECTIONS|main|Wikipedia:Village pump (technical)|level=2}} produces Template:NUMBEROFSECTIONS
{{#invoke:NUMBEROFSECTIONS|main|Wikipedia:Village pump (technical)#Wikipedia:Village pump (proposals)|level=2}} produces Template:NUMBEROFSECTIONS



local p = {}

-- Counting function accepting a string haystack and table of needles
local function count(haystack, needles)
	local number = 0
	local index = 1
	local needle = needles[index]
	-- While we have needles to look for
	while needle 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 accepting a page name and section level numbers
function p.sections(frame)
	local needles = {}
	local levels = frame.args[2]
	local title = mw.title.new(frame.args[1])
	-- For every section level number
	for level in mw.text.gsplit(levels, "") do
		if level ~= " " then
			-- add the needle to our table of needles
			needles[#needles + 1] = "\n" ..
				string.rep("=", tonumber(level)) .. "[^=]"
		end
	end
	-- then return how many needles are in our haystack
	return count(title:getContent(), needles)
end

return p