Jump to content

Module:Infobox cabinet members

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Frietjes (talk | contribs) at 18:57, 21 September 2018. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

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')
	if args.float == 'left' then
		root
			:css('margin-left', '0em')
			:css('margin-right', '1em')
			:css('float', 'left')
			:css('clear', 'left')
	elseif args.float == 'center' then
		root
			:css('margin-left', 'auto')
			:css('margin-right', 'auto')
			:css('float', 'none')
			:css('clear', 'none')
	elseif args.float == 'none' then
		root
			:css('margin-left', '0em')
			:css('margin-right', '0em')
			:css('float', 'none')
			:css('clear', 'none')
	else
		root
			:css('margin-left', '1em')
			:css('margin-right', '0em')
			:css('float', 'right')
			:css('clear', 'right')
	end
	
	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 subRows = {}
	local keys = {}
	for k,v in pairs(args) do
		k = tostring(k)
		local num = k:match('^office(%d+)$') 
		if num then
			num = tonumber(num)
			if subRows[num] == nil then 
				subRows[num] = {} 
				table.insert(keys, num)
			end
		end
		local n1,l1,n2,l2,n3,l3 = nil,nil,nil,nil,nil,nil
		local n1,l1 = k:match('^name(%d+)([a-z])$')
		if args.party_column then
			n2,l2 = k:match('^party(%d+)([a-z])$')
		end
		local n3,l3 = k:match('^term(%d+)([a-z])$')
		if n1 or n2 or n3 then
			num = tonumber(n1 or n2 or n3)
			local l = (n1 and l1) or (n2 and l2) or (n3 and l3)
			if args['name' .. num .. l] then
				if subRows[num] == nil then 
					subRows[num] = {}
					table.insert(keys,num)
				end
				subRows[num][l] = l
			end
		end
	end
	
	table.sort(keys)

	for i, num in ipairs(keys) do 
		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')
		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
			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])
		end
		ocell:attr('rowspan', (subrow > 1) and subrow or nil)
	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