Jump to content

Module:Infobox cabinet members and Module:Infobox cabinet members/sandbox: Difference between pages

(Difference between pages)
Page 1
Page 2
Content deleted Content added
use require('strict') instead of require('Module:No globals')
 
get this saved
 
Line 1: Line 1:
require('strict')
require('strict')
local getArgs = require('Module:Arguments').getArgs

local p = {}
local p = {}
local infobox_image = require('Module:InfoboxImage').InfoboxImage
function p.infobox(frame, args)

if not args then
function p._main(args)
args = getArgs(frame)
end


local root = mw.html.create()
local root = mw.html.create()
root = root
:tag('table')
:addClass('infobox')
:addClass('cabinet-members')
:addClass(args.float and ('cabinet-members-' .. args.float:lower()) or 'cabinet-members-right')

local columns = args.party_column and 4 or 3
local columns = args.party_column and 4 or 3
mw.log(columns)
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
if args.topcaption then
args.topcaption = '<br />' .. tostring(
args.topcaption = tostring(
mw.html.create('span')
mw.html.create('div')
:cssText(args.topcaptionstyle)
:cssText(args.topcaptionstyle)
:wikitext(args.topcaption)
:wikitext(args.topcaption)
)
)
end
end
local floatcss = {
left = 'margin-left:0; margin-right:1em; float:left; clear:left;',
center = 'margin-left:auto; margin-right:auto; float:none; clear:none;',
none = 'margin-left:0; margin-right:0; float:none; clear:none;',
right = 'margin-left:1em; margin-right:0; float:right; clear:right;'
}
root = root
:tag('table')
:addClass('infobox')
:css('width', 'auto')
:css('text-align', 'left')
:css('line-height', '1.2em')
:cssText(args.float and floatcss[(args.float):lower()] or floatcss['right'])

if args.topimage then
if args.topimage then
root
root
:tag('tr'):tag('td')
:tag('tr'):tag('td')
:attr('colspan', columns)
:attr('colspan', columns)
:css('text-align', 'center')
:addClass('cabinet-members-image')
:wikitext(require('Module:InfoboxImage').InfoboxImage{args = {
:wikitext(infobox_image{
image = args.topimage,
args = {
image = args.topimage,
size = args.topimagesize,
size = args.topimagesize,
sizedefault = 'frameless',
sizedefault = 'frameless',
upright = 1,
upright = 1,
alt = args.topimagealt
alt = args.topimagealt
}} .. (args.topcaption or '')
}
} .. (args.topcaption or ''))
)
end
end
if args.above then
if args.above then
Line 60: Line 40:
:tag('tr'):tag('th')
:tag('tr'):tag('th')
:attr('colspan', columns)
:attr('colspan', columns)
:css('line-height','1.5em')
:addClass('cabinet-members-above')
:css('font-size','110%')
:css('background','#DCDCDC')
:css('text-align', 'center')
:wikitext(args.above)
:wikitext(args.above)
end
if args.caption then
args.caption = tostring(
mw.html.create('div')
:cssText(args.captionstyle)
:wikitext(args.caption)
)
end
end
if args.image then
if args.image then
Line 70: Line 54:
:tag('tr'):tag('td')
:tag('tr'):tag('td')
:attr('colspan', columns)
:attr('colspan', columns)
:css('text-align', 'center')
:addClass('cabinet-members-image')
:wikitext(require('Module:InfoboxImage').InfoboxImage{args = {
:wikitext(infobox_image{
args = {
image = args.image,
image = args.image,
size = args.imagesize,
size = args.imagesize,
Line 77: Line 62:
upright = 1,
upright = 1,
alt = args.imagealt
alt = args.imagealt
}} .. (args.caption or '')
}
} .. (args.caption or ''))
)
end
end
local header = root:tag('tr')
-- Actual table
local cabinet = mw.html.create('table')
local header = cabinet:tag('tr')
header:tag('th')
header:tag('th')
:addClass('cabinet-members-header')
:wikitext(args.office_label or 'Office')
:wikitext(args.office_label or 'Office')
:attr('scope', 'col')
header:tag('th')
header:tag('th')
:wikitext(args.name_label or 'Name')
:wikitext(args.name_label or 'Name')
:attr('scope', 'col')
if args.party_column then
if args.party_column then
header:tag('th')
header:tag('th')
:wikitext(args.party_label or 'Party')
:wikitext(args.party_label or 'Party')
:attr('scope', 'col')
end
end
header:tag('th')
header:tag('th')
:wikitext(args.term_label or 'Term')
:wikitext(args.term_label or 'Term')
:attr('scope', 'col')
root:tag('tr')
:tag('td')
:attr('colspan', columns)
:css('background', '#000')


