Module:FootnoteUtil

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.initializeAllFootnotes -- start global table of footnotes footnote_table = { nodes = {}, texts = {} } end

function p.tagFootnote(node, text) if not text then return end if not footnote_table then return end local sup = node:tag('sup') footnote_table.nodes[#footnote_table.nodes+1] = sup footnote_table.texts[#footnote_table.texts+1] = { p.processFootnote(text) } end

function p.tagFootnotes(node, tbl) if not tbl or 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('```(.-)!!!(.-)@@@', ' ') return text end

function p.printFootnotes(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