Module:Infobox election/sandbox: Difference between revisions

Content deleted Content added
not needed
allow named colors to be used
 
(21 intermediate revisions by 3 users not shown)
Line 1:
local p = {}
local args = {}
local origArgs = {}
 
local InfoboxImage = require('Module:InfoboxImage').InfoboxImage
Line 7 ⟶ 5:
local fetch = require('Module:Political party')._fetch
-- Helper functions --
local notblank = function (v) return (mw.text.trim(v or "") ~= "") end
local ifblank = function (v, alt1, alt2) return notblank(v) and v or (notblank(alt1) and alt1 or alt2) 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 _rowexport(framefunc, rargsframe)
-- 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 lowercase and intentionally missing the training 's'
local names = {
independent = '[[Independent politician|' .. name .. ']]',
miscellaneou = '[[Independent politician|DIV]]', -- the 's' is intentionally left off
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(aoutput, { __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
Line 22 ⟶ 68:
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" |' .. (titleifblank(rargs[cellType or.. '_label'], title, ucfirst(cellType or ''))))
output(centeredCell(cellType .. '1'))
output(centeredCell(cellType .. '2'))
Line 37 ⟶ 83:
end
output('|- class="sr-only"')
if rargs.not ongoing ~= "yes" and (
mw.ustring.lower(rargs.type)rtype == "parliamentary" or mw.ustring.lower(rargs.type)rtype == "legislative"
) and (
notblank(rargs.party1) or notblank(rargs.party2) or notblank(rargs.party2)
Line 58 ⟶ 104:
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
Line 64 ⟶ 110:
end
if notblank(rargs['color' .. i]) then
image[i]local color = image[i] .. 'border-bottom: 6px solid #' .. mw.ustringtext.gsubtrim(mw.ustringtext.gsubdecode(rargs['color' .. i],'#',''),'#','')
elseif if rargs.party_color(#color ~== 'no'3 or #color == 6) and notblankstring.match(rargscolor, "^['party'0-9a-fA-F] .. i*$") then
color = '#' .. color
local color = mw.ustring.replace(rargs['party' .. i], '^(%[%[%s*[^%]%[]*%]%]).*$', '%1')
end
image[i] = image[i] .. 'border-bottom: 6px solid ' .. color
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
Line 77 ⟶ 127:
image = rargs['image' .. i] or '',
size = notblank(rargs['image' .. i .. '_size']) and rargs['image' .. i .. '_size'] or (rargs.image_size or ''),
alt = ragsrargs['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'),
}
} )
Line 90 ⟶ 140:
-- 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})[mw.ustring.lower(rargs.type)rtype] == 1 then
threeCells('nominee', '', true) -- nominee, no alternative title, always show
end
Line 97 ⟶ 147:
end
-- Party leader --
if mw.ustring.lower(rargs.type)rtype == 'parliamentary' or mw.ustring.lower(rargs.type)rtype == 'legislative' then
threeCells('leader')
end
-- Party name --
threeCells('party', ifblank(notblank(rargs.party_label) and rargs.party_label or, 'Party'), false, true) -- party, alternative name, don't always show, use shortname
-- Alliance name --
threeCells('alliance', '', false, true) -- alliance, notno alternative title, don't always show, use shortname
-- Primary or presidential
if mw.ustring.lower(rargs.type)rtype == 'primary' or mw.ustring.lower(rargs.type)rtype == 'presidential' then
-- Home state --
threeCells('home_state')
-- Running mate --
if mw.ustring.lower(rargs.type)rtype == 'presidential' then
threeCells('running_mate')
-- Electoral vote --
if not ongoing then
threeCells('electoral_vote', (rargs.college_votes == 'no' and 'Projected e' or 'E') .. 'lectoral vote')
threeCells('electoral_vote', (rargs.college_voted == 'no' and 'Projected e' or 'E') .. 'lectoral vote')
end
end
-- Delegate count --
threeCells('delegate_count', (rargs.ongoings == 'yes'ongoing and 'Estimated d' or 'D') .. 'elegate count')
-- StatesPrimary carriedonly --
if rtype == 'primary' then
threeCells('states_carried', mw.ustring.lower(rargs.type) == 'primary' and 'Contests won' or 'States carried')
-- Contests won
threeCells('states_carried', 'Contests won')
-- Presidential not ongoing --
elseif not ongoing then
-- States carried --
threeCells('states_carried', 'States carried')
end
end
-- Parliamentary or legistlativelegislative --
if mw.ustring.lower(rargs.type)rtype == 'parliamentary' or mw.ustring.lower(rargs.type)rtype == 'legislative' then
-- Leader since --
threeCells('leader_since')
Line 128 ⟶ 187:
threeCells('last_election')
-- Seats before --
threeCells('seats_before', rargs.ongoing == 'yes' and 'Current seats' or 'Seats before')
-- Ongoing --
if rargs.ongoing == 'yes' then
-- Seats needed --
threeCells('seats_needed')
Line 144 ⟶ 203:
end
-- Primary or not ongoing --
if rargs.not ongoing ~= 'yes' or rargs.typertype == 'primary' then
-- Popular vote --
threeCells('popular_vote', (rargs.vote_type or 'Popular') .. ' vote')
-- Percentage --
threeCells('percentage')
-- PrimaryFirst orround presidential--
if mw.ustring.lower(rargs.type)rtype == 'primary' or mw.ustring.lower(rargs.type) == 'presidential' 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_threshholdcounties_threshold', 'Counties with 25% vote')
end
end
-- Swing --
if (mw.ustring.lower(rargs.type)rtype == 'parliamentary' or mw.ustring.lower(rargs.type)rtype == 'legislative') and rargs.not ongoing ~= 'yes' then
threeCells('swing')
end
Line 167 ⟶ 232:
end
end
return table.concat(output, '\n')
end
 
