Module:Election results/sandbox

This is an old revision of this page, as edited by Number 57 (talk | contribs) at 22:04, 2 August 2020 (?). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
require('Module:No globals')
local p = {}
function p.main(frame)
	local args = require('Module:Arguments').getArgs(frame)
	local index, cols = {}, 3
	local cand, party, sc, vp = false, false
	local winner, winner_votes = 0, 0
	local seats, valid, invalid, electorate = 0, tonumber(args.invalid) or 0, tonumber(args.electorate) or 0
	for i = 1, 20 do
		if args['cand' .. i] then
			table.insert(index, i)
			if not party and args['party' .. i] then
				party = true
				cols = cols + 2
			end
			if not vp and args['vp' .. i] then
				vp = true
				cols = cols + 1
			end
			if args['votes' .. i] then
				args['votes' .. i] = tonumber(args['votes' .. i])
				if args['votes' .. i] > winner_votes then
					winner = i
					winner_votes = args['votes' .. i]
				end
				valid = valid + args['votes' .. i]
			end
		end
	end
	local root = mw.html.create('table')
	root
		:addClass('wikitable sortable')
		:tag('caption')
			:wikitext(args.caption)
	local row = mw.html.create('tr')
	if vp then
		row
			:tag('th')
				:wikitext('President')
				:attr('colspan', 2)
			:tag('th')
				:wikitext('Vice president')
			:tag('th')
				:wikitext('Party')
			:tag('th')
				:wikitext('Votes')
			:tag('th')
				:wikitext('%')
	elseif cand then
		row
			:tag('th')
				:wikitext('Candidate')
				:attr('colspan', 2)
			:tag('th')
				:wikitext('Party')
			:tag('th')
				:wikitext('Votes')
			:tag('th')
				:wikitext('%')
	elseif sc then
		row
			:tag('th')
				:wikitext('Party')
				:attr('colspan', 2)
			:tag('th')
				:wikitext('Votes')
			:tag('th')
				:wikitext('%')
			:tag('th')
				:wikitext('Seats')
			:tag('th')
				:wikitext('+/–')
	else
		row
			:tag('th')
				:wikitext('Party')
				:attr('colspan', 2)
			:tag('th')
				:wikitext('Votes')
			:tag('th')
				:wikitext('%')
			:tag('th')
				:wikitext('Seats')
	end
	root:node(row)
	local lang = mw.getContentLanguage()
	local function fmt(n)
		return lang:formatNum(n)
	end
	for i, v in ipairs(index) do
		local row = mw.html.create('tr')
		row
			:tag('td')
				:css('background-color', frame:expandTemplate{title = args['party' .. v] .. '/meta/color'})
		if vp then
			row
				:tag('td')
					:wikitext(string.format('\[\[%s\]\]', args['cand' .. v]))
			row
				:tag('td')
					:wikitext(string.format('\[\[%s\]\]', args['vp' .. v]))
			if args['party_name' .. v] then
				row
					:tag('td')
						:wikitext(string.format('\[\[%s\|%s\]\]', args['party' .. v], args['party_name' .. v]))
			else
				row
					:tag('td')
						:wikitext(string.format('\[\[%s\]\]', args['party' .. v]))
			end
			local cell = mw.html.create('td')
				cell
					:css('text-align', 'right')
				if args['votes' .. v] then
				cell
					:wikitext(fmt(args['votes' .. v]))
				end
				row:node(cell)
			local cell = mw.html.create('td')
				cell
					:css('text-align', 'right')
				if args['votes' .. v] then
				cell
					:wikitext(string.format('%.2f%%', args['votes' .. v] / valid * 100))
				end
				row:node(cell)
		elseif cand then
			row
				:tag('td')
					:wikitext(string.format('\[\[%s\]\]', args['cand' .. v]))
			if args['party_name' .. v] then
				row
					:tag('td')
						:wikitext(string.format('\[\[%s\|%s\]\]', args['party' .. v], args['party_name' .. v]))
			else
				row
					:tag('td')
						:wikitext(string.format('\[\[%s\]\]', args['party' .. v]))
			end
			local cell = mw.html.create('td')
				cell
					:css('text-align', 'right')
				if args['votes' .. v] then
				cell
					:wikitext(fmt(args['votes' .. v]))
				end
				row:node(cell)
			local cell = mw.html.create('td')
				cell
					:css('text-align', 'right')
				if args['votes' .. v] then
				cell
					:wikitext(string.format('%.2f%%', args['votes' .. v] / valid * 100))
				end
				row:node(cell)
		elseif seats then
			if args['party_name' .. v] then
				row
					:tag('td')
						:wikitext(string.format('\[\[%s\|%s\]\]', args['party' .. v], args['party_name' .. v]))
			else
				row
					:tag('td')
						:wikitext(string.format('\[\[%s\]\]', args['party' .. v]))
			end
			local cell = mw.html.create('td')
				cell
					:css('text-align', 'right')
				if args['votes' .. v] then
					cell
						:wikitext(fmt(args['votes' .. v]))
				end
				row:node(cell)
			local cell = mw.html.create('td')
				cell
					:css('text-align', 'right')
				if args['votes' .. v] then
					cell
					:wikitext(string.format('%.2f%%', args['votes' .. v] / valid * 100))
				end
				row:node(cell)
			local cell = mw.html.create('td')
				cell
					:css('text-align', 'right')
				if args['seats' .. v] then
				cell
					:wikitext(args['seats' .. v])
				end
				row:node(cell)
			local cell = mw.html.create('td')
				cell
					:css('text-align', 'right')
				if args['sc' .. v] then
				cell
					:wikitext(args['sc' .. v])
				end
				row:node(cell)
			end
		root:node(row)
	end
	root
		:tag('tr')
			:addClass('sortbottom')
			:css('background', '#eaecf0')
			:css('text-align', 'right')
			:tag('td')
				:wikitext('Valid votes')
				:attr('colspan', cols - 2)
 			        :css('text-align', 'left')
			:tag('td')
				:wikitext(fmt(valid))
			:tag('td')
				:wikitext(string.format('%.2f%%', valid / (valid + invalid) * 100))
		:tag('tr')
			:addClass('sortbottom')
			:css('background', '#eaecf0')
			:css('text-align', 'right')
			:tag('td')
				:wikitext('Invalid votes')
				:attr('colspan', cols - 2)
 			        :css('text-align', 'left')
			:tag('td')
				:wikitext(fmt(invalid))
			:tag('td')
				:wikitext(string.format('%.2f%%', invalid / (valid + invalid) * 100))
		:tag('tr')
			:addClass('sortbottom')
			:css('font-weight', 'bold')
			:css('background', '#eaecf0')
			:css('text-align', 'right')
			:tag('td')
				:wikitext('Total votes')
				:attr('colspan', cols - 2)
 			        :css('text-align', 'left')
			:tag('td')
				:wikitext(fmt(valid + invalid))
			:tag('td')
				:wikitext('100%')
		:tag('tr')
			:addClass('sortbottom')
			:css('background', '#eaecf0')
			:css('text-align', 'right')
			:tag('td')
				:wikitext('Registered voters/turnout')
				:attr('colspan', cols - 2)
 			        :css('text-align', 'left')
			:tag('td')
				:wikitext(fmt(electorate))
			:tag('td')
				:wikitext(string.format('%.2f%%', (valid + invalid) / electorate * 100))
		:tag('tr')
			:addClass('sortbottom')
			:css('background', '#eaecf0')
			:css('text-align', 'right')
			:tag('td')
				:wikitext('Source: ', args.source)
				:attr('colspan', cols - 0)
 			        :css('text-align', 'left')
	return tostring(root)
end
return p