Module:Cat color flags

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
Lua

CodeDiscussionEditHistoryLinksLink count Subpages:DocumentationTestsResultsSandboxLive code All modules


Logic for {{Cat color flags}}.

Usage

[edit]
{{#invoke:Cat color flags|cat_color_flags}}


Code

require('strict')

local getArgs = require('Module:Arguments').getArgs
local error_message = require('Module:Error')['error']
local bicolor = require('Module:Bicolor')._bicolor
local cat_see_also = require('Module:Cat see also').main
local intersect_categories = require('Module:Intersect categories')._intersect_categories
local message_box = require('Module:Message box').main

local p = {}

local current_frame = mw.getCurrentFrame()

local function expandTemplate(title, args)
	return current_frame:expandTemplate {title = title, args = args}
end

-- [[Template:ColorSample]]
local function color_sample(color)
	local span = mw.html.create('span')
	span:css({
		['line-height'] = '1.7',
		['padding'] = '0 0.6em',
		['margin-right'] = '-1px',
		['border'] = '1px solid #808080',
		['background'] = color
	})
	span:attr('title', color)
	span:wikitext(' ')
	return tostring(span)
end

-- [[Template:Fmbox]]
local function fmbox(text)
	return message_box('fmbox', {['type'] = 'system', ['text'] = text})
end

local function ucfirst(text)
	if not text or string.len(text) == 0 then
		return text
	elseif string.len(text) == 1 then
		return string.upper(text)
	end
	return string.upper(string.sub(text, 1, 1)) .. string.sub(text, 2)
end

local function color_cat_name(color)
	local names = {
		gold = 'golden'
	}
	return names[color] or color
end

local function error_for_first_n_args(args, n)
	local ordinals = {'first', 'second', 'third', 'fourth', 'fifth'}
	for i = 1, n do
		if not args[i] then
			return error_message({message = '[[Module:Cat color flags]] error: supply ' .. (ordinals[i] or i .. 'th') .. ' argument'})
		end
	end
	return nil
end

function p.cat_1_color_flags(args)
	local arg_check = error_for_first_n_args(args, 1)
	if arg_check then
		return arg_check
	end
	
	local country = args.country
	local country_prefix_text = args.country_prefix_text
	local i18n_of_country = args.i18n_of_country
	local continent = args.continent
	local shadeof = args.shadeof
	
	local countries_of_continent = ''
	if country then
		countries_of_continent = expandTemplate(
			'countries of ' .. continent,
			{prefix = ':Category:' .. args[1] .. ' flags of'}
		)
	end
	
	local color_display = ''
	if args[1] == 'bicolor' or args[1] == 'tricolor' or args[1] == 'four-color' then
		local topic = 'flags'
		if country then
			topic = topic .. ' of ' .. country_prefix_text
		end
		if args[1] == 'biclor' then
			color_display = bicolor({topic = topic})
		elseif args[1] == 'tricolor' then
			color_display = expandTemplate(args[1], {topic = topic})
		else
			color_display = '' -- four-color is just a placeholder ATM
		end
	else
		local by_color_args = {}
		by_color_args.category_i18n = expandTemplate('i18n/Flags', {})
		by_color_args[1] = 'Flags'
		by_color_args[2] = 'flags'
		if country then
			by_color_args.category_i18n = by_color_args.category_i18n .. expandTemplate('i18n/of place', {[1] = country, nolink = '1', plural = 'y'})
			by_color_args[1] = by_color_args[1] .. ' of ' .. country_prefix_text
			by_color_args[2] = by_color_args[2] .. ' of ' .. country_prefix_text
		end
		color_display = expandTemplate('by color', by_color_args)
	end
	
	local fmbox_text = color_sample(args[1]) .. ' ' .. expandTemplate('i18n/colored flags', {args[1]})
	if country then
		fmbox_text = fmbox_text .. ' ' .. i18n_of_country
	end
	local fmbox_display = fmbox(fmbox_text)
	
	local heraldry = ''
	if country then
		local heraldic_cat
		if args[1] == 'bicolor' then
			heraldic_cat = 'Two-colored heraldic shields of ' .. country_prefix_text
		elseif args[1] == 'tricolor' then
			heraldic_cat = 'Three-colored heraldic shields of ' .. country_prefix_text
		elseif args[1] == 'four-color' then
			heraldic_cat = 'Four-colored heraldic shields of ' .. country_prefix_text
		end
		if heraldic_cat and mw.title.makeTitle('Category', heraldic_cat).exists then
			heraldry = cat_see_also({heraldic_cat})
		end
	end
	
	local categories = {}
	if country then
		local country_by_color_cat = 'Flags of ' .. country_prefix_text .. ' by color'
		if country == 'Canada' or country == 'United Kindgom' then
			country_by_color_cat = 'Flags of ' .. country_prefix_text .. ' by colour'
		end
		local country_by_color_sorts = {
			['bicolor'] = '!02color',
			['tricolor'] = '!03color',
			['four-color'] = '!04color',
			['five-color'] = '!05color'
		}
		table.insert(categories, '[[Category:' .. country_by_color_cat .. '|' .. (country_by_color_sorts[args[1]] or args[1]) .. ']]')
		
		table.insert(categories, '[[Category:' .. color_cat_name(args[1]) .. ' flags by country|' .. country .. ']]')
		table.insert(categories, '[[Category:' .. color_cat_name(args[1]) .. ' objects in '.. country_prefix_text .. '|flags]]')
	else
		categories = {
		'[[Category:' .. 'Flags by color' .. ']]',
		'[[Category:' .. args[1] .. ' objects|Flags]]',
		'[[Category:' .. args[1] .. ' symbols|Flags]]'
		}
		if shadeof then
			table.insert(categories, '[[Category:' .. shadeof .. ' flags|+' .. args[1] .. ']]')
		end
		if args[1] ~= 'bicolor' and args[1] ~= 'tricolor' and args[1] ~= 'four-color' and args[1] ~= 'five-color' then
			table.insert(categories, '[[Category:' .. args[1] .. ' background|Flags]]')
		end
	end
	
	return table.concat({
		countries_of_continent,
		color_display,
		fmbox_display,
		heraldry,
		table.concat(categories)
	})
end

function p.cat_2_color_flags(args)
	local arg_check = error_for_first_n_args(args, 2)
	if arg_check then
		return arg_check
	end
	
	local country = args.country
	local country_prefix_text = args.country_prefix_text
	local i18n_of_country = args.i18n_of_country
	local continent = args.continent
	
	local countries_of_continent = ''
	if country then
		countries_of_continent = expandTemplate(
			'countries of ' .. continent,
			{prefix = ':Category:' .. args[1] .. ' and ' .. args[2] .. ' flags of'}
		)
	end
	
	local bicolor_topic = 'flags'
	if country then
		bicolor_topic = bicolor_topic .. ' of ' .. country_prefix_text
	end
	local bicolor_display = bicolor({topic = bicolor_topic})
	
	local fmbox_text = color_sample(args[1]) .. color_sample(args[2]) .. ' ' .. expandTemplate('i18n/colored flags', {args[1], args[2]})
	if country then
		fmbox_text = fmbox_text .. ' ' .. i18n_of_country
	end
	local fmbox_display = fmbox(fmbox_text)
	
	local heraldry = ''
	if not country then
		local heraldic_cat = ucfirst(args[1]) .. ' and '.. args[2] .. ' in heraldry'
		if heraldic_cat and mw.title.makeTitle('Category', heraldic_cat).exists then
			heraldry = cat_see_also({heraldic_cat})
		end
	end
	
	local intersect_categories_text = ' flags'
	if country then
		intersect_categories_text = intersect_categories_text .. ' of ' .. country_prefix_text
	end
	local intersect_categories_box = intersect_categories({
		['mbox-style'] = 'width:100%; clear:both; margin:0.5em auto;',
		['mbox-type'] = 'message',
		[1] = color_cat_name(args[1]) .. intersect_categories_text,
		[2] = color_cat_name(args[2]) .. intersect_categories_text
	})
	
	local categories = {}
	if country then
		categories = {
			'[[Category:Bicolor flags of ' .. country_prefix_text .. ']]',
			'[[Category:' .. args[1] .. ' and ' .. args[2] .. ' flags by country|' .. country .. ']]',
			'[[Category:' .. args[1] .. ' and ' .. args[2] .. ' in ' .. country_prefix_text .. '|Flags]]',
			'[[Category:' .. color_cat_name(args[1]) .. ' flags of ' .. country_prefix_text .. '|' .. args[2] .. ']]',
			'[[Category:' .. color_cat_name(args[2]) .. ' flags of ' .. country_prefix_text .. '|' .. args[1] .. ']]'
		}
	else
		categories = {
			'[[Category:' .. 'Bicolor flags' .. ']]',
			'[[Category:' .. args[1] .. ' and ' .. args[2] .. ' objects|Flags]]',
			'[[Category:' .. args[1] .. ' and ' .. args[2] .. ' symbols|Flags]]',
			'[[Category:' .. color_cat_name(args[1]) .. ' flags' .. '|' .. args[2] .. ']]',
			'[[Category:' .. color_cat_name(args[2]) .. ' flags' .. '|' .. args[1] .. ']]'
		}
	end
	
	return table.concat({
		countries_of_continent,
		bicolor_display,
		fmbox_display,
		heraldry,
		intersect_categories_box,
		table.concat(categories)
	})
end

function p.cat_3_color_flags(args)
	local arg_check = error_for_first_n_args(args, 3)
	if arg_check then
		return arg_check
	end
	
	local country = args.country
	local country_prefix_text = args.country_prefix_text
	local i18n_of_country = args.i18n_of_country
	local continent = args.continent
	local numbered_args = {args[1], args[2], args[3]}
	local numbered_args_concat = table.concat(numbered_args, ', ')
	
	local countries_of_continent = ''
	if country then
		countries_of_continent = expandTemplate(
			'countries of ' .. continent,
			{prefix = ':Category:' .. numbered_args_concat .. ' flags of'}
		)
	end
	
	local tricolor_topic = 'flags'
	if country then
		tricolor_topic = tricolor_topic .. ' of ' .. country_prefix_text
	end
	local tricolor_display = expandTemplate('tricolor', {tricolor_topic})
	
	local fmbox_text = table.concat({color_sample(args[1]), color_sample(args[2]), color_sample(args[3])}) .. ' ' .. expandTemplate('i18n/colored flags', numbered_args)
	if country then
		fmbox_text = fmbox_text .. ' ' .. i18n_of_country
	end
	local fmbox_display = fmbox(fmbox_text)
	
	local heraldry = ''
	if not country then
		local heraldic_cat = table.concat({ucfirst(args[1]), args[2], args[3]}, ', ') .. ' in heraldry'
		if heraldic_cat and mw.title.makeTitle('Category', heraldic_cat).exists then
			heraldry = cat_see_also({heraldic_cat})
		end
	end
	
	local intersect_categories_text = ' flags'
	if country then
		intersect_categories_text = intersect_categories_text .. ' of ' .. country_prefix_text
	end
	local intersect_categories_list = {
		intersect_categories({
			['mbox-style'] = 'width:100%; clear:both; margin:0.5em auto;',
			['mbox-type'] = 'message',
			[1] = color_cat_name(args[1]) .. ' and ' .. color_cat_name(args[2]) .. intersect_categories_text,
			[2] = color_cat_name(args[1]) .. ' and ' .. color_cat_name(args[3]) .. intersect_categories_text,
		}),
		intersect_categories({
			['mbox-style'] = 'width:100%; clear:both; margin:0.5em auto;',
			['mbox-type'] = 'message',
			[1] = color_cat_name(args[1]) .. ' and ' .. color_cat_name(args[2]) .. intersect_categories_text,
			[2] = color_cat_name(args[2]) .. ' and ' .. color_cat_name(args[3]) .. intersect_categories_text,
		}),
		intersect_categories({
			['mbox-style'] = 'width:100%; clear:both; margin:0.5em auto;',
			['mbox-type'] = 'message',
			[1] = color_cat_name(args[1]) .. ' and ' .. color_cat_name(args[3]) .. intersect_categories_text,
			[2] = color_cat_name(args[2]) .. ' and ' .. color_cat_name(args[3]) .. intersect_categories_text,
		})
	}
	local intersect_categories_collapsible = expandTemplate(
		'collapse',
		{
			['title'] = ucfirst(expandTemplate('i18n/intersect categories', {})),
			[1] = table.concat(intersect_categories_list)
		}
	)
	
	local categories = {}
	if country then
		categories = {
			'[[Category:Tricolor flags of ' .. country_prefix_text .. ']]',
			'[[Category:' .. numbered_args_concat .. ' flags by country|' .. country .. ']]',
			'[[Category:' .. numbered_args_concat .. ' in ' .. country_prefix_text .. '|Flags]]',
		}
	else
		categories = {
			'[[Category:' .. 'Tricolor flags' .. ']]',
			'[[Category:' .. numbered_args_concat .. ' objects|Flags]]',
			'[[Category:' .. numbered_args_concat .. ' symbols|Flags]]'
		}
	end
	local country_cat_suffix = ''
	if country then
		country_cat_suffix = ' of ' .. country_prefix_text
	end
	-- this seems inefficient
	for i = 1, 3 do
		local other_colors = {}
		for j = 1, 3 do
			if j ~= i then
				table.insert(other_colors, args[j])
			end
		end
		table.insert(categories, '[[Category:' .. table.concat(other_colors, ', ') .. country_cat_suffix .. '|' .. args[i] .. ']]')
	end
	
	return table.concat({
		countries_of_continent,
		tricolor_display,
		fmbox_display,
		heraldry,
		intersect_categories_collapsible,
		table.concat(categories)
	})
end

function p.cat_4_color_flags(args)
	local arg_check = error_for_first_n_args(args, 4)
	if arg_check then
		return arg_check
	end
	
	local country = args.country
	local country_prefix_text = args.country_prefix_text
	local i18n_of_country = args.i18n_of_country
	local continent = args.continent
	local numbered_args = {args[1], args[2], args[3], args[4]}
	local numbered_args_concat = table.concat(numbered_args, ', ')
	
	local countries_of_continent = ''
	if country then
		countries_of_continent = expandTemplate(
			'countries of ' .. continent,
			{prefix = ':Category:' .. numbered_args_concat .. ' flags of'}
		)
	end
	
	local fmbox_text = table.concat({color_sample(args[1]), color_sample(args[2]), color_sample(args[3]), color_sample(args[4])}) .. ' ' .. expandTemplate('i18n/colored flags', numbered_args)
	if country then
		fmbox_text = fmbox_text .. ' ' .. i18n_of_country
	end
	local fmbox_display = fmbox(fmbox_text)
	
	local heraldry = ''
	if not country then
		local heraldic_cat = table.concat({ucfirst(args[1]), args[2], args[3], args[4]}, ', ') .. ' in heraldry'
		if heraldic_cat and mw.title.makeTitle('Category', heraldic_cat).exists then
			heraldry = cat_see_also({heraldic_cat})
		end
	end
	
	local categories = {}
	if country then
		categories = {
			'[[Category:Four-color flags of ' .. country_prefix_text .. ']]',
			'[[Category:' .. numbered_args_concat .. ' flags by country|' .. country .. ']]',
			'[[Category:' .. numbered_args_concat .. ' in ' .. country_prefix_text .. '|Flags]]',
		}
	else
		categories = {
			'[[Category:' .. 'Four-color flags' .. ']]',
			'[[Category:' .. numbered_args_concat .. ' objects|Flags]]',
			'[[Category:' .. numbered_args_concat .. ' symbols|Flags]]'
		}
	end
	local country_cat_suffix = ''
	if country then
		country_cat_suffix = ' of ' .. country_prefix_text
	end
	-- this seems inefficient
	for i = 1, 4 do
		local other_colors = {}
		for j = 1, 3 do
			if j ~= i then
				table.insert(other_colors, args[j])
			end
		end
		table.insert(categories, '[[Category:' .. table.concat(other_colors, ', ') .. country_cat_suffix .. '|' .. args[i] .. ']]')
	end
	
	return table.concat({
		countries_of_continent,
		fmbox_display,
		heraldry,
		table.concat(categories)
	})
end

function p.cat_5_color_flags(args)
	local arg_check = error_for_first_n_args(args, 5)
	if arg_check then
		return arg_check
	end
	
	local country = args.country
	local country_prefix_text = args.country_prefix_text
	local i18n_of_country = args.i18n_of_country
	local continent = args.continent
	local numbered_args = {args[1], args[2], args[3], args[4], args[5]}
	local numbered_args_concat = table.concat(numbered_args, ', ')
	
	local countries_of_continent = ''
	if country then
		countries_of_continent = expandTemplate(
			'countries of ' .. continent,
			{prefix = ':Category:' .. numbered_args_concat .. ' flags of'}
		)
	end
	
	local fmbox_text = table.concat({color_sample(args[1]), color_sample(args[2]), color_sample(args[3]), color_sample(args[4]), color_sample(args[5])}) .. ' ' .. expandTemplate('i18n/colored flags', numbered_args)
	if country then
		fmbox_text = fmbox_text .. ' ' .. i18n_of_country
	end
	local fmbox_display = fmbox(fmbox_text)
	
	local heraldry = ''
	if not country then
		local heraldic_cat = table.concat({ucfirst(args[1]), args[2], args[3], args[4], args[5]}, ', ') .. ' in heraldry'
		if heraldic_cat and mw.title.makeTitle('Category', heraldic_cat).exists then
			heraldry = cat_see_also({heraldic_cat})
		end
	end
	
	local categories = {}
	if country then
		categories = {
			'[[Category:Five-color flags of ' .. country_prefix_text .. ']]',
			'[[Category:' .. numbered_args_concat .. ' flags by country|' .. country .. ']]',
			'[[Category:' .. numbered_args_concat .. ' in ' .. country_prefix_text .. '|Flags]]',
		}
	else
		categories = {
			'[[Category:' .. 'Five-color flags' .. ']]',
			'[[Category:' .. numbered_args_concat .. ' objects|Flags]]',
			'[[Category:' .. numbered_args_concat .. ' symbols|Flags]]'
		}
	end
	local country_cat_suffix = ''
	if country then
		country_cat_suffix = ' of ' .. country_prefix_text
	end
	-- this seems inefficient
	for i = 1, 5 do
		local other_colors = {}
		for j = 1, 5 do
			if j ~= i then
				table.insert(other_colors, args[j])
			end
		end
		table.insert(categories, '[[Category:' .. table.concat(other_colors, ', ') .. country_cat_suffix .. '|' .. args[i] .. ']]')
	end
	
	return table.concat({
		countries_of_continent,
		fmbox_display,
		heraldry,
		table.concat(categories)
	})
end

function p._cat_color_flags(args)
	local color_count = 0
	local max_color_count = 5
	for k, v in pairs(args) do
		if tonumber(k) and tonumber(k) > color_count then
			color_count = k
		end
	end
	if color_count == 0 then
		return error_message({message = 'Error in [[Module:Cat color flags]]: supply at least one argument.'})
	elseif color_count > max_color_count then
		return error_message({message = 'Error: [[Module:Cat color flags]] only supports up to ' .. max_color_count .. ' arguments.'})
	end
	
	if args.country then
		args.country_prefix_text = expandTemplate('country prefix', {args.country})
		args.i18n_of_country = expandTemplate('i18n/of place', {[1] = args.country, plural = 'y'})
		if not args.continent then
			args.continent = expandTemplate('continent from country', {args.country})
		end
	end
	
	local div = mw.html.create('div')
	div:addClass('cat-color-flags')
	div:wikitext(p['cat_' .. color_count .. '_color_flags'](args))
	return tostring(div)
end

function p.cat_color_flags(frame)
	return p._cat_color_flags(getArgs(frame))
end

return p