Module:Section sizes
This module creates a wikitable that lists each section in a page along with that section's size in bytes. Each section is wikilinked to its target in the page; subsections are indented. Cells for section sizes are shaded according to the size; the smallest section's background is white, and the largest section's background is red.
Usage
This module has two entry points: size
, and section_size_get
.
size
Use entry point size
to emit the section wikitable:
{{#invoke:Section sizes|size|<page name>|style=<style string>}}
Entry point size()
takes two arguments from frame
, one positional and one named:
<page name>
(required) – the first positional parameter is the page name; may include namespace|style=
(optional) – css string suitable for use in the wikitable'sstyle=
attribute; for example to render the table at the right side of the page:{{#invoke:Section sizes|size|<page name>|style=float:right; margin-left:.5em}}
Example
{{#invoke:Section sizes|size|Klingon language}}
![]() |
|
section_size_get
section_size_get
return an article or section size statistic:
{{#invoke:Section sizes|section_size_get|<page name>|[<section name|token>]|_all|_pct=yes |_nosep=yes}}
section_size_get
takes one to three positional parameters and two named parameters from frame
:
<page name>
(required) – the first positional parameter is the page name; may include namespace<section name|token>
(optional) – the second positional parameter is either the section name, or one of three tokens (if param 2 is absent, the default is the article's lead section)<section name>
– returns the size of the named section_lead
– returns the size of the lead section; default when second positional parameter is empty or omitted_max
– returns the size of the longest individual section_total
– returns the size of the article (should equal PAGESIZE)
_all
(optional) – the third positional parameter is the scope of the size measure; requires<section name>
; not supported for keywords; returns size of the named section plus the sizes of its subsections
Named parameters
|_nosep=
(optional) – no separator; accepts one value:yes
; section size emitted without thousands separators; ignored when|_pct=
is set (default: with separator)|_pct=
(optional) – percent; accepts one value:yes
; emits size of<section name>
,_lead
,_max
, or_total
as a percentage of_total
rounded to 2 decimals with a '%' symbol
Examples
These examples refer to the same article as shown in the size
example.
Sizes of a named section with and without its subsections (_all
):
{{#invoke:Section sizes|section_size_get| Klingon language | Phonology }}
→ Lua error in package.lua at line 80: module 'Module:No globals' not found.{{#invoke:Section sizes|section_size_get| Klingon language | Phonology | _all }}
→ Lua error in package.lua at line 80: module 'Module:No globals' not found.
Keywords:
{{#invoke:Section sizes|section_size_get| Klingon language | _lead }}
→ Lua error in package.lua at line 80: module 'Module:No globals' not found.{{#invoke:Section sizes|section_size_get| Klingon language | _max }}
→ Lua error in package.lua at line 80: module 'Module:No globals' not found.{{#invoke:Section sizes|section_size_get| Klingon language | _total }}
→ Lua error in package.lua at line 80: module 'Module:No globals' not found.
Section sizes without thousands separator:
{{#invoke:Section sizes|section_size_get| Klingon language | Phonology | _nosep=yes }}
→ Lua error in package.lua at line 80: module 'Module:No globals' not found.{{#invoke:Section sizes|section_size_get| Klingon language | _total | _nosep=yes }}
→ Lua error in package.lua at line 80: module 'Module:No globals' not found.
Section sizes as a percentage of _total
:
{{#invoke:Section sizes|section_size_get| Klingon language | Phonology | _pct=yes }}
→ Lua error in package.lua at line 80: module 'Module:No globals' not found.{{#invoke:Section sizes|section_size_get| Klingon language | _lead | _pct=yes }}
→ Lua error in package.lua at line 80: module 'Module:No globals' not found.{{#invoke:Section sizes|section_size_get| Klingon language | _max | _pct=yes }}
→ Lua error in package.lua at line 80: module 'Module:No globals' not found.{{#invoke:Section sizes|section_size_get| Klingon language | _total | _pct=yes }}
→ Lua error in package.lua at line 80: module 'Module:No globals' not found.
See also
- Template:Section sizes, a pre-styled template for talk pages which invokes this module
- Template:Section length, a simple template which invokes it to request the length of one named section, or related info
--{{#invoke:Sandbox/trappist the monk/section|size|<article name>}}
require('Module:No globals');
--[[--------------------------< S I Z E >----------------------------------------------------------------------
module entry point
create a list of sections and their size in bytes
returns the list of heading names and byte counts
]]
local function size (frame)
local A = {}; -- table to hold the parameters used in the first of two templates
local section_name_list = {} -- an interim list that holds just the section names
local section_content; -- section content used for counting
local section = '_LEAD_'; -- lead section doen't have a heading
local count; -- number of bytes in a section including the header text
local total; -- sum of all byte counts
local _; -- dummy for using gsub to count bytes
local style = ''; -- might be used to style output table
local content = mw.title.new (frame.args[1]):getContent(); -- get unparsed wikitext from the article
if not content then
return '<span style="font-size:100%;" class="error">error: no article:' .. frame.args[1] .. '</span>';
end
section_content = content:match ('(.-)===*'); -- get the lead section
_, count = section_content:gsub ('.', '%1'); -- count the size of the lead section
total = count;
table.insert (A, section .. '||' .. count)
local s; -- start position of found heading
local e = 1; -- end position of found heading
local section_name; -- captured heading name
while (1) do -- done this way because some articles reuse section names
s, e, section_name = string.find (content, '==+ *(.-) *==+', e); -- get start, end, and section name beginning a end of last find
if s then
table.insert (section_name_list, {section_name, s}); -- save section name and start location of this find
else
break;
end
end
for i, section_name in ipairs (section_name_list) do
local escaped_section_name = string.gsub (section_name[1], '([%(%)%.%%%+%-%*%?%[%^%$%]])', '%%%1'); -- escape lua patterns in section name
local pattern = '(==+ *' .. escaped_section_name .. ' *==+.-)==+'; -- make a pattern to get the content of a section
section_content = string.match (content, pattern, section_name[2]); -- get the content beginning at the string.find start location
if section_content then
_, count = section_content:gsub ('.', '%1'); -- count the bytes in the section
total = total + count;
else -- probably the last section (no proper header follows this section name)
pattern = '(==+ *' .. escaped_section_name .. ' *==+.+)'; -- make a new pattern
section_content = string.match (content, pattern, section_name[2]); -- try to get content
if section_content then
_, count = section_content:gsub ('.', '%1'); -- count the bytes in the section
total = total + count;
else
count = '—'; -- no content so show that
end
end
table.insert (A, section_name[1] .. '||' .. count);
end
local out = {}; -- make a sortable table for output
table.insert (out, string.format ('{| class="wikitable sortable" style="%s"\n|+section size for [[%s]]', style, frame.args[1])); -- output table header
table.insert (out, '\n!section name!!byte count\n|-\n|'); -- column headers, and first row pipe
table.insert (out, table.concat (A, '\n|-\n|')); -- section rows with leading pipes (except first row already done)
table.insert (out, '\n|}'); -- close the table
return table.concat (out, '') .. '\ntotal: ' .. total; -- make a big string, append total byte count, and done
end
--[[--------------------------< E X P O R T E D F U N C T I O N S >------------------------------------------
]]
return
{
size = size,
}