Module:Canadian federal election results
Appearance
| This module is rated as beta. It is considered ready for widespread use, but as it is still relatively new, it should be applied with some caution to ensure results are as expected. |
| This module depends on the following other modules: |
| This module uses TemplateStyles: |
This module implements Template:Canadian federal election results.
require('strict')
local p = {}
local templatestyles = 'Module:Canadian federal election results/styles.css'
local tracking, preview
local rows = {}
local rowcount = 0
local function checkarg(k,v)
if not k or type(k) ~= 'string' then return end
if k:find('^party[1-9]%d*[a-e]$') then
-- valid and not tracked
if k:find('^party[1-9]%d*a$') then
local n = mw.ustring.gsub(k, '^party([1-9]%d*)[a-e]', '%1')
table.insert(rows, tonumber(n))
rowcount = rowcount + 1
end
elseif k == 'title' or k:find('^year[1-9]%d*$') or k:find('^seats[1-9]%d*[a-e]') then
-- valid and not tracked
else
-- invalid
local vlen = mw.ustring.len(k)
k = mw.ustring.sub(k, 1, (vlen < 25) and vlen or 25)
k = mw.ustring.gsub(k, '[^%w\-_ ]', '?')
table.insert(tracking, '[[Category:Pages using canadian federal election results with unknown parameters|' .. k .. ']]')
table.insert(preview, '"' .. k .. '"')
end
end
local function makecell(frame, p, s)
local res = ''
for i = 1,#p do
if p[i] ~= '' then
local c = frame:expandTemplate{
title = 'Canadian party colour',
args = {'CA', p[i]}
}
res = res .. frame:expandTemplate{
title = 'composition histogram',
args = {c, s[i], title = p[i]}
}
end
end
return res
end
function p.main(frame)
local args = require('Module:Arguments').getArgs(frame)
tracking, preview = {}, {}
for k, v in pairs(args) do
if v ~= '' then
checkarg(k,v)
end
end
table.sort(rows)
local root = mw.html.create('table')
:addClass('ca-fed-elect-results wikitable floatright')
root:tag('caption'):wikitext(args['title'] or 'Electoral history')
local row = root:tag('tr')
row:tag('th'):attr('scope','col'):wikitext('Year')
row:tag('th'):attr('scope','col'):wikitext('Results')
for j = 1, rowcount do
local k = rows[j]
if args['party' .. k .. 'a'] then
row = root:tag('tr')
row:tag('th'):attr('scope', 'row'):wikitext(args['year' .. k] or '?')
row:tag('td'):wikitext(makecell(frame,
{args['party' .. k .. 'a'] or '',
args['party' .. k .. 'b'] or '',
args['party' .. k .. 'c'] or '',
args['party' .. k .. 'd'] or '',
args['party' .. k .. 'e'] or ''},
{args['seats' .. k .. 'a'] or '0',
args['seats' .. k .. 'b'] or '0',
args['seats' .. k .. 'c'] or '0',
args['seats' .. k .. 'd'] or '0',
args['seats' .. k .. 'e'] or '0'}))
end
end
local trackstr = (#tracking > 0) and table.concat(tracking, '') or ''
if #preview > 0 then
trackstr = require('Module:If preview')._warning({
'Unknown parameters: ' .. table.concat(preview, '; ')
})
end
return frame:extensionTag{
name = 'templatestyles', args = { src = templatestyles}
} .. tostring(root) .. trackstr
end
return p