Module:Position history

From NPOWiki
Jump to navigation Jump to search

Documentation for this module may be created at Module:Position history/doc

local datediff = require('Module:Date difference').call
local p = {}

local function argDefault(arg, default)
	if not arg or mw.text.trim(arg) == '' then
		return default or ''
	else
		return arg
	end
end

local function makeCell(content)
	return '\t\t<td>' .. content .. '</td>\n'
end

function p.main(frame)
	return p.call(frame:getParent().args)
end

function p.call(args)
	local incumbentString = '&mdash;'
	local html = ''
	local num = 1
	local row, holder, startDate, endDate
	
	repeat
		holder = argDefault(args[num], '{{{' .. num .. '}}}')
		startDate = argDefault(args[num + 1], 'unknown')
		endDate = argDefault(args[num + 2], 'unknown')
		row = '\t<tr>\n'
		if (endDate == 'unknown' or endDate == incumbentString) and num == 1 then
			endDate = incumbentString
			row = '\t<tr style="font-weight:bold;">\n'
		end
		
		row = row .. makeCell(startDate) .. makeCell(holder) .. makeCell(endDate)
		if startDate ~= 'unknown' and endDate ~= 'unknown' then
			if endDate == incumbentString then
				row = row .. makeCell(datediff({ startDate }) .. ' Days')
			else
				row = row .. makeCell(datediff({ startDate, endDate }) .. ' Days')
			end
		else
			row = row .. makeCell('? Days')
		end
		row = row .. '\t</tr>\n'
		html = html .. row
		num = num + 3
	until not args[num]
	
	return html
end

return p