Module:Election results/sandbox: Difference between revisions

Content deleted Content added
add optional |colour1= or |color1= and sanity check for existence of meta/color templates per request on talk page
optional two rounds (probably broken)
Line 1:
require('Module:No globals')
local p = {}
 
function p.main(frame)
local args = require('Module:Arguments').getArgs(frame)
local index, cols = {}, 3
local cand, party, seats, sc, vp = false, false, false, false, false
local winner, winner_votes = {0, 0}, {0, 0}
local rounds = 1
local valid, invalid, electorate = 0, tonumber(args.invalid) or 0, tonumber(args.electorate) or 0
local valid = {0, 0}
local invalid = {tonumber(args.invalid) or 0, tonumber(args.invalid2) or 0}
local electorate = {tonumber(args.electorate) or 0, tonumber(args.electorate2) or tonumber(args.electorate) or 0}
local row
for i = 1, 20 do
if args['cand' .. i] then
Line 19 ⟶ 24:
end
if args['votes' .. i] then
args['votes'local .. i]v = tonumber(args['votes' .. i]) or 0
if args['votes' .. i] = v
if v > winner_votes[1] then
winner[1] = i
winner_votes[1] = args['votes' .. i]v
end
valid[1] = valid[1] + args['votes' .. i]v
end
if args['votes' .. i .. '_2'] then
rounds = 2
local v = tonumber(args['votes' .. i .. '_2']) or 0
args['votes' .. i .. '_2'] = v
if v > winner_votes[2] then
winner[2] = i
winner_votes[2] = v
end
valid[2] = valid[2] + v
end
end
Line 33 ⟶ 49:
:tag('caption')
:wikitext(args.caption)
local row = mw.html.create('tr')
if vp then
row = root:tag('tr')
row
:tag('th')
:wikitext('President')
:attr('colspan', 2)
:attr('rowspan', (rounds > 1) and 2 or nil)
:tag('th')
:wikitext('Vice president')
:attr('rowspan', (rounds > 1) and 2 or nil)
:tag('th')
:wikitext('Party')
:attr('rowspan', (rounds > 1) and 2 or nil)
if (rounds > 1) then
row
:tag('th')
:wikitext('First round')
:attr('colspan', 2)
:tag('th')
:wikitext('Second round')
:attr('colspan', 2)
row = root:tag('tr')
end
row
:tag('th')
:wikitext('Votes')
Line 48 ⟶ 78:
:wikitext('%')
elseif cand then
row = root:tag('tr')
row
:tag('th')
:wikitext('Candidate')
:attr('colspan', 2)
:attr('rowspan', (rounds > 1) and 2 or nil)
:tag('th')
:wikitext('Party')
:attr('rowspan', (rounds > 1) and 2 or nil)
if (rounds > 1) then
row
:tag('th')
:wikitext('First round')
:attr('colspan', 2)
:tag('th')
:wikitext('Second round')
:attr('colspan', 2)
row = root:tag('tr')
end
row
:tag('th')
:wikitext('Votes')
Line 59 ⟶ 103:
:wikitext('%')
elseif sc then
row = root:tag('tr')
if (rounds > 1) then
:tag('th')
row
:wikitext('Party')
:attrtag('colspanth', 2)
:tagwikitext('thParty')
:wikitextattr('Votescolspan', 2)
:tagattr('throwspan', 2)
:wikitexttag('%th')
:tagwikitext('thFirst round')
:wikitextattr('Seatscolspan', 2)
:tag('th')
:wikitext('+/–Second round')
:attr('colspan', 2)
:tag('th')
:wikitext('Seats')
:attr('rowspan', 2)
:tag('th')
:wikitext('+/–')
:attr('rowspan', 2)
row = root:tag('tr')
row
:tag('th')
:wikitext('Votes')
:tag('th')
:wikitext('%')
else
row
:tag('th')
:wikitext('Party')
:attr('colspan', 2)
:tag('th')
:wikitext('Votes')
:tag('th')
:wikitext('%')
:tag('th')
:wikitext('Seats')
:tag('th')
:wikitext('+/–')
end
else
if (rounds > 1) then
row
row
:tag('th')
:wikitext('Party')
:attr('colspan', 2)
:tagattr('throwspan', 2)
:wikitexttag('Votesth')
:tagwikitext('thFirst round')
:wikitextattr('%colspan', 2)
:tag('th')
:wikitext('SeatsSecond round')
:attr('colspan', 2)
:tag('th')
:wikitext('Seats')
:attr('rowspan', 2)
row = root:tag('tr')
:tag('th')
:wikitext('Votes')
:tag('th')
:wikitext('%')
else
row
:tag('th')
:wikitext('Party')
:attr('colspan', 2)
:tag('th')
:wikitext('Votes')
:tag('th')
:wikitext('%')
:tag('th')
:wikitext('Seats')
end
end
root:node(row)
local lang = mw.getContentLanguage()
local function fmt(n)
return n and lang:formatNum(n) or nil
end
local rsuff = (rounds > 1) and {'', '_2'} or {''}
for i, v in ipairs(index) do
local row = mw.html.createroot:tag('tr')
local color = args['colour' .. v] or args['color' .. v]
or (mw.title.new('Template:' .. args['party' .. v] .. '/meta/color').exists
Line 101 ⟶ 194:
:tag('td')
:wikitext(string.format('\[\[%s\]\]', args['cand' .. v]))
row
:tag('td')
:wikitext(string.format('\[\[%s\]\]', args['vp' .. v]))
Line 113 ⟶ 205:
:wikitext(string.format('\[\[%s\]\]', args['party' .. v]))
end
for kk, suf in ipairs(rsuff) do
local cell = mw.html.create('td')
if(args['votes' .. v .. suff]) then
cell
row:tag('td')
:css('text-align', 'right')
if :wikitext(fmt(args['votes' .. v] then.. suff]))
cell row:tag('td')
:wikitextcss(fmt(args['votestext-align', .. v])'right')
:wikitext(string.format('%.2f%%', args['votes' .. v .. suff] / valid * 100))
else
row:tag('td'):attr('colspan', 2)
end
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]))
row
if args['party_name' .. v] then
row:tag('td')
:tagwikitext(args['tdparty_name') .. v] and
:wikitext(string.format('\[\[%s\|%s\]\]', args['party' .. v], args['party_name' .. v])) or
string.format('\[\[%s\]\]', args['party' .. v]) or nil)
else
for kk, suf in ipairs(rsuff) do
row
if(args['votes' .. v .. suff]) then
:tag('td')
row:tag('td')
:wikitext(string.format('\[\[%s\]\]', args['party' .. v]))
:css('text-align', 'right')
:wikitext(fmt(args['votes' .. v .. suff]))
row:tag('td')
:css('text-align', 'right')
:wikitext(string.format('%.2f%%', args['votes' .. v .. suff] / valid * 100))
else
row:tag('td'):attr('colspan', 2)
end
end
elseif seats then
local cell = mw.html.create('td')
cellrow
:csstag('text-align', 'righttd')
if :wikitext(args['votesparty_name' .. v] thenand
string.format('\[\[%s\|%s\]\]', args['party' .. v], args['party_name' .. v]) or
cell
:wikitext(fmt string.format('\[\[%s\]\]', args['votesparty' .. v]) or nil)
for kk, suf in ipairs(rsuff) do
if(args['votes' .. v .. suff]) then
row:tag('td')
:css('text-align', 'right')
:wikitext(fmt(args['votes' .. v .. suff]))
row:tag('td')
:css('text-align', 'right')
:wikitext(string.format('%.2f%%', args['votes' .. v .. suff] / valid * 100))
else
row:tag('td'):attr('colspan', 2)
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
row
local cell = mw.html.create('td')
cell:tag('td')
: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(fmt(args['seats' .. v]))
end
row:node(cell)
local cell = mw.html.create('td')
cell
Line 201 ⟶ 269:
row:node(cell)
end
root:node(row)
end
row = root
:tag('tr')
:addClass('sortbottom')
:css('background', '#eaecf0')
:css('text-align', 'right')
row
:tag('td')
:wikitext('Valid votes')
:attr('colspan', cols - 2)
:css('text-align', 'left')
for k=1,rounds do
row
:tag('td')
:wikitext('Valid votes'fmt(valid[k]))
:attr('colspan', cols - 2)
:css('text-align', 'left')
:tag('td')
:wikitext(fmtstring.format('%.2f%%', valid[k] / (valid[k] + invalid[k]) * 100))
end
:tag('td')
row = root:tag('tr')
:wikitext(string.format('%.2f%%', valid / (valid + invalid) * 100))
:tag('tr')
:addClass('sortbottom')
:css('background', '#eaecf0')
:css('text-align', 'right')
row
:tag('td')
:wikitext('Invalid votes')
:attr('colspan', cols - 2)
:css('text-align', 'left')
for k=1,rounds do
row
:tag('td')
:wikitext('Invalid votes'fmt(invalid[k]))
:attr('colspan', cols - 2)
:css('text-align', 'left')
:tag('td')
:wikitext(fmtstring.format('%.2f%%', invalid[k] / (valid[k] + invalid[k]) * 100))
end
:tag('td')
row = root:tag('tr')
:wikitext(string.format('%.2f%%', invalid / (valid + invalid) * 100))
:tag('tr')
:addClass('sortbottom')
:css('font-weight', 'bold')
:css('background', '#eaecf0')
:css('text-align', 'right')
row
:tag('td')
:wikitext('Total votes')
:attr('colspan', cols - 2)
:css('text-align', 'left')
for k=1,rounds do
row
:tag('td')
:wikitext('Totalfmt(valid[k] votes'+ invalid[k]))
:attr('colspan', cols - 2)
:css('text-align', 'left')
:tag('td')
:wikitext(fmt(valid + invalid))
:tag('td')
:wikitext('100%')
end
:tag('tr')
row = root:tag('tr')
:addClass('sortbottom')
:css('background', '#eaecf0')
:css('text-align', 'right')
row:tag('td')
:wikitext('Registered voters/turnout')
:attr('colspan', cols - 2)
:css('text-align', 'left')
for k=1,rounds do
:tag('td')
row:tag('td')
:wikitext(fmt(electorate[k]))
row:tag('td')
:wikitext(string.format('%.2f%%', (valid[k] + invalid[k]) / electorate[k] * 100))
end
:tag('tr')
row = root:tag('tr')
:addClass('sortbottom')
:css('background', '#eaecf0')
:css('text-align', 'right')
row:tag('td')
:wikitext('Source: ', args.source)
:attr('colspan', cols - 0)
:css('text-align', 'left')
return tostring(root)
end
 
return p