Module:Sandbox/BrandonXLF/3

This is an old revision of this page, as edited by BrandonXLF (talk | contribs) at 06:46, 14 December 2021. 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 string.match(image, ".-%+.-") then
		k, image =  string.match(image, "(.-)+(.-)")
	end
	
	if true then
		return image
	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)
	if string.match(country, ".-%+.-") then
		country, _ = string.match(country, "(.-)%+(.-)")
	end
	
	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 + 1] do
		image = getImage(frame, args[i])
		
		table.insert(entries, {
			image = image,
			country = getCountry(args[i]),
			post = args[i + 1] and ' (' .. args[i + 1] .. ')' or ''
		})
	
		if image then
			hasImages = true
		end
	
		i = i + 2
	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