Module:Auto compact TOC/sandbox: Difference between revisions
Appearance
Content deleted Content added
HouseBlaster (talk | contribs) test |
HouseBlaster (talk | contribs) test |
||
Line 2: | Line 2: | ||
local horizontal = require('Module:List').horizontal |
local horizontal = require('Module:List').horizontal |
||
local function |
local function make_TOC_item_linked(anchor, text) |
||
text = text or anchor |
|||
-- if (frame.args.auto == "") |
|||
return ("[[#%s|%s]]"):format(anchor, |
return ("[[#%s|%s]]"):format(anchor, text) |
||
end |
|||
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 21: | Line 25: | ||
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 |
|||
for letter in content:gmatch "%f[^\n]==%s*(%a)%s*==%f[^=]" do |
|||
letter = letter:upper() |
|||
letters:insert(make_TOC_item_linked(letter)) |
|||
end |
|||
else -- f we do not want them linked, don't |
|||
for letter in content:gmatch "%f[^\n]==%s*(%a)%s*==%f[^=]" do |
|||
letter = letter:upper() |
letter = letter:upper() |
||
letters:insert( |
letters:insert(make_TOC_item_unlinked(letter)) |
||
end |
|||
end |
end |
||
Revision as of 01:50, 10 September 2023
![]() | This is the module sandbox page for Module:Auto compact TOC (diff). |
![]() | This module depends on the following other modules: |
Implements {{auto compact TOC}}
local p = {}
local horizontal = require('Module:List').horizontal
local function make_TOC_item_linked(anchor, text)
text = text or anchor
-- if (frame.args.auto == "")
return ("[[#%s|%s]]"):format(anchor, text)
end
local function make_TOC_item_unlinked(anchor, text)
text = text or anchor
return ("%s"):format(anchor, 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.
if frame.args.auto:upper() == "LINK" then -- If we wanted them linked, do that
for letter in content:gmatch "%f[^\n]==%s*(%a)%s*==%f[^=]" do
letter = letter:upper()
letters:insert(make_TOC_item_linked(letter))
end
else -- f we do not want them linked, don't
for letter in content:gmatch "%f[^\n]==%s*(%a)%s*==%f[^=]" do
letter = letter:upper()
letters:insert(make_TOC_item_unlinked(letter))
end
end
return horizontal(letters) .. horizontal(rest)
end
return p