Jump to content

Module:Section sizes

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Trappist the monk (talk | contribs) at 14:18, 23 December 2018 (create;). 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)

--{{#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,
	}