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 13:09, 17 June 2020. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
local getArgs = require('Module:Arguments').getArgs
local p = {}

function main(args)
	local tb = mw.html.create('table')
	tb
		:addClass('wikitable')
		:tag('tr')
			:tag('th')
				:wikitext('Party')
				:attr('colspan', '2')
			:tag('th')
				:wikitext('Candidate')
			:tag('th')
				:wikitext('Votes')
			:tag('th')
				:wikitext('%')
	
	local lang = mw.getContentLanguage()
	local function fmt(n)
		return lang:formatNum(n)
	end
	
	local index, valid = {}, 0
	for i = 1, 99 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
	for i, v in ipairs(index) do
			tb
				:tag('tr')
					:tag('td')
						:css('background-color', string.format('{{%s/meta/color}}', args['party' .. v]))
					:tag('td')
						:wikitext(string.format('\[\[%s\]\]', args['party' .. v]))
					:tag('td')
						:wikitext(string.format('\[\[%s\]\]', args['cand' .. v]))
					:tag('td')
						:wikitext(fmt(args['votes' .. v]))
					:tag('td')
						:wikitext(string.format('%.2f', args['votes' .. v] / valid * 100))
	end
	
	args.invalid, args.electorate = tonumber(args.invalid), tonumber(args.electorate)
	tb
		:tag('tr')
			:css('background', '#EAECF0')
			:tag('td')
				:wikitext('Valid votes')
				:attr('colspan', '3')
				: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', '3')
				: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', '3')
				: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', '3')
				:css('text-align', 'right')
			:tag('td')
				:wikitext(fmt(args.electorate))
			:tag('td')
				:wikitext(string.format('%.2f', (valid + args.invalid) / args.electorate * 100))
	return tostring(tb)
end

function p.main(frame)
	return main(getArgs(frame))
end

return p