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 = {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
if v > winner_votes[1] then winner[1] = i
winner_votes[1] =
end
valid[1] = valid[1] +
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)
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
row
:
:
:
:
:
:
:
:tag('th')
:wikitext('
: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
:tag('th') :wikitext('Party')
:attr('colspan', 2)
:
:
:
:
:tag('th')
:wikitext('
: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
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 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]))
: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
if(args['votes' .. v .. suff]) then
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 cand then
row
:tag('td')
:wikitext(string.format('\[\[%s\]\]', args['cand' .. v]))
row
:
string.format('\[\[%s\]\]', args['party' .. 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
end
elseif seats then
string.format('\[\[%s\|%s\]\]', args['party' .. v], args['party_name' .. v]) or
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
end
row
:css('text-align', 'right')
:wikitext(fmt(args['seats' .. v]))
local cell = mw.html.create('td')
cell
Line 201 ⟶ 269:
row:node(cell)
end
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(
:tag('td')
:wikitext(
end
row = root: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(
:tag('td')
:wikitext(
end
row = root: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(
:tag('td')
:wikitext('100%')
end
row = root:tag('tr')
:addClass('sortbottom')
:css('background', '#eaecf0')
:css('text-align', 'right')
for k=1,rounds do
row:tag('td')
:wikitext(fmt(electorate[k])) end
row = root:tag('tr')
:addClass('sortbottom')
:css('background', '#eaecf0')
:css('text-align', 'right')
return tostring(root)
end
return p
|