m (Began to write down comments on these modules so they can become more accessible) |
m (Added full comments) |
||
Line 28: | Line 28: | ||
-- process every |key=value called by the parameter, i.e. {{CustomTOC|title="Something"|key=value}}, etc. | -- 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.., | -- 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 , ... } | -- then store each number into the rowNums table, looking something like { 1, 2, 3, 2, 1, 4, 1 , ... } | ||
Line 50: | Line 50: | ||
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 section = args['section' .. num] | ||
local link = args['link' .. 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 | ||
Line 59: | Line 62: | ||
: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 | ||
Line 65: | Line 70: | ||
section = '[[' .. link .. '|' .. section .. ']]' | section = '[[' .. link .. '|' .. section .. ']]' | ||
end | end | ||
if | -- if there's a link for that section, format it as such | ||
local ul = root:tag('ul') | |||
ul:tag('li') | ul:tag('li') | ||
:addClass('toclevel-1') | :addClass('toclevel-1') | ||
Line 78: | Line 81: | ||
: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 | ||
-- increment for each TOC section | |||
end | end | ||
end | end |
Revision as of 04:50, 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')
:addClass('template-toctitle')
: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