Module:FootnoteUtil

From Call of Duty Esports Wiki
Jump to: navigation, search

To edit the documentation or categories for this module, click here.


local util_map = require('Module:MapUtil')
local util_table = require('Module:TableUtil')

local p = {}

function p.tagFootnotePlain(tbl,n)
	if not n or n == '' then return end
	tbl:tag('sup'):wikitext(n)
end

function p.init()
	-- start global table of footnotes
	footnote_table = {
		nodes = {},
		texts = {}
	}
end

function p.tag(node, tbl)
	if not tbl then return end
	if type(tbl) == 'string' then tbl = { tbl } end
	if not next(tbl) then return end
	local sup = node:tag('sup')
	footnote_table.nodes[#footnote_table.nodes+1] = sup
	footnote_table.texts[#footnote_table.texts+1] = util_map.arrayInPlace(tbl, p.processFootnote)
end

function p.processFootnote(text)
	local text = text:gsub('```(.-)!!!(.-)@@@', '<ref name="%1">%2</ref>')
	return text
end
	

function p.printTexts(tbl, frame)
	if #footnote_table.nodes == 0 then
		return
	end
	if not frame then
		frame = mw.getCurrentFrame()
	end
	local endlist = p.resolveFootnotes()
	local div_outer = tbl:tag('div'):addClass('footnotes-list-outer')
	local div_inner = div_outer:tag('div'):addClass('footnotes-list-inner')
	local dl = div_inner:tag('dl')
	for k, text in ipairs(endlist) do
		dl:tag('dd')
			:tag('span')
				:addClass('footnotes-list-text')
				:tag('sup')
					:wikitext(k)
				:done()
				:wikitext(' ', frame:preprocess(text))
	end
end

function p.resolveFootnotes()
	if #footnote_table.nodes == 0 then
		return
	end
	local lookup = {}
	local endlist = {}
	local nodelist = {}
	local currentindex = 1
	for k, row in ipairs(footnote_table.texts) do
		local thisnode = {}
		for i, text in ipairs(row) do
			if not lookup[text] then
				lookup[text] = currentindex
				currentindex = currentindex + 1
				endlist[#endlist+1] = text
			end
			thisnode[#thisnode+1] = lookup[text]
		end
		nodelist[k] = thisnode
	end
	for k, node in ipairs(footnote_table.nodes) do
		local numbers = nodelist[k]
		node:wikitext(table.concat(util_table.sortUnique(numbers),','))
	end
	return endlist
end

return p