m (Testing tocnumbers) |
m (Improved code for numbering sections) |
||
Line 40: | Line 40: | ||
:tag('span') | :tag('span') | ||
:addClass('tocnumber') | :addClass('tocnumber') | ||
:wikitext(a) | |||
:done() | :done() | ||
:tag('span') | :tag('span') | ||
:addClass('toctext') | |||
:wikitext(section) | :wikitext(section) | ||
a = a + 1 | a = a + 1 |
Revision as of 17:05, 23 June 2022
local p = {}
function p.toc( frame )
local args = frame:getParent().args
local root = mw.html.create()
root = root:tag('div')
root:addClass('template-toc')
if args.title then
root:tag('div')
:addClass('toctitle')
:tag('big')
:css('font-weight', 'bold')
:wikitext(args.title)
end
local rowNums = {}
for k,v in pairs(args) do
local num = k:match('^header(%d+)$') or k:match('^section(%d+)$') or k:match('^link(%d+)$')
if num then table.insert(rowNums, tonumber(num))
end
end
table.sort(rowNums)
for i = #rowNums, 1, -1 do
if rowNums[i] == rowNums[i - 1] then
table.remove(rowNums, i)
end
end
for i, num in ipairs(rowNums) do
local header = args['header' .. num]
if header then
root:tag('div')
:addClass('toctitle')
:css('font-weight', 'bold')
:wikitext(header)
end
local section = args['section' .. num]
local ul = root:tag('ul')
a = 1
if section then
ul:tag('li')
:addClass('toclevel-1')
:tag('span')
:addClass('tocnumber')
:wikitext(a)
:done()
:tag('span')
:addClass('toctext')
:wikitext(section)
a = a + 1
end
end
return tostring(root)
end
return p