Module:TOC: Difference between revisions

From ProleWiki, the proletarian encyclopedia
m (Possible fix)
m (Attempt at fixing undesirable behaivor)
Line 34: Line 34:
for i, num in ipairs(rowNums) do
for i, num in ipairs(rowNums) do
local header = args['header' .. num]
local header = args['header' .. num]
local section = args['section' .. num]
local link = args['link' .. num]
if header then
if header then
root:tag('div')
root:tag('div')
Line 39: Line 42:
:css('font-weight', 'bold')
:css('font-weight', 'bold')
:wikitext(header)
:wikitext(header)
if not i == 1 then local ul = root:tag('ul')
end
end


local section = args['section' .. num]
local link = args['link' .. num]
if section then
if section then
if link then
if link then
section = '[[' .. link .. '|' .. section .. ']]'
section = '[[' .. link .. '|' .. section .. ']]'
end
end
if header then
local ul = root:tag('ul')
ul:tag('li')
:addClass('toclevel-1')
:tag('span')
:addClass('tocnumber')
:wikitext(section_number)
:done()
:tag('span')
:addClass('toctext')
:wikitext(section)
else
ul:tag('li')
ul:tag('li')
:addClass('toclevel-1')
:addClass('toclevel-1')

Revision as of 01:23, 24 June 2022

local p = {}

function p.main( 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('template-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

	local section_number = 1
	local ul = root:tag('ul')

	for i, num in ipairs(rowNums) do
		local header = args['header' .. num]
		local section = args['section' .. num]
		local link = args['link' .. num]

		if header then
			root:tag('div')
				:addClass('template-toctitle')
				:css('font-weight', 'bold')
				:wikitext(header)
			if not i == 1 then local ul = root:tag('ul')
		end

		if section then
			if link then
				section = '[[' .. link .. '|' .. section .. ']]'
			end
			ul:tag('li')
				:addClass('toclevel-1')
				:tag('span')
					:addClass('tocnumber')
					:wikitext(section_number)
					:done()
				:tag('span')
					:addClass('toctext')
					:wikitext(section)
			section_number = section_number + 1
			end
		end
	end
	return tostring(root)
end
return p