local subRows = {}
local subRows = {}
local keys = {}
local keys = {}
for k,v in pairs(args) do
for k, v in pairs(args) do
k = tostring(k)
k = tostring(k)
local num = k:match('^office(%d+)$')
local num = k:match('^office(%d+)$')
if num and args['name' .. num .. 'a'] then
if num and args['name' .. num .. 'a'] then
num = tonumber(num)
num = tonumber(num)
if subRows[num] == nil then
if subRows[num] == nil then
subRows[num] = {}
subRows[num] = {}
table.insert(keys, num)
table.insert(keys, num)
Line 109: Line 98:
end
end


local num,l = k:match('^name(%d+)([a-z])$')
local num, l = k:match('^name(%d+)([a-z])$')
if num then
if num then
num = tonumber(num)
num = tonumber(num)
Line 122: Line 111:
table.sort(keys)
table.sort(keys)


for i, num in ipairs(keys) do
for _, num in ipairs(keys) do
local row_table = {}
if i > 1 then
for _, letter in pairs(subRows[num]) do
root:tag('tr')
table.insert(row_table, letter)
:tag('td')
:attr('colspan',columns)
:css('background','#D1D1D1')
end
end
table.sort(row_table)
local r = {}
for j,l in pairs(subRows[num]) do
for j, letter in pairs(row_table) do
local row = mw.html.create('tr')
table.insert(r,l)
if j == 1 then
end
row:addClass('cabinet-members-office')
table.sort(r)
local row = root:tag('tr')
local office = row:tag('td'):wikitext(args['office' .. num])
office:attr('rowspan', (row_table:size() > 1) and row_table:size() or nil)
local ocell = row:tag('td'):wikitext(args['office' .. num])
local subrow = 0
for j, l in pairs(r) do
subrow = subrow + 1
if subrow > 1 then
row:tag('tr')
end
end
row:tag('th')
row:tag('th')
:wikitext(args['name'..num..letter])
:css('font-weight', 'bold')
:wikitext(args['name'..num..l])
:attr('scope', 'row')
if args.party_column then
if args.party_column then
row:tag('td')
row:tag('td')
:wikitext(args['party'..num..l])
:wikitext(args['party'..num..letter])
end
end
row:tag('td')
row:tag('td')
:wikitext(args['term'..num..l])
:wikitext(args['term'..num..letter])
cabinet:node(row)
end
end
ocell:attr('rowspan', (subrow > 1) and subrow or nil)
end
end
Line 159: Line 141:
:tag('td')
:tag('td')
:attr('colspan', columns)
:attr('colspan', columns)
:css('border-top', '#D1D1D1 2px solid')
:wikitext(args.below)
:wikitext(args.below)
end
end
local frame = mw.getCurrentFrame()
local base_templatestyles = frame:extensionTag{
'templatestyles', args = { src = 'Module:Infobox cabinet members/styles.css' }
}
local templatestyles = ''
if args.templatestyles and mw.text.trim(args.templatestyles) ~= '' then
templatestyles = frame:extensionTag{
'templatestyles', args = { src = 'Module:Infobox cabinet members/styles.css' }
}
end


return tostring(root)
return base_templatestyles .. templatestyles .. tostring(root)
end
end

function main(frame)
local args = require('Module:Arguments').getArgs(frame)
return p._main(args)
end

return p
return p