Difference between revisions of "Module:Auto compact ToC"

From blackwiki
Jump to navigation Jump to search
blackwiki>Erutuon
(trying to make version of Template:Compact ToC that automatically determines which section headings exist)
 
blackwiki>Erutuon
(return package)
Line 1: Line 1:
--[[
+
local p = {}
<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;">       
 
<ul><li><a href="#A">A</a></li>
 
<li><a href="#B">B</a></li>
 
<li><a href="#C">C</a></li>
 
<li><a href="#D">D</a></li>
 
<li><a href="#E">E</a></li>
 
<li><a href="#F">F</a></li>
 
<li><a href="#G">G</a></li>
 
<li><a href="#H">H</a></li>
 
<li><a href="#I">I</a></li>
 
<li><a href="#J">J</a></li>
 
<li><a href="#L">L</a></li>
 
<li><a href="#N">N</a></li>
 
<li><a href="#O">O</a></li>
 
<li><a href="#R">R</a></li>
 
<li><a href="#S">S</a></li>
 
<li><a href="#T">T</a></li>
 
<li><a href="#V">V</a></li>
 
<li><a href="#W">W</a> </li></ul>
 
<p class="mw-empty-elt">     
 
</p>
 
</div></div>
 
--]]
 
  
 
local start = [[
 
local start = [[
Line 34: Line 9:
 
local close = [[</div></div>]]
 
local close = [[</div></div>]]
  
local function make_TOC()
+
function p.make_TOC()
 
local content = mw.title.getCurrentTitle():getContent()
 
local content = mw.title.getCurrentTitle():getContent()
 
 
Line 46: Line 21:
 
return start .. letters:concat("\n") .. close
 
return start .. letters:concat("\n") .. close
 
end
 
end
 +
 +
return p

Revision as of 23:14, 12 August 2018

Implements {{auto compact ToC}}


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