Module:Sandbox/Ythlev

local p = {} function p.main(frame) local args = require('Module:Arguments').getArgs(frame) local index, cols = {}, 3 local party, vp = false, false local winner, winner_votes = 0, 0 local 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') local row = mw.html.create('tr') if party then row :tag('th') :wikitext('Party') :attr('colspan', 2) end if vp then 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 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] 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 vp 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 local cell = mw.html.create('td') cell :css('text-align', 'right') if args['votes' .. v] then cell :wikitext(fmt(args['votes' .. v])) end if v == winner then cell :css('font-weight', 'bold') 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 if v == winner then cell :css('font-weight', 'bold') end row:node(cell) root:node(row) end root :tag('tr') :css('background', '#eaecf0') :css('text-align', 'right') :tag('td') :wikitext('Valid votes') :attr('colspan', cols - 2) :tag('td') :wikitext(fmt(valid)) :tag('td') :wikitext(string.format('%.2f', valid / (valid + invalid) * 100)) :tag('tr') :css('background', '#eaecf0') :css('text-align', 'right') :tag('td') :wikitext('Invalid votes') :attr('colspan', cols - 2) :tag('td') :wikitext(fmt(invalid)) :tag('td') :wikitext(string.format('%.2f', invalid / (valid + invalid) * 100)) :tag('tr') :css('font-weight', 'bold') :css('background', '#eaecf0') :css('text-align', 'right') :tag('td') :wikitext('Total votes') :attr('colspan', cols - 2) :tag('td') :wikitext(fmt(valid + invalid)) :tag('td') :wikitext('100.00') :tag('tr') :css('background', '#eaecf0') :css('text-align', 'right') :tag('td') :wikitext('Electorate–turnout') :attr('colspan', cols - 2) :tag('td') :wikitext(fmt(electorate)) :tag('td') :wikitext(string.format('%.2f', (valid + invalid) / electorate * 100)) return tostring(root) end return p