Module:TOC: Difference between revisions

From ProleWiki, the proletarian encyclopedia
m (Possible fix)
m (Abolished classes from header 👌️👌️👌️👌️👌️)
 
(7 intermediate revisions by the same user not shown)
Line 1: Line 1:
local p = {}
local p = {}
-- creates a table to capture data processed here


function p.main( frame )
function p.main( frame )
-- create function which will process data from the template (which is the frame). Currently the template is Template:Custom TOC
-- Inside Custom TOC it essentially performs {{#invoke:TOC|main}} which invokes this module and calls the function main
local args = frame:getParent().args
local args = frame:getParent().args
-- creates a table "args" capturing data from template's transclusion, that is, when the template is called as {{Custom TOC}}
local root = mw.html.create()
local root = mw.html.create()
-- creates a variable calling the function which processes HTML building
root = root:tag('div')
root = root:tag('div')
root:addClass('template-toc')
root:addClass('template-toc')
-- attaches a <div class="template-toc"></div> tag inside this HTML
if args.title then
if args.title then
root:tag('div')
root:tag('div')
Line 12: Line 19:
:css('font-weight', 'bold')
:css('font-weight', 'bold')
:wikitext(args.title)
:wikitext(args.title)
-- if there is anything on the "title" parameter, i.e.: {{Custom TOC|title="Title of the work"}}, then create
-- then create a <div>Title of work</div> tag with the options above
end
end


local rowNums = {}
local rowNums = {}
-- creates a table called rowNums
for k,v in pairs(args) do
for k,v in pairs(args) do
-- process every |key=value called by the parameter, i.e. {{CustomTOC|title="Something"|key=value}}, etc.
local num = k:match('^header(%d+)$') or k:match('^section(%d+)$') or k:match('^link(%d+)$')
local num = k:match('^header(%d+)$') or k:match('^section(%d+)$') or k:match('^link(%d+)$')
-- for every key which matches the patterns header1..header2..3., section1..2..3., and link1..2..3..,
if num then table.insert(rowNums, tonumber(num))
if num then table.insert(rowNums, tonumber(num))
-- then store each number into the rowNums table, looking something like { 1, 2, 3, 2, 1, 4, 1 , ... }
end
end
end
end


table.sort(rowNums)
table.sort(rowNums)
 
-- sort the rowNums table, so looking like { 1, 1, 1, 2, 2, 3, 4, ... }
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
table.remove(rowNums, i)
table.remove(rowNums, i)
-- process every index for every number (i.e., inside rowNums table)
-- if there are repeating numbers,
-- then remove them, so the table becomes something like { 1, 2, 3, 4, ... }
end
end
end
end


local section_number = 1
local section_number = 1
local ul = root:tag('ul')
local first_section = true
-- establishes two premises for the next operations


for i, num in ipairs(rowNums) do
for i, num in ipairs(rowNums) do
-- for each number in the rowNums table, follow these steps:
local header = args['header' .. num]
local header = args['header' .. num]
local section = args['section' .. num]
local link = args['link' .. num]
-- name variables so they can be easily called,
-- assign them the value of every template parameter[number] referenced e.g.: {{Custom TOC|header1="value"}}
if header then
if header then
root:tag('div')
root:tag('div')
:addClass('template-toctitle')
:css('font-weight', 'bold')
:css('font-weight', 'bold')
:wikitext(header)
:wikitext(header)
-- if there is a value for header[number],
-- then create a <div>Header</div> tag with the options above
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
-- if there's a link for that section, format it as such
local ul = root:tag('ul')
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')
Line 68: Line 80:
:addClass('toctext')
:addClass('toctext')
:wikitext(section)
:wikitext(section)
-- if there is a value for section[number],
-- then create <ul><li></li></ul> for each new row added
section_number = section_number + 1
section_number = section_number + 1
end
-- increment for each TOC section
end
end
end
end

Latest revision as of 05:00, 24 June 2022

local p = {}
-- creates a table to capture data processed here

function p.main( frame )
-- create function which will process data from the template (which is the frame). Currently the template is Template:Custom TOC
-- Inside Custom TOC it essentially performs {{#invoke:TOC|main}} which invokes this module and calls the function main

	local args = frame:getParent().args
	-- creates a table "args" capturing data from template's transclusion, that is, when the template is called as {{Custom TOC}}
	local root = mw.html.create()
	-- creates a variable calling the function which processes HTML building
	root = root:tag('div')
	root:addClass('template-toc')
	-- attaches a <div class="template-toc"></div> tag inside this HTML
	if args.title then
		root:tag('div')
			:addClass('template-toctitle')
			:tag('big')
			:css('font-weight', 'bold')
			:wikitext(args.title)
	-- if there is anything on the "title" parameter, i.e.: {{Custom TOC|title="Title of the work"}}, then create
		-- then create a <div>Title of work</div> tag with the options above
	end

	local rowNums = {}
	-- creates a table called rowNums
	for k,v in pairs(args) do
	-- process every |key=value called by the parameter, i.e. {{CustomTOC|title="Something"|key=value}}, etc.	
		local num = k:match('^header(%d+)$') or k:match('^section(%d+)$') or k:match('^link(%d+)$')
		-- for every key which matches the patterns header1..header2..3., section1..2..3., and link1..2..3..,
		if num then table.insert(rowNums, tonumber(num))
		-- then store each number into the rowNums table, looking something like { 1, 2, 3, 2, 1, 4, 1 , ... }
		end
	end

	table.sort(rowNums)
	-- sort the rowNums table, so looking like { 1, 1, 1, 2, 2, 3, 4, ... }
	for i = #rowNums, 1, -1 do
		if rowNums[i] == rowNums[i - 1] then
			table.remove(rowNums, i)
	-- process every index for every number (i.e., inside rowNums table)
		-- if there are repeating numbers,
			-- then remove them, so the table becomes something like { 1, 2, 3, 4, ... }
		end
	end

	local section_number = 1
	local first_section = true
	-- establishes two premises for the next operations

	for i, num in ipairs(rowNums) do
	-- for each number in the rowNums table, follow these steps:
		local header = args['header' .. num]
		local section = args['section' .. num]
		local link = args['link' .. num]
		-- name variables so they can be easily called, 
		-- assign them the value of every template parameter[number] referenced e.g.: {{Custom TOC|header1="value"}}

		if header then
			root:tag('div')
				:css('font-weight', 'bold')
				:wikitext(header)
		-- if there is a value for header[number],
			-- then create a <div>Header</div> tag with the options above
		end

		if section then
			if link then
				section = '[[' .. link .. '|' .. section .. ']]'
			end
			-- if there's a link for that section, format it as such
			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)
		-- if there is a value for section[number],
			-- then create <ul><li></li></ul> for each new row added
			section_number = section_number + 1
			-- increment for each TOC section
		end
	end
	return tostring(root)
end
return p