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 17:15, 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
	mw.logObject(args)
	local root = mw.html.create()
	local columns = '4'
	
	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 '')
				)
		:tag('tr')
			:tag('th')
				:wikitext('Office')
			:tag('th')
				:wikitext('Name')
			:tag('th')
				:wikitext('Party')
			:tag('th')
				:wikitext('Term')
		: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)
			table.insert(officeNums, n)
			if subRows[n] == nil then subRows[n] = {} end
			table.insert(subRows[n], l)
		end
	end
	table.sort(officeNums)
	-- remove duplicates
	for k = 2,#officeNums do
		if officeNums[k] == officeNums[k-1] then
			table.remove(officeNums, k)
		end
	end
	mw.logObject(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])
			row:tag('td')
				:wikitext(args['party'..num..l])
			row:tag('td')
				:wikitext(args['term'..num..l])
			subrow = subrow + 1
		end
	end

	return tostring(root)
end
return p