Module:TOC: Difference between revisions

From ProleWiki, the proletarian encyclopedia
m (Removed debug options)
Tag: Manual revert
m (Added few spaces to improve readability, using function "main")
Line 1: Line 1:
local p = {}
local p = {}
function p.toc( frame )
 
function p.main( frame )
local args = frame:getParent().args
local args = frame:getParent().args
local root = mw.html.create()
local root = mw.html.create()
Line 12: Line 13:
:wikitext(args.title)
:wikitext(args.title)
end
end
local rowNums = {}
local rowNums = {}
for k,v in pairs(args) do
for k,v in pairs(args) do
Line 18: Line 20:
end
end
end
end
table.sort(rowNums)
table.sort(rowNums)
for i = #rowNums, 1, -1 do
for i = #rowNums, 1, -1 do
if rowNums[i] == rowNums[i - 1] then
if rowNums[i] == rowNums[i - 1] then
Line 24: Line 28:
end
end
end
end
local section_number = 1
local section_number = 1
for i, num in ipairs(rowNums) do
for i, num in ipairs(rowNums) do
local header = args['header' .. num]
local header = args['header' .. num]
Line 33: Line 39:
:wikitext(header)
:wikitext(header)
end
end
local section = args['section' .. num]
local section = args['section' .. num]
local link = args['link' .. num]
local link = args['link' .. num]

Revision as of 00:17, 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

	for i, num in ipairs(rowNums) do
		local header = args['header' .. num]
		if header then
			root:tag('div')
				:addClass('template-toctitle')
				:css('font-weight', 'bold')
				:wikitext(header)
		end

		local section = args['section' .. num]
		local link = args['link' .. num]
		local ul = root:tag('ul')
		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
	return tostring(root)
end
return p