모듈:Country alias/연습장

-- This module returns the country name or the flag name for a country,
-- based on the three-letter IOC/CGA/FINA alias.
--
The following country code is used for multiple countries:
ANG (workaround: added ANG_CGF for use with Commonwealth Games)
The following names have different names/flags based on sport/year
Great Britain (and N.I.)         GBR, GBR_WCA (latter added to add text in parens)
Hong Kong                        HKG, HKG_CGF (latter added to keep colonial flag)
Individual Olympic Athletes      IOA, IOA_2000 (IOA changed to Independent Olympic Athletes in 2012)
Russian Athletes				 OAR (2018), ROC_2020 (2020 Summer, 2022 Winter)
SWZ                              Swaziland became Eswatini after the 2018 Commonwealth Games
MKD								 Macedonia became North Macedonia in 2019
ART								 No "Athlete" before Refugee Team @ 2017 AIMAG
D.R. of the Congo				 Was Zaire (ZAI) and Congo-Kinshasa (COK_COD)
The following countries have multiple aliases due to CGF/IOC/FINA/IAAF/etc differences, or deprecated uses
Anguilla                         AIA, ANG_CGF
Antigua and Barbuda              ANT, ATG
Bahrain                          BHN, BHR, BRN
Curaçao                          CUR, CUW
East Timor						 TLS, TMP
Falkand Islands					 FAI, FLK
Faroe Islands                    FAR, FRO
Guernsey                         GGY, GUE
Iran                             IRI, IRN
Ireland                          IRE, IRL - IRE is *only* for CGF apps
Jersey                           JER, JEY
Lebanon                          LBN, LIB
Montserrat                       MNT, MSR
Nicaragua                        NCA, NIC
Norfolk Island                   NFI, NFK
Oman                             OMA, OMN
Refugee Olympic Team             ROA, ROT, EOR
Romania                          ROM, ROU
Saint Helena                     SHE, SHN
Saint Vincent and the Grenadines SVG, VIN
Sarawak                          SAR, SWK
Singapore                        SGP, SIN
South Africa                     RSA, SAF
Tonga                            TGA, TON
Trinidad and Tobago              TRI, TTO
Turks and Caicos Islands         TCA, TCI, TKS
Oddity that needs to be revisited
French Polynesia                 PYF, TAH - TAH has been converted to Tahiti per SILENCE
local function stripToNil(text)
	-- If text is a string, return its trimmed content, or nil if empty.
	-- Otherwise return text (which may, for example, be nil).
	if type(text) == 'string' then
		text = mw.ustring.match(text, '(%S.-)%s*$')
	end
	return text
end
local function yes(parameter)
	-- Return true if parameter should be interpreted as "yes".
	return ({ y = true, yes = true, on = true, true = true })parameter
end
local function getAlias(args)
	-- Return alias parameter, possibly modified for exceptional cases.
	local alias = stripToNil(args.alias)
	local games = stripToNil(args.games)
	local year = tonumber(args.year)
	local fullName = stripToNil(args.fullName)
	if fullName then
		year = tonumber(fullName:match('^%d+'))  -- ignore args.year
	end
	if alias == 'ANG' then
		if games == '코먼웰스 게임' then
			alias = 'ANG_CGF'
		end
	elseif alias == 'ART' then
		if games == 'Asian Indoor and Martial Arts Games' then
			alias = 'ART_AIMAG'
		end
	elseif alias == 'GBR' then
		if games == '세계 육상 선수권 대회' or games == '유럽 육상 선수권 대회' then
			alias = 'GBR_WCA'
		elseif games == '유럽 선수권 대회' then
			if year == 2018 then
				alias = 'GBR_WCA'
			end
		end
	elseif alias == 'HKG' then
		if games == '코먼웰스 게임' then
			alias = 'HKG_CGF'
		end
	elseif alias == 'IOA' then
		if year == 2000 then
			alias = 'IOA_2000'
		end
	elseif alias == 'MAL' or alias == 'MAS' then
		if year and year = 2018 and fullName ~= '2018년 코먼웰스 게임' then
				alias = 'SWZ_YO2018'
			end
		elseif year and year >= 2018 and games ~= '코먼웰스 게임' then
			alias = 'SWZ_YO2018'
		else
			alias = 'SWZ_YO2018'
		end
	elseif alias == 'MKD' then
		if year and year  1992 and year   , with the third value optional
	local args = frame:getParent().args
	local code = stripToNil(args1) or error('flagIOC parameter 1 should be a country code')
	local games = stripToNil(args2)
	local athletes = stripToNil(args3)
	if athletes == '하계' or athletes == '동계' or athletes == '중간' or athletes == '청소년' or athletes == '하계 청소년' or athletes == '동계 청소년' then
		games = games .. ' ' .. athletes
		athletes = stripToNil(args4)
	end
	games = getOlympics(games)
	local parms = {
		alias = code,
		fullName = games,
		year = mw.ustring.match(games, '^%d+'),
		games = mw.ustring.gsub(games, '^%d+년? ?', ),
	}
	local fullName = countryAlias(parms)
	parms.flag = true
	local dispName = fullName
	if fullName == 'ROC' then
		fullName = '러시아 올림픽 위원회'
	end
	return (('link= {dispName}{athletes}')
		:gsub('{(%w+)}', {
			athletes = athletes and
				(' (' .. athletes .. ')') or
				,
			flag = countryAlias(parms),
			games = games,
			name = fullName,
			dispName = dispName,
			teamName = mw.ustring.find(fullName, '선수') and fullName or fullName .. ' 선수단'
		}))
