Module:Election results/sandbox: Difference between revisions

Content deleted Content added
No edit summary
refactoring
Line 4:
function p.main(frame)
local args = require('Module:Arguments').getArgs(frame)
local index, colsheadings = {}, 3{}
local candcols, party, seats, sc, vprounds = false, false, false, false0, false1
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, secondrow
local tracking = ''
-- helper functions
local lang = mw.getContentLanguage()
local function fmt(n)
return n and lang:formatNum(n) or nil
end
local function pct(n,d)
n, d = tonumber(n), tonumber(d)
if n and d and d > 0 then
return string.format('%.2f', n / d * 100)
end
return '–'
end
local function unlink(s)
if s then
s = s:match(".-%[%[(.-)|.-%]%].*")
s = s:match(".-%[%[(.-)%]%].*")
end
return s
end
 
-- preprocess the input
for i = 1, 20 do
for k, key in ipairs({'cand', 'vp', 'party', 'sc', 'seats'}) do
if args['cand' .. i] then
if args[key .. i] then
cand = true
headings[key] = true
table.insert(index, i)
if not party and args['party' .. i] then
party = true
cols = cols + 2
end
end
if not vp and args['vp' .. i] then
-- temporary tracking
vp = true
for k, key in ipairs({'party', 'cand', 'vp'}) do
cols = cols + 1
if args[key .. i] then
if unlink(args[key .. i]) == args[key .. i] then
tracking = tracking .. '[[Category:Pages using election results with unlinked ' .. key .. '|' .. i .. ']]'
end
end
end
if args['cand' .. i] or args['party' .. i] then
table.insert(index, i)
if args['votes' .. i] then
local votesi = tonumber(args['votes' .. i]) or 0
Line 42 ⟶ 68:
end
valid[2] = valid[2] + votesi
end
if args['sc' .. i] then
sc = true
end
if args['seats' .. i] then
seats = true
end
end
end
 
cand = ((sc or seats) and false) or cand
-- build the table
local root = mw.html.create('table')
root
Line 57 ⟶ 78:
:tag('caption')
:wikitext(args.caption)
 
