Module:Auto compact TOC/sandbox: Difference between revisions
Appearance
Content deleted Content added
HouseBlaster (talk | contribs) test |
sync to live |
||
Line 1: | Line 1: | ||
require('strict') |
|||
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>]] |
|||
⚫ | |||
⚫ | |||
⚫ | |||
-- if (frame.args.auto == "") |
|||
⚫ | |||
return ("[[#%s|%s]]"):format(anchor, |
return ("[[#%s|%s]]"):format(anchor, link_text) |
||
⚫ | |||
local function make_TOC_item_unlinked(anchor, text) |
|||
text = text or anchor |
|||
return ("%s"):format(anchor, text) |
|||
end |
end |
||
local Array_mt = { __index = table } |
local Array_mt = { __index = table } |
||
local function Array() |
local function Array() |
||
Line 25: | Line 28: | ||
local letters = Array() |
local letters = Array() |
||
-- Find uppermost headers containing a single ASCII letter. |
-- Find uppermost headers containing a single ASCII letter. |
||
⚫ | |||
if frame.args.auto:upper() == "LINK" then -- If we wanted them linked, do that |
|||
⚫ | |||
letter = letter:upper() |
letter = letter:upper() |
||
letters:insert( |
letters:insert(make_TOC_item(letter)) |
||
⚫ | |||
else -- f we do not want them linked, don't |
|||
⚫ | |||
letter = letter:upper() |
|||
letters:insert(make_TOC_item_unlinked(letter)) |
|||
end |
|||
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", |
|||
} |
|||
⚫ | |||
if other_headers[header] then |
|||
rest:insert(make_TOC_item(header)) |
|||
⚫ | |||
⚫ | |||
return horizontal(letters) .. horizontal(rest) |
return start .. horizontal(letters) .. horizontal(rest) .. close |
||
end |
end |
||
Revision as of 05:45, 29 September 2024
![]() | This is the module sandbox page for Module:Auto compact TOC (diff). |
![]() | This module depends on the following other modules: |
Implements {{auto compact TOC}}
require('strict')
local p = {}
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)
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 .. horizontal(letters) .. horizontal(rest) .. close
end
return p