![]() | This module is rated as ready for general use. It has reached a mature form and is thought to be relatively bug-free and ready for use wherever appropriate. It is ready to mention on help pages and other Wikipedia resources as an option for new users to learn. To reduce server load and bad output, it should be improved by sandbox testing rather than repeated trial-and-error editing. |
![]() | This module is subject to page protection. It is a highly visible module in use by a very large number of pages, or is substituted very frequently. Because vandalism or mistakes would affect many pages, and even trivial editing might cause substantial load on the servers, it is protected from editing. |
Usage
![]() | This module is invoked by {{NUMBEROFSECTIONS}} |
sections
{{ #invoke:NUMBEROFSECTIONS|main| page name [ # page name [ # ... ] ] | level = section level number(s) }}
- Multiple page names (at least one required) are the
#
delimited names of any Wikipedia pages (including namespaces). - section level(s) (required) is any group of numerals between 1 and 6 (inclusive) e.g.
435
or5 3 4
equates to:
- sections with a level
3
( "===" ),4
( "====" ) or5
( "=====" ) heading.
- sections with a level
- Multiple page names (at least one required) are the
{{#invoke:NUMBEROFSECTIONS|main|Wikipedia:Village pump (technical)|level=2}}
producesScript error: The function "main" does not exist.
{{#invoke:NUMBEROFSECTIONS|main|Wikipedia:Village pump (technical)#Wikipedia:Village pump (proposals)|level=2}}
producesScript error: The function "main" does not exist.
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