<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://mywikibiz.com/index.php?action=history&amp;feed=atom&amp;title=Module%3ATransclude_TOC</id>
	<title>Module:Transclude TOC - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://mywikibiz.com/index.php?action=history&amp;feed=atom&amp;title=Module%3ATransclude_TOC"/>
	<link rel="alternate" type="text/html" href="https://mywikibiz.com/index.php?title=Module:Transclude_TOC&amp;action=history"/>
	<updated>2026-06-19T03:34:34Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.35.3</generator>
	<entry>
		<id>https://mywikibiz.com/index.php?title=Module:Transclude_TOC&amp;diff=479804&amp;oldid=prev</id>
		<title>Zoran: Pywikibot 6.4.0</title>
		<link rel="alternate" type="text/html" href="https://mywikibiz.com/index.php?title=Module:Transclude_TOC&amp;diff=479804&amp;oldid=prev"/>
		<updated>2021-07-16T07:41:05Z</updated>

		<summary type="html">&lt;p&gt;Pywikibot 6.4.0&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;require('Module:No globals');&lt;br /&gt;
&lt;br /&gt;
--[=[-------------------------&amp;lt; R E M O V E _ W I K I _ L I N K &amp;gt;----------------------------------------------&lt;br /&gt;
&lt;br /&gt;
Gets the display text from a wikilink like [[A|B]] or [[B]] gives B&lt;br /&gt;
&lt;br /&gt;
The str:gsub() returns either A|B froma [[A|B]] or B from [[B]] or B from B (no wikilink markup).&lt;br /&gt;
&lt;br /&gt;
In l(), l:gsub() removes the link and pipe (if they exist); the second :gsub() trims white space from the label&lt;br /&gt;
if str was wrapped in wikilink markup.  Presumably, this is because without wikimarkup in str, there is no match&lt;br /&gt;
in the initial gsub, the replacement function l() doesn't get called.&lt;br /&gt;
&lt;br /&gt;
]=]&lt;br /&gt;
&lt;br /&gt;
local function remove_wiki_link (str)&lt;br /&gt;
	return (str:gsub( &amp;quot;%[%[:?([^%[%]]*)%]%]&amp;quot;, function(l)&lt;br /&gt;
		return l:gsub( &amp;quot;^[^|]*|(.*)$&amp;quot;, &amp;quot;%1&amp;quot; ):gsub(&amp;quot;^%s*(.-)%s*$&amp;quot;, &amp;quot;%1&amp;quot;);&lt;br /&gt;
	end));&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
--[=[-------------------------&amp;lt; M A K E _ W I K I L I N K &amp;gt;----------------------------------------------------&lt;br /&gt;
&lt;br /&gt;
Makes a wikilink; when both link and display text is provided, returns a wikilink in the form [[L|D]]; if only&lt;br /&gt;
link is provided, returns a wikilink in the form [[L]]; if neither are provided or link is omitted, returns an&lt;br /&gt;
empty string.&lt;br /&gt;
&lt;br /&gt;
]=]&lt;br /&gt;
&lt;br /&gt;
local function make_wikilink (link, display)&lt;br /&gt;
	if link and ('' ~= link) then&lt;br /&gt;
		if display and ('' ~= display) then&lt;br /&gt;
			return table.concat ({'[[', link, '|', display, ']]'});&lt;br /&gt;
		else&lt;br /&gt;
			return table.concat ({'[[', link, ']]'});&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
	return display or '';													-- link not set so return the display text&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
