Jump to content

Module:Auto compact TOC

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Erutuon (talk | contribs) at 23:14, 12 August 2018 (return package). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

local p = {}

local start = [[
<div role="navigation" id="toc" class="toc plainlinks hlist" aria-labelledby="tocheading" style="text-align:left;">
<div id="toctitle" class="toctitle" style="text-align:center;"><span id="tocheading" style="font-weight:bold;">Contents</span></div>
<div style="margin:auto;white-space:nowrap;">
]]

local close = [[</div></div>]]

function p.make_TOC()
	local content = mw.title.getCurrentTitle():getContent()
	
	local letters = setmetatable({}, table)
	-- Find uppermost headers containing a single ASCII letter.
	for letter in content:gmatch "%f[^\n]==%s*(%a)%s*==%f[^==]" do
		letter = letter:upper()
		letters:insert(("* [[#%s|%s]]"):format(letter, letter))
	end
	
	return start .. letters:concat("\n") .. close
end

return p