Module:Infobox election/sandbox: Difference between revisions

Content deleted Content added
Fix "ongoing" tests
re-organize, create export function to reduce duplicate code
Line 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, 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 _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 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'
Line 17 ⟶ 62:
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 187 ⟶ 232:
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
 
-- Template:Infobox election --
function p._successionlinks(sargs)
local output = {}
local dqhm = function (s) return require('Module:MultiReplace').main({s, '%[%[ *([%?-]) *%]%]', '%1', '%[%[ *[%?-] *| *(.-) *%]%]','%1'}) end
if notblank(sargs.center) then
table.insert(output, '<table style="width:100%; margin:1px; display:inline-table;"><tr>')
table.insert(output, '<td style="text-align:left; vertical-align:middle; padding:0 0.5em 0 0;' .. (sargs.leftstyle or '') .. '" class = "noprint">')
table.insert(output, (notblank(sargs.left) and ('&larr;&nbsp;' .. dqhm(sargs.left or '')) or '') .. '</td>')
table.insert(output, '<td style="text-align:center; vertical-align:middle; padding:0 1px;' .. (sargs.centerstyle or '') .. '">')
table.insert(output, sargs.center .. '</td>')
table.insert(output, '<td style="text-align:right; vertical-align:middle; padding:0 0 0 0.5em;' .. (sargs.rightstyle or '') .. '" class="noprint">')
table.insert(output, (notblank(sargs.right) and (dqhm(sargs.right or '') .. '&nbsp;&rarr;') or '') .. '</td>')
table.insert(output, '</tr></table>')
elseif notblank(sargs.left) or notblank(sargs.right) then
table.insert(output, '<div style="width:100%">')
if notblank(sargs.left) then
table.insert(output, '<div style="float: left; text-align:left;padding-right:0.5em;' .. (sargs.leftstyle or '') .. ' class="noprint">')
table.insert(output, '&larr;&nbsp;' .. dqhm(sargs.left or '') .. '</div>')
end
if notblank(sargs.right) then
table.insert(output, '<div style="float: right; text-align:right;padding-left:0.5em;' .. (sargs.rightstyle or '') .. ' class="noprint">')
table.insert(output, dqhm(sargs.right or '') .. '&nbsp;&rarr;</div>')
end
table.insert('</div>')
end
return table.concat(output, '\n')
end
 
function p.row(frame)
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 _row(frame, args)
end
function p.infobox(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
 
function p._infobox(args, frame)
-- Start defining infobox arguments --
local pagetitle = frame:getParent() and frame:getParent():getTitle() or frame:getTitle()
Line 466 ⟶ 435:
-- Insert data --
table.insert(data10, '|-')
table.insert(data10, _row(framerargs, rargsframe))
end
Line 478 ⟶ 447:
)
end
 
p.infobox = function (frame) return export('_infobox', frame) end
 
return p