모듈:Team appearances list

-- This module implements [[Template:Team appearances list]].local p = {}-- i18n support for Koreanlocal param_ko = {['국가'] = 'team',['선수단'] = 'team',['대회'] = 'competition',['첫참가'] = 'begin_year',['마지막참가'] = 'end_year',['연속'] = 'interval',}local data_competitionslocal data_old_nameslocal function load_data(frame)-- Load data module (or its sandbox) and set variables from its exported data.if not data_competitions thenframe = frame or mw.getCurrentFrame()local sandbox = frame:getTitle():find('연습장', 1, true) and '/연습장' or ''local datamod = mw.loadData('모듈:Team appearances list/data' .. sandbox)data_competitions = datamod.competitionsdata_old_names = datamod.old_namesendendlocal function strip_to_nil(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' thentext = text:match('(%S.-)%s*$')endreturn textendlocal function make_options(args)-- Return table of options from validated args or throw error.local options = {}local function valid_integer(name, min, max, is_optional)local arg = args[name]if arg == nil or arg == '' thenif is_optional thenreturn nilenderror('' .. name .. ' 변수가 빠졌습니다')endarg = tonumber(arg)if type(arg) ~= 'number' thenerror('' .. name .. ' 변수에는 숫자가 들어가야 합니다')endif math.floor(arg) ~= arg thenerror('' .. name .. ' 변수에는 정수가 들어가야 합니다')endif not (min <= arg and arg <= max) thenerror('' .. name .. ' 변수가 유효하지 않습니다')endreturn argendlocal function valid_text(name)local arg = args[name]if arg == nil or arg == '' thenerror(name .. ' 변수가 빠졌습니다')endif type(arg) ~= 'string' thenerror(name .. ' 변수는 문자열이 아닙니다')endreturn argend-- i18n support for Koreanlocal function localname(parameter)return param_ko[parameter] or parameterendlocal parent_args = argsfor k, v in pairs(parent_args) doif v ~= '' thenargs[localname(k)] = vendendoptions.competition = valid_text('competition')options.team = valid_text('team')options.competitions = data_competitions[options.competition] or data_competitions[data_old_names[options.competition]]local begin_optionalif options.competitions thenbegin_optional = trueelseoptions.interval = valid_integer('interval', 1, 30)endoptions.begin_year = valid_integer('begin_year', 1800, 2100, begin_optional)options.end_year = valid_integer('end_year', 1800, 2100, true)if options.begin_year and options.end_year thenif options.begin_year > options.end_year thenerror('end_year 변수는 begin_year 보다 앞서지 않아야 합니다')endendoptions.disqualified_year = valid_integer('disqualified_year', 1800, 2100, true)return optionsendlocal function extract_range(text)-- Return first (if text is a single year), or first, last if a range.-- The returned values are numbers.-- Return nothing if text is invalid.local year = text:match('^(%d+)$')if year thenif #year == 4 thenreturn tonumber(year)endreturnendlocal first, dash, last = text:match('^(%d+)(%D+)(%d+)$')if not (first and #first == 4) thenreturnenddash = strip_to_nil(dash)if not (dash == '-' or dash == '–') thenreturnendif #last ~= 4 thenif #last == 2 thenlast = first:sub(1, 2) .. lastelsereturnendendfirst = tonumber(first)last = tonumber(last)if first < last thenreturn first, lastelseif first == last thenreturn firstendendlocal function competition_absences(data)-- Return two tables with absent years and absent year ranges.-- Parameter data is an array of strings from template parameters, or-- numbers or strings from built-in data.-- Parameters that are blank or not numbers or strings are ignored.local absent_years, absent_ranges = {}, {}for _, item in ipairs(data) doif type(item) == 'number' thenabsent_years[item] = trueelseitem = strip_to_nil(item)if type(item) == 'string' thenlocal first, last = extract_range(item)if not first thenerror('연도 ' .. item .. ' 항목이 유효하지 않습니다')endif last thentable.insert(absent_ranges, {first, last})elseabsent_years[first] = trueendendendendreturn absent_years, absent_rangesendlocal function competition_information(args)-- Return four tables with competition and team information:-- * List of competition years that the team attended or could have attended.-- * Table of disqualified years (the team was absent, but there is an--   article regarding the absent year).-- * Table of absent years (when the team did not attend).-- * List of pairs of years (absent for each year in range, inclusive).local options = make_options(args)local absenceslocal comp_years = {}local begin_year = options.begin_yearlocal end_year = options.end_yearlocal competitions = options.competitionsif competitions thenabsences = competitions[options.team] or competitions[data_old_names[options.team]]begin_year = begin_year or (absences and absences.begin_year) or 0end_year = end_year or (absences and absences.end_year) or 9999for _, y in ipairs(competitions) doif y > end_year thenbreakelseif y >= begin_year thentable.insert(comp_years, y)endendelseend_year = end_year or (os.date('!*t').year + options.interval)for y = begin_year, end_year, options.interval dotable.insert(comp_years, y)endendlocal disqualified_years = {}if options.disqualified_year then-- Input currently only allows entry of a single disqualified year.-- However processing works for any number of such years.disqualified_years[options.disqualified_year] = trueendreturn comp_years, disqualified_years, competition_absences(absences or args)endlocal function gameName(year, inputName)-- Modifies output of display being sent back to the hlist--  for games that have had a name change but are still considered--  the same competition.if inputName=="World Athletics Championships" or inputName=="World Championships in Athletics" thenif year <= 2017 thenreturn "World Championships in Athletics"elsereturn "World Athletics Championships"endelseif (inputName=="British Empire Games"or inputName=="British Empire and Commonwealth Games"or inputName=="British Commonwealth Games"or inputName=="Commonwealth Games") thenif year <= 1950 thenreturn "British Empire Games"elseif year <= 1966 thenreturn "British Empire and Commonwealth Games"elseif year <= 1974 thenreturn "British Commonwealth Games"elsereturn "Commonwealth Games"endelseif inputName=="Southeast Asian Peninsular Games" or inputName=="Southeast Asian Games" thenif year <= 1975 thenreturn "Southeast Asian Peninsular Games"elsereturn "Southeast Asian Games"endelseif inputName=="Asian Indoor Games" or inputName=="Asian Indoor and Martial Arts Games" thenif year <= 2009 thenreturn "Asian Indoor Games"elsereturn "Asian Indoor and Martial Arts Games"endelseif inputName=="Southern Cross Games" or inputName=="South American Games" thenif year <= 1982 thenreturn "Southern Cross Games"elsereturn "South American Games"endelseif inputName=="All-Africa Games" or inputName=="African Games" thenif year <= 2011 thenreturn "All-Africa Games"elsereturn "African Games"endelsereturn inputNameendendlocal function teamName(year, inputName, comp)-- Modifies output of display being sent back to the hlist--  for games that have had a name change but are still considered--  the same competition.if inputName=="Eswatini" or inputName=="Swaziland" thenif year < 2018 or year == 2018 and comp == 'Commonwealth Games' thenreturn "Swaziland"elsereturn "Eswatini"endelseif inputName=="Upper Volta" or inputName=="Burkina Faso" thenif year <= 1983 thenreturn "Upper Volta"elsereturn "Burkina Faso"endelseif inputName=="Democratic Republic of the Congo" or inputName=="Zaire" thenif year >= 1984 and year <=1996 thenreturn "Zaire"elsereturn "Democratic Republic of the Congo"endelseif (inputName=="Individual Olympic Athletes" or inputName=="Independent Olympic Athletes" or inputName=="Independent Olympic Participants"or inputName=="Olympic Athletes from Russia"or inputName=="ROC") thenif year == 1992 or year==2014 thenreturn "Independent Olympic Participants"elseif year == 2000 thenreturn "Individual Olympic Athletes"elseif year == 2012 or year==2016 thenreturn "Independent Olympic Athletes"elseif year == 2018 thenreturn "Olympic Athletes from Russia"elseif year == 2020 or year==2022 thenreturn "ROC"elsereturn inputNameendelseif (inputName=="Independent Paralympic Participants" or inputName=="Individual Paralympic Athletes" or inputName=="Independent Paralympic Athletes"or inputName=="RPC") thenif year == 1992 thenreturn "Independent Paralympic Participants"elseif year == 2000 thenreturn "Individual Paralympic Athletes"elseif year==2016 thenreturn "Independent Paralympic Athletes"elseif year == 2020 or year==2022 thenreturn "RPC"elsereturn inputNameendelseif inputName=="North Macedonia" or inputName=="Macedonia" thenif year < 2019 thenreturn "Macedonia"elsereturn "North Macedonia"endelseif inputName=="Malaysia" or inputName=="Malaya" thenif year < 1963 thenreturn "Malaya"elsereturn "Malaysia"end-- 대한민국 지역elseif inputName=="강원특별자치도" or inputName=="강원도" thenif year <= 2022 and comp == '전국체육대회' thenreturn "강원도"elseif year <= 2023 and comp == '전국동계체육대회' thenreturn "강원도"elseif year <= 2023 and comp == '전국소년체육대회' thenreturn "강원도"elsereturn "강원특별자치도"endelseif inputName=="광주광역시" or inputName=="광주직할시" thenif year <= 1994 thenreturn "광주직할시"elsereturn "광주광역시"endelseif inputName=="대구광역시" or inputName=="대구직할시" thenif year <= 1994 thenreturn "대구직할시"elsereturn "대구광역시"endelseif inputName=="대전광역시" or inputName=="대전직할시" thenif year <= 1994 thenreturn "대전직할시"elsereturn "대전광역시"endelseif inputName=="부산광역시" or inputName=="부산직할시" or inputName=="부산시" thenif year <= 1980 and comp == '전국체육대회' thenreturn "부산시"elseif year <= 1982 and comp == '전국동계체육대회' thenreturn "부산시"elseif year <= 1980 and comp == '전국소년체육대회' thenreturn "부산시"elseif year <= 1994 thenreturn "부산직할시"elsereturn "부산광역시"endelseif inputName=="서울특별시" or inputName=="서울특별자유시" thenif year <= 1948 and comp == '전국체육대회' thenreturn "서울특별자유시"elseif year <= 1949 and comp == '전국동계체육대회' thenreturn "서울특별자유시"elsereturn "서울특별시"endelseif inputName=="인천광역시" or inputName=="인천직할시" thenif year <= 1994 thenreturn "인천직할시"elsereturn "인천광역시"endelseif inputName=="전북특별자치도" or inputName=="전라북도" thenif year <= 2023 thenreturn "전라북도"elsereturn "전북특별자치도"endelseif inputName=="제주특별자치도" or inputName=="제주도" thenif year <= 2005 and comp == '전국체육대회' thenreturn "제주도"elseif year <= 2006 and comp == '전국동계체육대회' thenreturn "제주도"elseif year <= 2006 and comp == '전국소년체육대회' thenreturn "제주도"elsereturn "제주특별자치도"endelsereturn inputNameendendfunction p._main(args)load_data()  -- in case this function is called by another modulelocal hlist = require('Module:List').horizontallocal competitions, disqualified_years, absent_years, absent_ranges = competition_information(args)local current_year = os.date('!*t').yearlocal function is_absent(y)if absent_years[y] thenreturn trueendfor _, range in ipairs(absent_ranges) doif range[1] <= y and y <= range[2] thenreturn trueendendreturn falseendlocal appearances = {}local absent_first, absent_lastfor i = 1, #competitions + 1 do  -- +1 to handle any trailing absenceslocal y = competitions[i]if y and is_absent(y) thenif absent_first thenabsent_last = yelseabsent_first = yendelseif absent_first thentable.insert(appearances,'<span style="color:gray">' ..(absent_last and (absent_first .. '–' .. absent_last) or absent_first) ..'</span>')absent_first, absent_last = nil, nilendif y thenlocal display = tostring(y)if y > current_year thendisplay = '<i>' .. display .. '</i>'endif disqualified_years[y] thendisplay = '<del>' .. display .. '</del>'endlocal compName = gameName(y, args.competition)local teamOut = teamName(y, args.team, args.competition)if compName == 'FIS Alpine World Ski Championships' thentable.insert(appearances, string.format('[[%s at the %s %d|%s]]',teamOut, compName, y, display))elsetable.insert(appearances, string.format('[[%d년 %s %s 선수단|%s]]',y, compName, teamOut, display))endendendendreturn hlist(appearances)endfunction p.main(frame)load_data(frame)return p._main(frame:getParent().args)endreturn p