Module:Sandbox/BrandonXLF/3

This is an old revision of this page, as edited by BrandonXLF (talk | contribs) at 03:48, 3 April 2024. 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 yesno = require('Module:Yesno')
local p = {}

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

local function getTimespan(timespan)
	return ' (' .. timespan .. ')'
end

local function getImage(image)
	local file = null

	if string.match(image, '.-%.') then
		file = mw.getCurrentFrame():expandTemplate{title = 'flagicon image', args = {image}}
	elseif string.match(image, '.- %(.-%)') then
		local country, var = string.match(image,'(.-) %((.-)%)')
		file = mw.getCurrentFrame():expandTemplate{title = 'flagdeco', args = {country, var, noredlink = 'y'}}
	else
		file = mw.getCurrentFrame():expandTemplate{title = 'flagdeco', args = {image, noredlink = 'y'}}
	end
	
	return file
end

function p.main(frame)
	local args = getArgs(frame)
	local hasFlags = false
	local entries = {}
	local i = 1
	local knownKeys = {
		[''] = true,
		['date'] = true,
		flag = true
	}

	for i, entry in ipairs(args) do
		local country
		local timespan
		local flag
		local subArgs = {}
		local data = {}
		
		for line in entry:gmatch('[^\n]+') do
			line = mw.text.trim(line)
			
			local key, val = line:match('^([a-z]*):? *(.*)$')
			
			if knownKeys[key] then
				subArgs[key] = val
			else
				return require('Module:Error').error{'Invalid key ' .. key .. ' in argument ' .. i}
			end
		end
		
		if subArgs[''] then
			data[''] = getCountry(subArgs[''])
		else
			return require('Module:Error').error{'A country is required in argument ' .. i}
		end
		
		if subArgs['date'] then
			data['date'] = getTimespan(subArgs['date'])
		else
			return require('Module:Error').error{'A date is required in argument ' .. i}
		end
	
		if subArgs.flag then
			hasFlags = true
		end
		
		local noFlag = yesno(subArgs['flag']) == false
		
		data['flag'] = getImage(
			noFlag
				and ''
				or (subArgs['flag'] == '' or subArgs['flag'] == nil)
					and subArgs['']
					or subArgs['flag'])

		-- Add placeholder image
		if data['flag'] == '' then
			data['flag'] = getImage('')
		end
		
		table.insert(entries, data)
	end
	
	local container = mw.html.create('div')
		:addClass('historical-affiliation')
		:css('float', args.float or 'left')
	
	local titleId = math.random()

	container:tag('div')
		:attr('id', titleId)
		:addClass('historical-affiliation-title')
		:wikitext(args.title and args.title or 'Historical affiliations')
		
	local list = container:tag('ul')
		:attr('aria-labelledby', titleId)
		:addClass('historical-affiliation-list')

	for i, entry in ipairs(entries) do
		local item = list:tag('li');
		
		item:tag('span')
			:wikitext(entry.flag)
				
		item:tag('span')
			:wikitext(entry[''] .. entry['date'])
	end

	return tostring(container) .. frame:extensionTag{
		name = 'templatestyles',
		args = { src = 'User:BrandonXLF/J/styles.css' }
	}
end

return p