if vp then
local rowspan = (rounds > 1) and 2 or nil
row = root:tag('tr')
row = root:tag('tr')
if headings['cand'] then
row
:tag('th')
:wikitext(headings['vp'] and 'President' or 'Candidate')
:attr('scope', 'col')
:attr('colspan', 2)
:attr('rowspan', (rounds > 1) and 2 or nilrowspan)
cols = cols + 2
:tag('th')
if headings['vp'] then
: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('FirstVice roundpresident')
:attr('colspanscope', 2'col')
:tagattr('throwspan', rowspan)
cols = cols + 1
:wikitext('Second round')
:attr('colspan', 2)
row = root:tag('tr')
end
if headings['party'] then
for k=1,rounds do
row
:tag('th')
:wikitextattr('Votesscope', 'col')
:tagattr('throwspan', rowspan)
cols = cols + 1
:wikitext('%')
end
elseif candheadings['party'] then
row = root:tag('tr')
row
:tag('th')
:wikitext('CandidateParty')
:attr('scope', 'col')
:attr('colspan', 2)
:attr('rowspan', (rounds > 1) and 2 or nilrowspan)
cols = cols + 2
end
if rounds > 1 then
row
:tag('th')
:wikitext('PartyFirst round')
:attr('rowspanscope', (rounds > 1) and 2 or nil'col')
:attr('colspan', 2)
if (rounds > 1) then
row:tag('th')
:tagwikitext('thSecond round')
:wikitextattr('Firstscope', round'col')
:attr('colspan', 2)
secondrow = root:tag('thtr')
:wikitext('Second round')
:attr('colspan', 2)
row = root:tag('tr')
end
for k=1,rounds do
row
:tag('th')
:wikitext('Votes')
:tag('th')
:wikitext('%')
end
elseif sc then
row = root:tag('tr')
if (rounds > 1) then
row
:tag('th')
:wikitext('Party')
:attr('colspan', 2)
:attr('rowspan', 2)
:tag('th')
:wikitext('First round')
:attr('colspan', 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')
for k=1,rounds do
row
:tag('th')
:wikitext('Votes')
:tag('th')
:wikitext('%')
end
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
secondrow = row
row = root:tag('tr')
if (rounds > 1) then
row
:tag('th')
:wikitext('Party')
:attr('colspan', 2)
:attr('rowspan', 2)
:tag('th')
:wikitext('First round')
:attr('colspan', 2)
:tag('th')
:wikitext('Second round')
:attr('colspan', 2)
:tag('th')
:wikitext('Seats')
:attr('rowspan', 2)
row = root:tag('tr')
for k=1,rounds do
row
:tag('th')
:wikitext('Votes')
:tag('th')
:wikitext('%')
end
else
row
:tag('th')
:wikitext('Party')
:attr('colspan', 2)
:tag('th')
:wikitext('Votes')
:tag('th')
:wikitext('%')
:tag('th')
:wikitext('Seats')
end
end
for k=1,rounds do
local lang = mw.getContentLanguage()
secondrow
local function fmt(n)
:tag('th')
return n and lang:formatNum(n) or nil
:wikitext('Votes')
:attr('scope', 'col')
:tag('th')
:wikitext('%')
:attr('scope', 'col')
cols = cols + 2
end
if headings['seats'] then
row
:tag('th')
:wikitext('Seats')
:attr('scope', 'col')
:attr('rowspan', rowspan)
cols = cols + 1
end
if headings['sc'] then
row
:tag('th')
:wikitext(headings['seats'] and '+/–' or 'Seats±')
:attr('scope', 'col')
:attr('rowspan', rowspan)
cols = cols + 1
end
local rsuff = (rounds > 1) and {'', '_2'} or {''}
for i, v in ipairs(index) do
row = root:tag('tr')
 
local color = args['colour' .. v] or args['color' .. v]
-- determine the color
or (mw.title.new('Template:' .. args['party' .. v] .. '/meta/color').exists
andlocal frame:expandTemplate{titlecolor = args['partycolour' .. v] ..or args['/meta/color'}) .. v] or nil
orif color == nil then
local party = unlink(args['party' .. v]) or ''
if party == '' and mw.title.new('Template:' .. party .. '/meta/color').exists then
color = frame:expandTemplate{title = party .. '/meta/color'}
end
end
-- create the empty color cell
row
:tag('td')
:css('background-color', color)
 
if vp then
-- add the rest of the row
if headings['cand'] then
row
:tag('td')
:wikitext(string.format('\[\[%s\]\]', args['cand' .. v]))
if headings['vp'] then
:tag('td')
:wikitext(string.format('\[\[%s\]\]', args['vp' .. v]))
if args['party_name' .. v] then
row
:tag('td')
:wikitext(string.format('\[\[%s\|%s\]\]', args['party' .. v], args['party_namevp' .. v]))
else
row
:tag('td')
:wikitext(string.format('\[\[%s\]\]', args['party' .. v]))
end
end
for kk, suf in ipairs(rsuff) do
if(args headings['votesparty' .. v .. suf]) then
row:tag('td')
:css('text-align', 'right')
:wikitext(fmt(args['votes' .. v .. suf]))
row:tag('td')
:css('text-align', 'right')
:wikitext(string.format('%.2f', args['votes' .. v .. suf] / valid[kk] * 100))
else
row:tag('td'):attr('colspan', 2)
end
end
elseif cand then
row
:tag('td')
:wikitext(string.format('\[\[%s\]\]', args['candparty' .. v]))
rowend
for kk, suf in ipairs(rsuff) do
:tag('td')
:wikitextif(args['party_namevotes' .. v .. suf]) andthen
row:tag('td')
string.format('\[\[%s\|%s\]\]', args['party' .. v], args['party_name' .. v]) or
string.format:css('\[\[%s\]\]text-align', args['partyright' .. v]) or nil)
:wikitext(fmt(args['votes' .. v .. suf]))
for kk, suf in ipairs(rsuff) do
row:tag('td')
if(args['votes' .. v .. suf]) then
row:tagcss('tdtext-align', 'right')
:wikitext(pct(args['votes' .. v .. suf], valid[kk]))
:css('text-align', 'right')
else
:wikitext(fmt(args['votes' .. v .. suf]))
row:tag('td'):attr('colspan', 2)
:css('text-align', 'right')
:wikitext(string.format('%.2f', args['votes' .. v .. suf] / valid[kk] * 100))
else
row:tag('td'):attr('colspan', 2)
end
end
elseif seats then
row
:tag('td')
:wikitext(args['party_name' .. v] and
string.format('\[\[%s\|%s\]\]', args['party' .. v], args['party_name' .. v]) or
string.format('\[\[%s\]\]', args['party' .. v]) or nil)
for kk, suf in ipairs(rsuff) do
if(args['votes' .. v .. suf]) then
row:tag('td')
:css('text-align', 'right')
:wikitext(fmt(args['votes' .. v .. suf]))
row:tag('td')
:css('text-align', 'right')
:wikitext(string.format('%.2f', args['votes' .. v .. suf] / valid[kk] * 100))
else
row:tag('td'):attr('colspan', 2)
end
end
end
if headings['seats'] then
row
:tag('td')
:css('text-align', 'right')
:wikitext(fmt(args['seats' .. v]))
end
if headings['sc'] then
row
:tag('td')
:css('text-align', 'right')
:wikitext(args['sc' .. v])
end
end
row = root
Line 291 ⟶ 220:
:tag('td')
:wikitext('Valid votes')
:attr('colspan', cols - 2*rounds)
:css('text-align', 'left')
for k=1,rounds do
Line 298 ⟶ 227:
:wikitext(fmt(valid[k]))
:tag('td')
:wikitext(string.formatpct('%.2f', valid[k], / (valid[k] + invalid[k]) * 100))
end
row = root:tag('tr')
Line 307 ⟶ 236:
:tag('td')
:wikitext('Invalid votes')
:attr('colspan', cols - 2*rounds)
:css('text-align', 'left')
for k=1,rounds do
Line 314 ⟶ 243:
:wikitext(fmt(invalid[k]))
:tag('td')
:wikitext(string.formatpct('%.2f', invalid[k], / (valid[k] + invalid[k]) * 100))
end
row = root:tag('tr')
Line 324 ⟶ 253:
:tag('td')
:wikitext('Total votes')
:attr('colspan', cols - 2*rounds)
:css('text-align', 'left')
for k=1,rounds do
Line 331 ⟶ 260:
:wikitext(fmt(valid[k] + invalid[k]))
:tag('td')
:wikitext('100.00'pct(1,1))
end
Line 340 ⟶ 269:
row:tag('td')
:wikitext('Registered voters/turnout')
:attr('colspan', cols - 2*rounds)
:css('text-align', 'left')
for k=1,rounds do
Line 346 ⟶ 275:
:wikitext(fmt(electorate[k]))
row:tag('td')
:wikitext(string.format('%.2f', pct(valid[k] + invalid[k]) /, electorate[k] * 100))
end
row = root:tag('tr')
Line 354 ⟶ 283:
row:tag('td')
:wikitext('Source: ', args.source)
:attr('colspan', cols + 2*(rounds-1))
:css('text-align', 'left')
return tostring(root) .. tracking
end