Jump to content

Module:Auto compact TOC/sandbox: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
Create sandbox version of Module:Auto compact TOC
 
test
Line 1: Line 1:
local p = {}
local p = {}
local horizontal = require('Module:List').horizontal
local horizontal = require('Module:List').horizontal
local start = [[
__NOTOC__<div id="toc" class="toc plainlinks" role="navigation" aria-labelledby="mw-toc-heading">
<div class="toctitle"><h2 id="mw-toc-heading">Contents</h2></div>
]]

local close = [[</div>]]


local function make_TOC_item(anchor, link_text)
local function make_TOC_item(anchor, link_text)
Line 32: Line 26:
end
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 .. horizontal(letters) .. horizontal(rest) .. close
return horizontal(letters) .. horizontal(rest)
end
end



Revision as of 01:36, 10 September 2023

local p = {}
local horizontal = require('Module:List').horizontal

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
	
	
	return   horizontal(letters) .. horizontal(rest) 
end

return p