require('strict');--[=[-------------------------< R E M O V E _ W I K I _ L I N K >---------------------------------------------- Gets the display text from a wikilink like [[A|B]] or B gives B
The str:gsub returns either A|B froma B or B from B or B from B (no wikilink markup).
In l, l:gsub removes the link and pipe (if they exist); the second :gsub trims white space from the labelif str was wrapped in wikilink markup. Presumably, this is because without wikimarkup in str, there is no matchin the initial gsub, the replacement function l doesn't get called.
]=]
local function remove_wiki_link (str) return (str:gsub("%[%[([^%[%]]*)%]%]", function(l) return l:gsub("^[^|]*|(.*)$", "%1"):gsub("^%s*(.-)%s*$", "%1"); end));end
--[=[-------------------------< M A K E _ W I K I L I N K >---------------------------------------------------- Makes a wikilink; when both link and display text is provided, returns a wikilink in the form [[L|D]]; if onlylink is provided, returns a wikilink in the form L; if neither are provided or link is omitted, returns anempty string.
]=]
local function make_wikilink (link, display) if link and ( ~= link) then if display and ( ~= display) then return table.concat ; else return table.concat ; end end return display or ; -- link not set so return the display textend
--size|
local function size (frame) local A = ; -- table to hold section names and sizes 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 cumcount = ; local lastlevel; local maxlevels; local levelcounts = ; local upperlevel; local highlight; local highlightc; local total; -- sum of all byte counts local max; -- largest section so far encountered local cummax; -- largest section so far encountered (cumulative) local _; -- dummy for using gsub to count bytes local lang = mw.language.getContentLanguage; -- language object for number formatting appropriate to local language local s; -- start position of found heading (returned from string.find) local e = 1; -- end position of found heading (returned from string.find) local section_name; -- captured heading name (returned from string.find) local level = ; -- number of leading '=' in heading markup; used for indenting subsections in the rendered list local wl_name; -- anchor and display portion for wikilinks in rendered list
local content = mw.title.new (frame.args[1]):getContent; -- get unparsed wikitext from the article if not content then return '
error: no article:' .. frame.args[1] .. ''; endif content:find ('#REDIRECT') then -- redirects don't have sections return '
error: ' .. frame.args[1] .. ' is a redirect'; endsection_content = content:match ('(.-)
while (1) do -- done this way because some articles reuse section names s, e, section_name = string.find (content, '\n
+', e); -- get start, end, and section name beginning a end of last find; newline must precede '
+ *' .. 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[i] = section_content:gsub ('.', '%1'); -- count the bytes in the section total = total + count[i]; max = max < count[i] and count[i] or max; -- keep track of largest count else -- probably the last section (no proper header follows this section name) pattern = '(
+.+)'; -- make a new pattern section_content = string.match (content, pattern, section_name[2]); -- try to get content if section_content then _, count[i] = section_content:gsub ('.', '%1'); -- count the bytes in the section total = total + count[i]; max = max < count[i] and count[i] or max; -- keep track of largest count else count[i] = '—'; -- no content so show that end end
_, level[i] = section_content:find ('^=+'); -- should always be the first n characters of section content end cummax=0; lastlevel=0; maxlevels=7; for j=1,maxlevels do levelcounts[j]=0; end for i=#count,1,-1 do --cumcount[i]=level[i]; if level[i]
for i, section_name in ipairs (section_name_list) do
if count[i]
2 then highlightc=highlightc .. 'font-weight:bold;'; -- if main section, make it bold elseif cumcount[i]
cummax then highlightc=highlightc .. 'color:red;'; -- if the largest size, make it red end highlightc=highlightc .. '"|'; -- close the style declaration level[i] = (2 < level[i]) and ((level[i]-2) * 1.6) or nil; -- remove offset and mult by 1.6em (same indent as ':' markup which doesn't work in a table) wl_name = remove_wiki_link (section_name[1]):gsub ('%b', ); -- remove wikilinks and templates from section headings so that we can link to the section wl_name = wl_name:gsub ('[%[%]]',); -- replace '[' and ']' characters with html entities so that wikilinked section names work wl_name = mw.text.trim (wl_name); -- trim leading/trailing white space if any because white space buggers up url anchor links table.insert (A, table.concat); end
local out = ; -- make a sortable wikitable for output table.insert (out, string.format ('
-\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 | -\n!Total | ' .. lang:formatNum (total) .. ' | ' .. lang:formatNum (total)); -- total number of bytes counted as column headers so that sorting doesn't move this row from the bottom to top table.insert (out, '\n |
---|
----------------------------< E X P O R T E D F U N C T I O N S >------------------------------------------
return