Module:Sandbox/BrandonXLF/3

This is an old revision of this page, as edited by BrandonXLF (talk | contribs) at 19:40, 5 October 2021 (Make sure parameters are balanced). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

-- Sandbox, do not delete

local getArgs = require('Module:Arguments').getArgs
local p = {}

function getImage(frame, image)
	if not image then
		return nil
	end

	local file = nil

	if string.match(image, "(.-)%.(.*)") then
		file = frame:expandTemplate{title = "flagicon image", args = {image}}
	elseif string.match(image, ".-%s%(.-%)") then
		local country, var = string.match(image,"(.-)%s%((.-)%)")
		file = frame:expandTemplate{title = "flagdeco", args = {country, var}}
	else
		file = frame:expandTemplate{title = "flagdeco", args = {image}}
	end

	return file
end

function getCountry(country)
	local title = mw.title.new(country)
	
	if not title or not title.exists then
		return country
	end
	
	return '[[' .. country .. ']]'
end

function p.main(frame)
	local args = getArgs(frame)
	local hasImages = false
	local entries = {}
	local i = 1

	while args[i + 2] do
		image = getImage(frame, args[i])
		
		table.insert(entries, {
			image = image,
			country = getCountry(args[i + 1]),
			post = args[i + 2] and ' (' .. args[i + 2] .. ')' or ''
		})
	
		if image then
			hasImages = true
		end
	
		i = i + 3
	end
	
	if args[i] then
		return require('Module:Error').error{'Uneven number of parameters, there should be 3 parameters per entry'}
	end
	
	local outTable = mw.html.create('table')
		:css({
			padding = '5px',
			float = args.float or 'left',
			['background-color'] = '#f8f9fa',
			border = '1px solid #aaa',
			width = '20em',
			['font-size'] = '90%'
		})

	outTable:tag('tr')
		:tag('th')
			:attr('colspan', hasImages and 2 or 1)
			:css({
				['text-align'] = 'center',
				['font-size'] = 'larger',
				['font-weight'] = 'bold'
			})
			:wikitext(args.title and args.title or 'Historical affiliations')

	for i, entry in ipairs(entries) do
		local row = outTable:tag('tr')
		
		if entry.image then
			row:tag('td')
				:css('vertical-align', 'top')
				:wikitext(entry.image)
		elseif hasimages then
			row:tag('td')
		end
				
		row:tag('td')
			:css({
				width = '100%',
				['vertical-align'] = 'top'
			})
			:wikitext(entry.country .. entry.post)
	end

	return tostring(outTable)
end

return p