Jump to content

Module:NUMBEROFSECTIONS

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Fred Gandt (talk | contribs) at 10:05, 24 March 2016 (to be expanded - pushing to module space early due to strange sandbox results). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

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