Difference between revisions of "Module:Infobox cabinet members"
Jump to navigation
Jump to search
test>Zackmann08 (adding overrides for labels) |
test>Zackmann08 (allowing party column to be optional) |
||
| Line 7: | Line 7: | ||
args = getArgs(frame) | args = getArgs(frame) | ||
end | end | ||
| − | + | ||
local root = mw.html.create() | local root = mw.html.create() | ||
| − | local columns = '4' | + | local columns |
| + | if args.party_column then columns = '4' else columns = '3' end | ||
| + | mw.log(columns) | ||
if args.caption then | if args.caption then | ||
| Line 68: | Line 70: | ||
}} .. (args.caption or '') | }} .. (args.caption or '') | ||
) | ) | ||
| − | + | local header = root:tag('tr') | |
| − | + | header:tag('th') | |
| − | + | :wikitext(args.office_label or 'Office') | |
| − | + | header:tag('th') | |
| − | + | :wikitext(args.name_label or 'Name') | |
| − | + | if args.party_column then | |
| − | + | header:tag('th') | |
| − | + | :wikitext(args.party_label or 'Party') | |
| − | + | end | |
| − | + | header:tag('th') | |
| + | :wikitext(args.term_label or 'Term') | ||
| + | root:tag('tr') | ||
:tag('td') | :tag('td') | ||
:attr('colspan', columns) | :attr('colspan', columns) | ||
| Line 101: | Line 105: | ||
table.sort(officeNums) | table.sort(officeNums) | ||
| − | |||
| − | |||
for i, num in ipairs(officeNums) do | for i, num in ipairs(officeNums) do | ||
num = tonumber(num) | num = tonumber(num) | ||
| Line 125: | Line 127: | ||
:css('font-weight', 'bold') | :css('font-weight', 'bold') | ||
:wikitext(args['name'..num..l]) | :wikitext(args['name'..num..l]) | ||
| − | row:tag('td') | + | if args.party_column then |
| − | + | row:tag('td') | |
| + | :wikitext(args['party'..num..l]) | ||
| + | end | ||
row:tag('td') | row:tag('td') | ||
:wikitext(args['term'..num..l]) | :wikitext(args['term'..num..l]) | ||
Revision as of 18:34, 21 September 2018
| 40x40px | This module is rated as ready for general use. It has reached a mature form and is thought to be bug-free and ready for use wherever appropriate. It is ready to mention on help pages and other Wikipedia resources as an option for new users to learn. To reduce server load and bad output, it should be improved by sandbox testing rather than repeated trial-and-error editing. |
Usage
See: Template:Infobox cabinet members
require('Module:No globals')
local getArgs = require('Module:Arguments').getArgs
local p = {}
function p.infobox(frame, args)
if not args then
args = getArgs(frame)
end
local root = mw.html.create()
local columns
if args.party_column then columns = '4' else columns = '3' end
mw.log(columns)
if args.caption then
args.caption = '<br />' .. tostring(
mw.html.create('span')
:cssText(args.captionstyle)
:wikitext(args.caption)
)
end
if args.topcaption then
args.topcaption = '<br />' .. tostring(
mw.html.create('span')
:cssText(args.topcaptionstyle)
:wikitext(args.topcaption)
)
end
root = root
:tag('table')
:addClass('infobox')
:css('width', 'auto')
:css('text-align', 'left')
:css('line-height', '1.2em')
:css('margin-left', '1em')
:css('margin-right', '0em')
:css('float', 'right')
:css('clear', 'right')
root
:tag('tr'):tag('td')
:attr('colspan', columns)
:css('text-align', 'center')
:wikitext(require('Module:InfoboxImage').InfoboxImage{args = {
image = args.topimage,
size = args.topimagesize,
sizedefault = 'frameless',
upright = 1,
alt = args.topimagealt
}} .. (args.topcaption or '')
)
:tag('tr'):tag('th')
:attr('colspan', columns)
:css('line-height','1.5em')
:css('font-size','110%')
:css('background','#DCDCDC')
:css('text-align', 'center')
:wikitext(args['above'])
:tag('tr'):tag('td')
:attr('colspan', columns)
:css('text-align', 'center')
:wikitext(require('Module:InfoboxImage').InfoboxImage{args = {
image = args.image,
size = args.imagesize,
sizedefault = 'frameless',
upright = 1,
alt = args.imagealt
}} .. (args.caption or '')
)
local header = root:tag('tr')
header:tag('th')
:wikitext(args.office_label or 'Office')
header:tag('th')
:wikitext(args.name_label or 'Name')
if args.party_column then
header:tag('th')
:wikitext(args.party_label or 'Party')
end
header:tag('th')
:wikitext(args.term_label or 'Term')
root:tag('tr')
:tag('td')
:attr('colspan', columns)
:css('background', '#000')
local officeNums = {}
local subRows = {}
for k,v in pairs(args) do
k = tostring(k)
local num = k:match('^office(%d+)$')
if num then
num = tonumber(num)
table.insert(officeNums, num)
end
local n,l = k:match('^name(%d+)([a-z])$')
if n then
n = tonumber(n)
if subRows[n] == nil then subRows[n] = {} end
table.insert(subRows[n], l)
end
end
table.sort(officeNums)
for i, num in ipairs(officeNums) do
num = tonumber(num)
if i > 1 then
root:tag('tr')
:tag('td')
:attr('colspan',columns)
:css('background','#D1D1D1')
end
local r = subRows[num] or {}
table.sort(r)
local row = root:tag('tr')
row:tag('td')
:attr('rowspan', r and (#r > 1) and #r or nil)
:wikitext(args['office' .. num])
local subrow = 1
for j, l in ipairs(r) do
if subrow > 1 then
row:tag('tr')
end
row:tag('th')
:css('font-weight', 'bold')
:wikitext(args['name'..num..l])
if args.party_column then
row:tag('td')
:wikitext(args['party'..num..l])
end
row:tag('td')
:wikitext(args['term'..num..l])
subrow = subrow + 1
end
end
if args.below then
root:tag('tr')
:tag('td')
:attr('colspan', columns)
:css('border-top', '#D1D1D1 2px solid')
:wikitext(args.below)
end
return tostring(root)
end
return p