Jump to content

Module:Note

From Wikisource

require('strict')

local getArgs = require('Module:Arguments').getArgs
local yesno = require('Module:Yesno')
local TableTools = require('Module:TableTools')

local p = {}

local function _note(args)
	local styles = mw.getCurrentFrame():extensionTag('templatestyles', '', {src = 'Note/styles.css'})
	
	local id = args[1] or args.id or '{{{1}}}'
	
	local cite = mw.html.create('cite')
		:attr('id', 'endnote_' .. id)
		:addClass('wst-endnote')
	
	if yesno(args.italics) == false then
		cite:addClass('wst-endnote-no-italics')
	else
		cite:addClass('wst-endnote-italics')
	end
	
	if yesno(args.bold) == false then
		cite:addClass('wst-endnote-no-bold')
	else
		cite:addClass('wst-endnote-bold')
	end
	
	local refs = {}
	for k, v in pairs(args) do
		if string.match(k, '^ref%d*$') then
			local n = string.gsub(k, '^ref(%d*)$', '%1')
			n = tonumber(n) or 1
			refs[n] = refs[n] or {}
			refs[n]['id'] = refs[n]['id'] or v
		elseif string.match(k, '^ref%d*%-display$') then
			local n = string.gsub(k, '^ref(%d*)%-display$', '%1')
			n = tonumber(n) or 1
			refs[n] = refs[n] or {}
			refs[n]['display'] = refs[n]['display'] or v
		end
	end
	refs = TableTools.compressSparseArray(refs)
	
	local ref_len = #refs
	for i = 1, ref_len do
		if not refs[i]['id'] then
			refs[i] = nil
		end
	end
	refs = TableTools.compressSparseArray(refs)
	
	if #refs == 0 then
		refs = {
			{id = id, display = args[2]}
		}
	end
	
	local reflinks = {}
	local cat = ''
	for i, v in ipairs(refs) do
		local display = v.display or args[2]
		if #refs > 1 then
			display = display or '↑ ' .. i
		else
			display = display or '↑'
		end
		table.insert(reflinks, '[[#ref_' .. v.id .. '|' .. display .. ']]')
		if v.display or args[2] then
			cat = '[[Category:' .. 'Footnotes using custom labels' .. ']]'
		end
	end
	
	cite:wikitext(table.concat(reflinks, ' '))
	
	return styles .. tostring(cite) .. ' ' .. cat
end

function p.note(frame)
	return _note(getArgs(frame))
end

return p