Module:NUMBEROFSECTIONS

From blackwiki
Revision as of 10:05, 24 March 2016 by blackwiki>Fred Gandt (to be expanded - pushing to module space early due to strange sandbox results)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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 = {}

local function count(raw, seclevs)
	local nos = 0
	local index = 1
	local lev = seclevs[index]
	while lev do
		for m in string.gmatch(raw, "\n" .. lev .. "[^=]") do
			nos = nos + 1
		end
		index = index + 1
		lev = seclevs[index]
	end
	return nos
end

function p.sections(frame)
	local levels = {}
	local level = frame.args[2]
	local title = mw.title.new(frame.args[1])
	for value in mw.text.gsplit(level, "") do
		if value ~= " " then
			levels[#levels + 1] = string.rep("=", tonumber(value))
		end
	end
	return count(title:getContent(), levels)
end

return p