--[[--------------------------&amp;lt; T O C &amp;gt;----------------------------------------------------------------------&lt;br /&gt;
&lt;br /&gt;
module entry point&lt;br /&gt;
&lt;br /&gt;
create a wikilinked list of &amp;lt;page name&amp;gt;'s sections&lt;br /&gt;
&lt;br /&gt;
{{#invoke:Sandbox/DannyS712/TOC|TOC|&amp;lt;article name&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
]]&lt;br /&gt;
&lt;br /&gt;
local function TOC (frame)&lt;br /&gt;
	local A = {};																-- table to hold section names and sizes&lt;br /&gt;
	local section_name_list = {}												-- an interim list that holds just the section names&lt;br /&gt;
	local section_content;														-- section content used for counting&lt;br /&gt;
	local section = '_LEAD_';													-- lead section doen't have a heading&lt;br /&gt;
	local count;																-- number of bytes in a section including the header text&lt;br /&gt;
	local _;																	-- dummy for using gsub to count bytes&lt;br /&gt;
	local lang = mw.language.getContentLanguage();								-- language object for number formatting appropriate to local language&lt;br /&gt;
	local s;																	-- start position of found heading (returned from string.find())&lt;br /&gt;
	local e = 1;																-- end position of found heading (returned from string.find())&lt;br /&gt;
	local section_name;															-- captured heading name (returned from string.find())&lt;br /&gt;
	local level;																-- number of leading '=' in heading markup; used for indenting subsections in the rendered list&lt;br /&gt;
	local wl_name;																-- anchor and display portion for wikilinks in rendered list&lt;br /&gt;
&lt;br /&gt;
	local title = mw.title.new (frame.args[1]);									-- page title&lt;br /&gt;
	local content = title:getContent();											-- get unparsed wikitext from the article&lt;br /&gt;
	if not content then&lt;br /&gt;
		return '&amp;lt;span style=&amp;quot;font-size:100%;&amp;quot; class=&amp;quot;error&amp;quot;&amp;gt;error: no article:' .. frame.args[1] .. '&amp;lt;/span&amp;gt;';&lt;br /&gt;
	end&lt;br /&gt;
&lt;br /&gt;
	if title.isRedirect then													-- redirects don't have sections&lt;br /&gt;
		return '&amp;lt;span style=&amp;quot;font-size:100%;&amp;quot; class=&amp;quot;error&amp;quot;&amp;gt;error: ' .. frame.args[1] .. ' is a redirect&amp;lt;/span&amp;gt;';&lt;br /&gt;
	end&lt;br /&gt;
&lt;br /&gt;
	section_content = content:match ('(.-)===*');								-- get the lead section&lt;br /&gt;
	if section_content then&lt;br /&gt;
		_, count = section_content:gsub ('.', '%1');							-- count the size of the lead section&lt;br /&gt;
	else&lt;br /&gt;
		return '&amp;lt;span style=&amp;quot;font-size:100%;&amp;quot; class=&amp;quot;error&amp;quot;&amp;gt;error: no sections found in: ' .. frame.args[1] .. '&amp;lt;/span&amp;gt;';&lt;br /&gt;
	end&lt;br /&gt;
	&lt;br /&gt;
	table.insert (A, make_wikilink (frame.args[1], section));&lt;br /&gt;
&lt;br /&gt;
	while (1) do																-- done this way because some articles reuse section names&lt;br /&gt;
		s, e, section_name = string.find (content, '\n==+ *(.-) *==+', e);		-- get start, end, and section name beginning a end of last find; newline must precede '==' heading markup&lt;br /&gt;
		if s then&lt;br /&gt;
			table.insert (section_name_list, {section_name, s});				-- save section name and start location of this find&lt;br /&gt;
		else&lt;br /&gt;
			break;&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
	&lt;br /&gt;
	for i, section_name in ipairs (section_name_list) do&lt;br /&gt;
		local escaped_section_name = string.gsub (section_name[1], '([%(%)%.%%%+%-%*%?%[%^%$%]])', '%%%1');	-- escape lua patterns in section name&lt;br /&gt;
		local pattern = '(==+ *' .. escaped_section_name .. ' *==+.-)==+';		-- make a pattern to get the content of a section&lt;br /&gt;
		section_content = string.match (content, pattern, section_name[2]);		-- get the content beginning at the string.find() start location&lt;br /&gt;
		if section_content then&lt;br /&gt;
			_, count = section_content:gsub ('.', '%1');						-- count the bytes in the section&lt;br /&gt;
		else																	-- probably the last section (no proper header follows this section name)&lt;br /&gt;
			pattern = '(==+ *' .. escaped_section_name .. ' *==+.+)';			-- make a new pattern&lt;br /&gt;
			section_content = string.match (content, pattern, section_name[2]);	-- try to get content&lt;br /&gt;
			if section_content then&lt;br /&gt;
				_, count = section_content:gsub ('.', '%1');					-- count the bytes in the section&lt;br /&gt;
			else&lt;br /&gt;
				count = '—';													-- no content so show that&lt;br /&gt;
			end&lt;br /&gt;
		end&lt;br /&gt;
&lt;br /&gt;
		_, level = section_content:find ('^=+');								-- should always be the first n characters of section content&lt;br /&gt;
		level = (2 &amp;lt; level) and ((level-2) * 1.6) or nil;						-- remove offset and mult by 1.6em (same indent as ':' markup which doesn't work in a table)&lt;br /&gt;
&lt;br /&gt;
		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&lt;br /&gt;
		wl_name = wl_name:gsub ('[%[%]]', {['[']='&amp;amp;#91;', [']']='&amp;amp;#93;'});		-- replace '[' and ']' characters with html entities so that wikilinked section names work&lt;br /&gt;
		wl_name = mw.text.trim (wl_name);										-- trim leading/trailing white space if any because white space buggers up url anchor links&lt;br /&gt;
		&lt;br /&gt;
		table.insert (A, table.concat ({										-- build most of a table row here because here we have heading information that we won't have later&lt;br /&gt;
			level and '&amp;lt;span style=&amp;quot;margin-left:' .. level .. 'em&amp;quot;&amp;gt;' or '';		-- indent per heading level (number of '=' in heading markup)&lt;br /&gt;
			make_wikilink (frame.args[1] .. '#' .. wl_name, wl_name),			-- section link&lt;br /&gt;
			level and '&amp;lt;/span&amp;gt;' or ''}));										-- close the span if opened&lt;br /&gt;
	end&lt;br /&gt;
&lt;br /&gt;
	local out = {};																-- make a sortable wikitable for output&lt;br /&gt;
	table.insert (out, string.format ('{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;%s&amp;quot;\n|+Full table of contents for [[%s]] (%d sections)', frame.args.style or '', frame.args[1], #A));	-- output table header&lt;br /&gt;
	table.insert (out, '\n!Sections\n|-\n|');									-- column headers, and first row pipe&lt;br /&gt;
	table.insert (out, table.concat (A, '\n|-\n|'));							-- section rows with leading pipes (except first row already done)&lt;br /&gt;
	table.insert (out, '\n|}');													-- close the wikitable&lt;br /&gt;
	return table.concat (out, '');&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
--[[--------------------------&amp;lt; E X P O R T E D   F U N C T I O N S &amp;gt;------------------------------------------&lt;br /&gt;
]]&lt;br /&gt;
&lt;br /&gt;
return&lt;br /&gt;
	{&lt;br /&gt;
	TOC = TOC,&lt;br /&gt;
	}&lt;/div&gt;</summary>
		<author><name>Zoran</name></author>
	</entry>
</feed>