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)
 
m (9 revisions imported)
 
(8 intermediate revisions by 2 users not shown)
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 = [[
<div role="navigation" id="toc" class="toc plainlinks hlist" aria-labelledby="tocheading" style="text-align:left;">
+
__NOTOC__<!--
 +
--><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 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;">
 
<div style="margin:auto;white-space:nowrap;">
Line 34: Line 10:
 
local close = [[</div></div>]]
 
local close = [[</div></div>]]
  
local function make_TOC()
+
local function make_TOC_item(anchor, link_text)
 +
link_text = link_text or anchor
 +
return ("* [[#%s|%s]]"):format(anchor, link_text)
 +
end
 +
 
 +
local Array_mt = { __index = table }
 +
local function Array()
 +
return setmetatable({}, Array_mt)
 +
end
 +
 
 +
function p.make_TOC(frame)
 
local content = mw.title.getCurrentTitle():getContent()
 
local content = mw.title.getCurrentTitle():getContent()
 
 
local letters = setmetatable({}, table)
+
if not content then
 +
error "The current page has no content"
 +
end
 +
 +
local letters = Array()
 
-- Find uppermost headers containing a single ASCII letter.
 
-- Find uppermost headers containing a single ASCII letter.
for letter in content:gmatch "%f[^\n]==%s*(%a)%s*==%f[^==]" do
+
for letter in content:gmatch "%f[^\n]==%s*(%a)%s*==%f[^=]" do
 
letter = letter:upper()
 
letter = letter:upper()
letters:insert(("* [[#%s|%s]]"):format(letter, letter))
+
letters:insert(make_TOC_item(letter))
 
end
 
end
 
 
return start .. letters:concat("\n") .. close
+
local yesno = require "Module:Yesno"
 +
local rest = Array()
 +
local other_headers = require "Module:TableTools".listToSet{
 +
"See also", "References", "Notes", "Further reading", "External links",
 +
}
 +
for header in content:gmatch "%f[^\n]==%s*(..-)%s*==%f[^=]" do
 +
if other_headers[header] then
 +
rest:insert(make_TOC_item(header))
 +
end
 +
end
 +
 +
return start .. letters:concat("\n") .. "\n\n" .. rest:concat("\n") .. close
 
end
 
end
 +
 +
return p

Latest revision as of 13:16, 26 September 2020

Implements {{auto compact ToC}}


local p = {}

local start = [[
__NOTOC__<!--
--><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>]]

local function make_TOC_item(anchor, link_text)
	link_text = link_text or anchor
	return ("* [[#%s|%s]]"):format(anchor, link_text)
end

local Array_mt = { __index = table }
local function Array()
	return setmetatable({}, Array_mt)
end

function p.make_TOC(frame)
	local content = mw.title.getCurrentTitle():getContent()
	
	if not content then
		error "The current page has no content"
	end
	
	local letters = Array()
	-- 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(make_TOC_item(letter))
	end
	
	local yesno = require "Module:Yesno"
	local rest = Array()
	local other_headers = require "Module:TableTools".listToSet{
		"See also", "References", "Notes", "Further reading", "External links",
	}
	for header in content:gmatch "%f[^\n]==%s*(..-)%s*==%f[^=]" do
		if other_headers[header] then
			rest:insert(make_TOC_item(header))
		end
	end
	
	return start .. letters:concat("\n") .. "\n\n" .. rest:concat("\n") .. close
end

return p