Jump to content

Module:Sandbox/Ythlev

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Ythlev (talk | contribs) at 17:23, 17 June 2020. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
local p = {}
function p.main(frame)
	local args = require('Module:Arguments').getArgs(frame)
	local yesno = require('Module:Yesno')
	local root = mw.html.create('table')
	local cols = 5
	root
		:addClass('wikitable')
	local row = mw.html.create('tr')
	row
		:tag('th')
			:wikitext('Party')
			:attr('colspan', 2)
	if yesno(args.vp) == true then
		cols = cols + 1
		row
			:tag('th')
				:wikitext('President')
			:tag('th')
				:wikitext('Vice president')
	else
		row
			:tag('th')
				:wikitext('Candidate')
	end
	row
		:tag('th')
			:wikitext('Votes')
		:tag('th')
			:wikitext('%')
	root:node(row)
	local index, valid = {}, 0
	for i = 1, 20 do
		if args['party' .. i] ~= nil and
			args['cand' .. i] ~= nil and
			args['votes' .. i] ~= nil
		then
			table.insert(index, i)
			args['votes' .. i] = tonumber(args['votes' .. i])
			valid = valid + args['votes' .. i]
		end
	end
	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 args['party_name' .. v] ~= nil then
			row
				:tag('td')
					:wikitext(string.format('\[\[%s\]\]', args['party_name' .. v]))
		else
			row
				:tag('td')
					:wikitext(string.format('\[\[%s\]\]', args['party' .. v]))
		end
		if yesno(args.vp) == true then
			row
				:tag('td')
					:wikitext(string.format('\[\[%s\]\]', args['cand' .. v]))
			row
				:tag('td')
					:wikitext(string.format('\[\[%s\]\]', args['vp' .. v]))
		else
			row
				:tag('td')
					:wikitext(string.format('\[\[%s\]\]', args['cand' .. v]))
		end
		row
			:tag('td')
				:wikitext(fmt(args['votes' .. v]))
			:tag('td')
				:wikitext(string.format('%.2f', args['votes' .. v] / valid * 100))
		root:node(row)
	end
	args.invalid, args.electorate = tonumber(args.invalid), tonumber(args.electorate)
	root
		:tag('tr')
			:css('background', '#eaecf0')
			:tag('td')
				:wikitext('Valid votes')
				:attr('colspan', cols - 2)
				:css('text-align', 'right')
			:tag('td')
				:wikitext(fmt(valid))
			:tag('td')
				:wikitext(string.format('%.2f', valid / (valid + args.invalid) * 100))
		:tag('tr')
			:css('background', '#eaecf0')
			:tag('td')
				:wikitext('Invalid votes')
				:attr('colspan', cols - 2)
				:css('text-align', 'right')
			:tag('td')
				:wikitext(fmt(args.invalid))
			:tag('td')
				:wikitext(string.format('%.2f', args.invalid / (valid + args.invalid) * 100))
		:tag('tr')
			:css('background', '#eaecf0')
			:css('font-weight', 'bold')
			:tag('td')
				:wikitext('Total votes')
				:attr('colspan', cols - 2)
				:css('text-align', 'right')
			:tag('td')
				:wikitext(fmt(valid + args.invalid))
			:tag('td')
				:wikitext('100.00')
		:tag('tr')
			:css('background', '#eaecf0')
			:tag('td')
				:wikitext('Electorate–turnout')
				:attr('colspan', cols - 2)
				:css('text-align', 'right')
			:tag('td')
				:wikitext(fmt(args.electorate))
			:tag('td')
				:wikitext(string.format('%.2f', (valid + args.invalid) / args.electorate * 100))
	return tostring(root)
end
return p