p.row = function (frame) return export('_row', frame) end
function p._shortname(link, 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
 
return p
function p.infobox(frame)
for k,v in frame:getParent().args do
if notblank(v) then origArgs[k] = mw.text.trim(v) end
end
for k,v in frame.args do
if notblank(v) then origArgs[k] = mw.text.trim(v) end
end
local width = math.max(
(origArgs.map_image or origArgs.map) and (mw.ustring.match((origArgs.map_size or 300),"(%d*)")) or 0,
(origArgs.map2_image or origArgs.map2) and (mw.ustring.match((origArgs.map2_size or 300),"(%d*)")) or 0,
(origArgs.map3_image or origArgs.map3) and (mw.ustring.match((origArgs.map3_size or 300),"(%d*)")) or 0,
(origArgs.map4_image or origArgs.map4) and (mw.ustring.match((origArgs.map4_size or 300),"(%d*)")) or 0
)
local pagetitle = frame:getParent() and frame:getParent():getTitle() or frame:getTitle()
args = {
child = origArgs.child or origArgs.embed,
bodyclass = origArgs.election_date and origArgs.vevent or nil,
bodystyle = "line-height:1.5em;" .. ((width > 0) and ("width:"..width.."px;") or ""),
titleclass = "summary",
titlestyle = "font-size:125%;",
title = ((origArgs.child or origArgs.embed) == yes)
and (origArgs.election_name and '<div style="background-color:#ddd; font-size:120%; font-weight:bold">'..origArgs.election_name..'</div>')
or (origArgs.election_name or (frame:getParent() and frame:getParent():getTitle() or frame:getTitle())),
datastyle = "text-align:center;",
labelstyle = "width:5em;white-space:nowrap;",
subheaderstyle = "line-height:130%;",
subheader1 = origArgs.flag_image
and (InfoboxImage(
frame:newChild{ title = frame:getTitle(), args = {
image = origArgs.flag_image,
size = "50px",
alt = "",
link = ""
} }
) .. "<hr />")
or (require('Module:CountryData').gettable(mw.getCurrentFrame(),origArgs.country).alias and (require('Module:flag').deco(
frame:newChild{ title = frame:getTitle(), args = {
[1] = origArgs.country,
variant = origArgs.flag_year,
size = "50px"
} }
) .. "<hr />") ),
}
return require('Module:Infobox').infoboxTemplate(
frame:newChild{ title = frame:getTitle(), args = args }
)
end