Jump to content

Module:Infobox cabinet members/sandbox

From Wikipedia, the free encyclopedia
require('strict')
local p = {}
local infobox_image = require('Module:InfoboxImage').InfoboxImage

function p._main(args)

	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
	mw.log(columns)
	if args.topcaption then
		args.topcaption = tostring(
			mw.html.create('div')
				:cssText(args.topcaptionstyle)
				:wikitext(args.topcaption)
		)
	end
	if args.topimage then
		root
			:tag('tr'):tag('td')
				:attr('colspan', columns)
				:addClass('cabinet-members-image')
				:wikitext(infobox_image{
					args = {
						image = args.topimage,
						size = args.topimagesize,
						sizedefault = 'frameless',
						upright = 1,
						alt = args.topimagealt
					}
				} .. (args.topcaption or ''))
	end
	if args.above then
		root
			:tag('tr'):tag('th')
				:attr('colspan', columns)
				:addClass('cabinet-members-above')
				:wikitext(args.above)
	end
	if args.caption then
		args.caption = tostring(
			mw.html.create('div')
				:cssText(args.captionstyle)
				:wikitext(args.caption)
		)
	end
	if args.image then
		root
			:tag('tr'):tag('td')
				:attr('colspan', columns)
				:addClass('cabinet-members-image')
				:wikitext(infobox_image{
					args = {
							image = args.image,
							size = args.imagesize,
							sizedefault = 'frameless',
							upright = 1,
							alt = args.imagealt
					}
				} .. (args.caption or ''))
	end
	
	-- Actual table
	local cabinet = mw.html.create('table')
	local header = cabinet:tag('tr')
	header:tag('th')
		:addClass('cabinet-members-header')
		:wikitext(args.office_label or 'Office')
		:attr('scope', 'col')
	header:tag('th')
		:wikitext(args.name_label or 'Name')
		:attr('scope', 'col')
	if args.party_column then 
		header:tag('th')
			:wikitext(args.party_label or 'Party')
			:attr('scope', 'col')
	end
	header:tag('th')
		:wikitext(args.term_label or 'Term')
		:attr('scope', 'col')

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

		local num, l = k:match('^name(%d+)([a-z])$')
		if num then
			num = tonumber(num)
			if subRows[num] == nil then 
				subRows[num] = {}
				table.insert(keys,num)
			end
			subRows[num][l] = l
		end
	end
	
	table.sort(keys)

	for _, num in ipairs(keys) do
		local row_table = {}
		for _, letter in pairs(subRows[num]) do
			table.insert(row_table, letter)
		end
		table.sort(row_table)
		for j, letter in pairs(row_table) do
			local row = mw.html.create('tr')
			if j == 1 then
				row:addClass('cabinet-members-office')
				local office = row:tag('td'):wikitext(args['office' .. num])
				office:attr('rowspan', (row_table:size() > 1) and row_table:size() or nil)
			end
			row:tag('th')
				:wikitext(args['name'..num..letter])
				:attr('scope', 'row')
			if args.party_column then
				row:tag('td')
					:wikitext(args['party'..num..letter])
			end
			row:tag('td')
				:wikitext(args['term'..num..letter])
			cabinet:node(row)
		end
	end
	
	if args.below then
		root:tag('tr')
			:tag('td')
				:attr('colspan', columns)
				:wikitext(args.below)
	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 base_templatestyles .. templatestyles .. tostring(root)
end

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

return p