![]() | This is the module sandbox page for Module:Infobox election (diff). |
Implements {{Infobox election/row}} and {{Infobox election/shortname}}with a reduced post-expand include size
{{#invoke:Infobox election|row}}
{{#invoke:Infobox election|shortname}}
local p = {}
local InfoboxImage = require('Module:InfoboxImage').InfoboxImage
local delink = require('Module:Delink')._delink
local fetch = require('Module:Political party')._fetch
-- Helper functions --
local notblank = function (v) return (mw.text.trim(v or "") ~= "") end
local ifblank = function (v, a) return notblank(v) and v or a end
local ucfirst = function (s) return mw.ustring.upper(mw.ustring.sub(s,1,1)) .. mw.ustring.gsub(mw.ustring.sub(s,2), '_', ' ') end
local function export(func, frame)
-- Read args from input --
local args = {}
local frameArgs = false
for k,v in pairs(frame.args) do
if notblank(v) then
args[k] = mw.text.trim(v)
frameArgs = true
end
end
if not frameArgs then
for k,v in pairs(frame:getParent().args) do
if notblank(v) then args[k] = mw.text.trim(v) end
end
end
return p[func](args, frame)
end
-- Template:Infobox election/shortname --
function p._shortname(sargs)
local link = sargs.link
local name = sargs.name
if link ~= 'no' and notblank(name) and delink({[1] = name, wikilinks = 'target'}) == name then
-- the below array items are intentionally missing the training 's'
local names = {
independent = '[[Independent politician|' .. name .. ']]',
miscellaneous = '[[Independent politician|DIV]]',
other = 'Others',
['other parties and independent'] = 'Others & [[Independent politician|IND]]',
regionalist = '[[Regionalism (politics)|Regionalists]]',
tbc = '[[To be announced|TBC]]',
tbd = '[[To be announced|TBD]]',
vacant = '[[Casual vacancy|Vacant]]'
}
return names[mw.ustring.gsub(mw.ustring.lower(name), 's$', '')] or
('[[' .. name .. '|' .. fetch({[1] = name, [2] = 'shortname'}) .. ']]')
end
return name or ''
end
p.shortname = function (frame) return export('_shortname', frame) end
-- Template:Infobox election/row --
function p._row(rargs, frame)
local rtype = mw.ustring.lower(rargs.type or '')
local ongoing = rargs.ongoing == 'yes'
local output = {'|-'}
setmetatable(output, {__call = function (t, v) t[#t+1] = v end }) -- Append to array by calling it
local function threeCells(cellType, title, always, shortname)
local centeredCell = function (key)
local name = shortname and p._shortname({link = rargs[key .. '_name'], name = rargs[key]}) or (rargs[key] or '')
return ('| style="' .. (notblank(rargs[key]) and 'width:75px;' or '') .. 'text-align:center" | ' .. name )
end
if always or ( notblank(rargs[cellType .. '1']) or notblank(rargs[cellType .. '2']) or notblank(rargs[cellType .. '3']) ) then
output('|-')
output('! style="width:75px;text-align:left" |' .. (ifblank(title, ucfirst(cellType or ''))))
output(centeredCell(cellType .. '1'))
output(centeredCell(cellType .. '2'))
if always or notblank(rargs.col3) then
output(centeredCell(cellType .. '3'))
end
end
end
if notblank(rargs.divider) and (
notblank(rargs.party1) or notblank(rargs.color1) or notblank(rargs.leader1) or notblank(rargs.party2) or notblank(rargs.color2) or notblank(rargs.leader2) or notblank(rargs.party3) or notblank(rargs.color3) or notblank(rargs.leader3)
) then
output('| colspan=4 | ' .. rargs.divider)
end
output('|- class="sr-only"')
if not ongoing and (
rtype == "parliamentary" or rtype == "legislative"
) and (
notblank(rargs.party1) or notblank(rargs.party2) or notblank(rargs.party2)
) then
output('! style="text-align:left;" | ')
output('! style="text-align:center" | ' .. (notblank(rargs.party1) and rargs.header1 or ''))
output('! style="text-align:center" | ' .. (notblank(rargs.party2) and rargs.header2 or ''))
if notblank(rargs.col3) then
output('! style="text-align:center" | ' .. (notblank(rargs.party3) and rargs.header3 or ''))
end
end
-- Images --
output('|-')
if notblank(rargs.party1) or notblank(rargs.color1) or notblank(rargs.image1) or notblank(rargs.party2) or notblank(rargs.color2) or notblank(rargs.image2) or notblank(rargs.party3) or notblank(rargs.color3) or notblank(rargs.image3) then
if notblank(rargs.filler) then
output('! <br />' .. rargs.filler)
else
output('| ')
end
local image = {}
for i = 1, notblank(rargs['col3']) and 3 or 2, 1 do
image[i] = '| class="notheme" style="text-align:center;'
if not notblank(rargs['party' .. i]) and not notblank(rargs['color' .. i]) then
image[i] = image[i] .. 'width:0;'
end
if notblank(rargs['color' .. i]) then
image[i] = image[i] .. 'border-bottom: 6px solid #' .. mw.ustring.gsub(mw.ustring.gsub(rargs['color' .. i],'#',''),'#','')
elseif rargs.party_color ~= 'no' and notblank(rargs['party' .. i]) then
local color = mw.ustring.gsub(rargs['party' .. i], '^(%[%[%s*[^%]%[]*%]%]).*$', '%1')
color = delink({[1] = color, wikilinks = 'target'})
color = fetch({[1] = color, [2] = 'color', error = 'NOTHERE'})
if color ~= 'NOTHERE' then
image[i] = image[i] .. 'border-bottom: 6px solid' .. color
end
end
image[i] = image[i] .. ';" | ' .. InfoboxImage( frame:newChild{
title = frame:getTitle(), args = {
image = rargs['image' .. i] or '',
size = notblank(rargs['image' .. i .. '_size']) and rargs['image' .. i .. '_size'] or (rargs.image_size or ''),
alt = rargs['alt' .. i] or '',
upright = notblank(rargs['image' .. i .. '_upright']) and rargs['image' .. i .. '_upright'] or (
(notblank(rargs['col3']) and notblank(rargs['image3'])) and '0.4' or '0.6'
),
suppressplaceholder = 'yes',
class = notblank(rargs['pageimage' .. i]) and '' or 'notpageimage noresize',
}
} )
output(image[i])
end
end
-- Presidential & by-election nominee --
if notblank(rargs['nominee1']) or notblank(rargs['nominee2']) or notblank(rargs['nominee3']) then
if ({primary = 1, presidential = 1, ['by-election'] = 1})[rtype] == 1 then
threeCells('nominee', '', true) -- nominee, no alternative title, always show
end
else
threeCells('candidate') -- nominee
end
-- Party leader --
if rtype == 'parliamentary' or rtype == 'legislative' then
threeCells('leader')
end
-- Party name --
threeCells('party', ifblank(rargs.party_label, 'Party'), false, true) -- party, alternative name, don't always show, use shortname
-- Alliance name --
threeCells('alliance', '', false, true) -- alliance, no alternative title, don't always show, use shortname
-- Primary or presidential
if rtype == 'primary' or rtype == 'presidential' then
-- Home state --
threeCells('home_state')
-- Running mate --
if rtype == 'presidential' then
threeCells('running_mate')
-- Electoral vote --
if not ongoing then
threeCells('electoral_vote', (rargs.college_voted == 'no' and 'Projected e' or 'E') .. 'lectoral vote')
end
end
-- Delegate count --
threeCells('delegate_count', (ongoing and 'Estimated d' or 'D') .. 'elegate count')
-- Primary only --
if rtype == 'primary' then
-- Contests won
threeCells('states_carried', 'Contests won')
-- Presidential not ongoing --
elseif not ongoing then
-- States carried --
threeCells('states_carried')
end
end
-- Parliamentary or legislative --
if rtype == 'parliamentary' or rtype == 'legislative' then
-- Leader since --
threeCells('leader_since')
-- Leader's seat --
threeCells('leaders_seat', "Leader's seat")
-- Last election --
threeCells('last_election')
-- Seats before --
threeCells('seats_before', ongoing and 'Current seats' or 'Seats before')
-- Ongoing --
if ongoing then
-- Seats needed --
threeCells('seats_needed')
-- Not ongoing --
else
-- Seats won --
threeCells('seats', 'Seats won')
-- Seats after --
threeCells('seats_after')
-- Seat change --
threeCells('seat_change')
end
end
-- Primary or not ongoing --
if not ongoing or rtype == 'primary' then
-- Popular vote --
threeCells('popular_vote', (rargs.vote_type or 'Popular') .. ' vote')
-- Percentage --
threeCells('percentage')
-- First round --
if rtype == 'primary' then
threeCells('first_round')
end
-- Final round --
threeCells('final_round')
-- Primary or presidential --
if rtype == 'primary' or rtype == 'presidential' then
-- Counties won --
threeCells('counties_won')
-- Counties 25% threshold --
threeCells('counties_threshold', 'Counties with 25% vote')
end
end
-- Swing --
if (rtype == 'parliamentary' or rtype == 'legislative') and not ongoing then
threeCells('swing')
end
-- Blank fields --
for i = 1,6,1 do
if notblank(rargs[i .. 'blank']) then
threeCells(i ..'data', rargs[i .. 'blank'])
end
end
return table.concat(output, '\n')
end
p.row = function (frame) return export('_row', frame) end
-- Subset of Template:Succession links --
function p._successionlinks(sargs)
local output = setmetatable({}, {__call = function (t, v) t[#t+1] = v end })-- Append to table by calling it
local dqhm = function (s) return require('Module:MultiReplace').main({s, '%[%[ *([%?-]) *%]%]', '%1', '%[%[ *[%?-] *| *(.-) *%]%]','%1'}) end
if notblank(sargs.center) then
output('<table style="width:100%; margin:1px; display:inline-table;"><tr>')
output('<td style="text-align:left; vertical-align:middle; padding:0 0.5em 0 0;' .. (sargs.leftstyle or '') .. '" class = "noprint">')
output((notblank(sargs.left) and ('← ' .. dqhm(sargs.left or '')) or '') .. '</td>')
output('<td style="text-align:center; vertical-align:middle; padding:0 1px;' .. (sargs.centerstyle or '') .. '">')
output(sargs.center .. '</td>')
output('<td style="text-align:right; vertical-align:middle; padding:0 0 0 0.5em;' .. (sargs.rightstyle or '') .. '" class="noprint">')
output((notblank(sargs.right) and (dqhm(sargs.right or '') .. ' →') or '') .. '</td>')
output('</tr></table>')
elseif notblank(sargs.left) or notblank(sargs.right) then
output('<div style="width:100%">')
if notblank(sargs.left) then
output('<div style="float: left; text-align:left;padding-right:0.5em;' .. (sargs.leftstyle or '') .. ' class="noprint">')
output('← ' .. dqhm(sargs.left or '') .. '</div>')
end
if notblank(sargs.right) then
output('<div style="float: right; text-align:right;padding-left:0.5em;' .. (sargs.rightstyle or '') .. ' class="noprint">')
output(dqhm(sargs.right or '') .. ' →</div>')
end
output('</div>')
end
return table.concat(output, '\n')
end
p.successionlinks = function (frame) return export('_successionlinks', frame) end
-- Template:Infobox election --
function p._infobox(args, frame)
-- Start defining infobox arguments --
local pagetitle = frame:getParent() and frame:getParent():getTitle() or frame:getTitle()
local etype = mw.ustring.lower(args.type or '')
local linkifexists = function(link, title, alttitle)
if notblank(title) and notblank(link) and mw.title.new(link).exists then
return '[[' .. link .. '|' .. title .. ']]'
elseif notblank(alttitle) or notblank(title) then
return ifblank(altitle, title)
end
end
local iargs = {
templatestyles = 'Infobox election/styles.css',
['child templatestyles'] = 'Screen reader-only/styles.css',
child = ifblank(args.child, args.embed or ''),
bodyclass = 'ib-election infobox-table' .. (notblank(args.election_date) and 'vevent' or ''),
bodystyle = '',
titleclass = 'summary',
title = (ifblank(args.child, args.embed or '') == 'yes')
and (notblank(args.election_name) and '<div style="background-color:#ddd; font-size:120%; font-weight:bold">'..args.election_name..'</div>')
or ifblank(args.election_name, pagetitle),
subheader1 = notblank(args.flag_image)
and (InfoboxImage(
frame:newChild{ title = frame:getTitle(), args = {
image = args.flag_image,
size = "50px",
alt = "",
link = ""
} }
) .. "<hr />")
or (require('Module:CountryData').gettable(mw.getCurrentFrame(),args.country).alias and (require('Module:flag').deco(
frame:newChild{ title = frame:getTitle(), args = {
[1] = args.country,
variant = args.flag_year,
size = "50px"
} }
) .. "<hr />") ),
subheader2 = (notblank(args.previous_year) or notblank(args.election_date) or notblank(args.next_year)) and (
_successionlinks({leftstyle = 'width:20%;', centerstyle = 'width:60%;', rightstyle = 'width:20%;',
left = linkifexists(args.previous_election, args.previous_year),
center = notblank(args.election_date) and ("'''" .. args.election_date .. "'''") or nil,
right = linkifexists(args.next_election, args.next_year)
})
) or (
nil
),
subheader3 = (notblank(args.previous_seat_year) or notblank(args.next_seat_year)) and (
_successionlinks({leftstyle = 'width:35%;text-align:center;', rightstyle = 'width:35%;text-align:center;',
left = linkifexists(args.previous_seat_election, args.previous_seat_year),
right = linkifexists(args.next_seat_election, args.next_seat_year)
})
) or (
nil
),
subheader4 = (notblank(args.outgoing_members) or notblank(args.previous_mps) or notblank(args.elected_members) or notblank(args.elected_mps)) and (
_successionlinks({leftstyle = 'width:40%;text-align:center;', rightstyle = 'width:40%;text-align:center;',
left = linkifexists(ifblank(args.outgoing_members, args.previous_mps or ''), 'outgoing members', ifblank(args.outgoing_members, args.previous_mps or '')),
right = linkifexists(ifblank(args.elected_members, args.elected_mps or ''), 'elected members', ifblank(args.elected_members, args.elected_mps or ''))
})
) or (
nil
),
header1 = (etype == 'primary' or etype == 'presidential') and (
notblank(args.votes_for_election) and (
'<hr />' .. args.votes_for_election .. (
notblank(args.needed_votes) and '<br />' .. args.needed_votes .. ' votes needed to win'
)
) or ''
) or (
(etype == 'parliamentary' or etype == 'legislative' or etype == 'by-election') and (
notblank(args.seats_for_election) and (
'<hr />' .. args.seats_for_election .. (
notblank(args.majority_seats) and '<br />' .. args.majority_seats .. ' seats needed for a majority'
)
) or ''
) or ''
),
data2 = notblank(args.opinion_polls) and ('[[' .. args.opinion_polls .. '|Opinion polls]]') or '',
label3 = 'Registered',
data3 = args.registered,
label4 = 'Turnout',
data4 = args.turnout,
label5 = 'Votes counted',
data5 = notblank(args.votes_counted) and (
'<div class="center" style="width:auto;margin-left:auto;margin-right:auto;">' ..
_percentageBar(args.votes_counted, args.votes_counted .. '%', '1BCE0E') ..
"</div>as of '''" .. (args.last_update or '') .. ' ' .. (args.time_zone or '') .. "'''"
) or '',
label6 = 'Reporting',
data6 = notblank(args.reporting) and (
'<div class="center" style="width:auto;margin-left:auto;margin-right:auto;">' ..
_percentageBar(args.reporting, args.reporting .. '%', '1BCE0E') ..
"</div>as of '''" .. (args.last_update or '') .. ' ' .. (args.time_zone or '') .. "'''"
) or '',
label7 = 'Declared',
data7 = notblank(args.declared) and (
'<div class="center" style="width:auto;margin-left:auto;margin-right:auto;">' ..
_percentageBar(args.declared, args.declared .. '%', '1BCE0E') ..
"</div>as of '''" .. (args.last_update or '') .. ' ' .. (args.time_zone or '') .. "'''"
) or '',
}
-- bodystyle --
local width = 0
local map = false
for _, v in ipairs({'', '2', '3', '4'}) do
if notblank(args['map' .. v]) or notblank(args['map' .. v .. '_image']) then
map = true
local nWidth = mw.ustring.gsub(args['map' .. v .. '_size'] or '300', '%s*px%s*$', '')
width = math.max(width, tonumber(nWidth) or 300)
end
end
if map then
iargs.bodystyle = 'width:' .. tostring(math.min(300, width)) .. "px"
end
-- Start {{Infobox election/row}} lines --
local ordinals = {'First', 'Second', 'Third', 'Fourth', 'Fifth', 'Sixth', 'Seventh', 'Eighth', 'Ninth'}
local data10 = {'<nowiki />',
'{| style="background:transparent; color: inherit; width:100%;"',
}
for i = 1,3,1 do
-- Start defining row args that are the same for each row --
local rargs = {
type = args.type,
divider = '<hr />',
ongoing = args.ongoing or 'no',
party_color = ifblank(args.party_color, 'yes'),
image_size = args.image_size or '150x150px',
image_upright = args.image_upright,
party_label = args.party_label or 'Party',
vote_type = args.vote_type or 'Popular',
opinion_polls = args.opinion_polls,
col3 = ''
}
for j = 1,6,1 do
rargs[tostring(j) .. 'blank'] = args[tostring(j) .. 'blank']
end
-- Start defining row args that are different for each row --
local startnum = (3 * (i - 1))
local headernum = startnum
if (i > 1) and (notblank(args.party3)) then
headernum = headernum - 1
end
if (i == 3) and (notblank(args.party6)) then
headernum = headernum - 1
end
-- Define row args that come in sets of 3 --
for j = 1,3,1 do
rargs['header' .. tostring(j)] = ordinals[headernum + j] .. ' party'
for _, v in ipairs({
'image?', 'image?_size', 'image?_upright', 'pageimage?', 'nominee?',
'candidate?', 'leader?', 'party?', 'alliance?', 'home_state?',
'running_mate?', 'college_voted', 'electoral_vote?', 'delegate_count?', 'states_carried?',
'leader_since?', 'leaders_seat?', 'last_election?',
'seats_before?', 'seats_needed?', 'seats?', 'seats_after?', 'seat_change?',
'popular_vote?', 'percentage?', 'swing?',
'1data?', '2data?', '3data?', '4data?', '5data?', '6data?'
}) do
rargs[mw.ustring.gsub(v, '?', tostring(j))] = args[mw.ustring.gsub(v, '?', tostring(j + startnum))]
end
rargs['color' .. tostring(j)] = ifblank(args['color' .. tostring(startnum + j)], args['colour' .. tostring(startnum + j)])
rargs['party' .. tostring(j) .. '_name'] = ifblank(args['party' .. tostring(startnum + j) .. '_name'], args['party_name'])
rargs['alliance' .. tostring(j) .. '_name'] = ifblank(args['alliance' .. tostring(startnum + j) .. '_name'], args['alliance_name'])
end
-- Determine if there is a third column --
for _, v in ipairs({
'color', 'colour', 'image', 'nominee', 'candidate',
'leader', 'party', 'alliance', 'home_state', 'running_mate',
'electoral_vote', 'delegate_count', 'states_carried',
'leader_since', 'leaders_seat', 'last_election',
'seats_before', 'seats_needed', 'seats', 'seats_after', 'seat_change',
'popular_vote', 'percentage', 'swing',
'1data', '2data', '3data', '4data', '5data', '6data'
}) do
if notblank(args[v .. tostring(3 * i)]) then
rargs.col3 = 'yes'
break
end
end
-- Insert data --
table.insert(data10, '|-')
table.insert(data10, _row(rargs, frame))
end
iargs.data10 = table.concat(data10, '\n') .. '|}'
-- Maps --
-- Return infobox --
return require('Module:Infobox').infoboxTemplate(
frame:newChild{ title = frame:getTitle(), args = iargs }
)
end
p.infobox = function (frame) return export('_infobox', frame) end
return p