end
local function flagXYZ(frame)
	-- Implement  and its variants which previously called this module three times.
	-- Returns one of four possible outputs:
	--	from flagIOC2:			  , with the third value optional
	--	from flagIOC2team:		  
	--	from flagIOC2athlete:	  
	--	from flagIOC2medalist:	 
	local args = frame:getParent().args
	local dispType = stripToNil(frame.args'type')
	local code=
	local games=
	local athletes=
	if dispType == 'athlete-O' then
		-- Implement Template:FlagIOCathlete.
		dispType = 'athlete'
		args3 = (args3 or ) .. ' Olympics'
	end
	if dispType == 'name' or dispType == 'team' then
		code = stripToNil(args1) or error('Parameter 1 should be a country code')
		games = stripToNil(args2) or error('Parameter 2 should be a competition name')
		athletes = stripToNil(args3)
	elseif dispType == 'athlete' or dispType == 'medalist' then
		athletes = stripToNil(args1) or error('Parameter 1 should be the name(s) of the athlete(s)')
		code = stripToNil(args2) or error('Parameter 2 should be a country code')
		games = stripToNil(args3) or error('Parameter 3 should be a competition name')
	end
	
	local parms = {
		alias = code,
		fullName = games,
		-- year = games:match('^%d+'),
		-- games = games:gsub('^%d+ ?', ),
		year = mw.ustring.match(games, '^%d+'),
		games = mw.ustring.gsub(games, '^%d+년? ?', ),
	}
	local fullName = countryAlias(parms)
	parms.flag = true
	local dispName = stripToNil(args.name)
	if fullName == 'ROC' then
		fullName = 'Russian Olympic Committee athletes'
		dispName = 'ROC'
		if dispType == 'team' then dispType = 'name' end
	end
	
	if dispType == 'name' then
		return (('link= {dispName}{athletes}')
			:gsub('{(%w+)}', {
				athletes = athletes and
					(' (' .. athletes .. ')') or
					,
				flag = countryAlias(parms),
				games = games,
				name = fullName,
				dispName = dispName or fullName,
			}))
	elseif dispType == 'team' then
		return (('link= {dispName}{alias}')
			:gsub('{(%w+)}', {
				alias = (' (' .. code .. ')'),
				flag = countryAlias(parms),
				games = games,
				name = fullName,
				dispName = dispName or fullName,
			}))
	elseif dispType == 'athlete' then
		return (('link= {athletes} ({dispName})')
			:gsub('{(%w+)}', {
				athletes = athletes,
				flag = countryAlias(parms),
				games = games,
				name = fullName,
				dispName = code,
			}))
	elseif dispType == 'medalist' then
		return (('{athletes}link= {dispName}')
			:gsub('{(%w+)}', {
				athletes = athletes,
				flag = countryAlias(parms),
				games = games,
				name = fullName,
				dispName = dispName or fullName,
			}))
	end
end
local function main(frame)
	return countryAlias(frame.args)
end
return {
	flagIOC = flagIOC,
	flagXYZ = flagXYZ,
	main = main,
}