モジュール:Team appearances list

-- This module implements Template:Team appearances list.
local p = {}
local data_competitions
local data_old_names
local data_ja_names
local function load_data(frame)
	-- Load data module (or its sandbox) and set variables from its exported data.
	if not data_competitions then
		frame = frame or mw.getCurrentFrame()
		local sandbox = frame:getTitle():find('sandbox', 1, true) and '/sandbox' or 
		local datamod = mw.loadData('Module:Team appearances list/data' .. sandbox)
		data_competitions = datamod.competitions
		data_old_names = datamod.old_names
		data_ja_names = datamod.ja_names
	end
end
local 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' then
		text = text:match('(%S.-)%s*$')
	end
	return text
end
local 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 = argsname
		if arg == nil or arg ==  then
			if is_optional then
				return nil
			end
			error('Parameter ' .. name .. ' is missing')
		end
		arg = tonumber(arg)
		if type(arg) ~= 'number' then
			error('Parameter ' .. name .. ' is not a number')
		end
		if math.floor(arg) ~= arg then
			error('Parameter ' .. name .. ' is not an integer')
		end
		if not (min  options.end_year then
			error('Parameter end_year must not be before begin_year')
		end
	end
	options.disqualified_year = valid_integer('disqualified_year', 1800, 2100, true)
	return options
end
local 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 then
		if #year == 4 then
			return tonumber(year)
		end
		return
	end
	local first, dash, last = text:match('^(%d+)(%D+)(%d+)$')
	if not (first and #first == 4) then
		return
	end
	dash = strip_to_nil(dash)
	if not (dash == '-' or dash == '–') then
		return
	end
	if #last ~= 4 then
		if #last == 2 then
			last = first:sub(1, 2) .. last
		else
			return
		end
	end
	first = tonumber(first)
	last = tonumber(last)
	if first  end_year then
				break
			elseif y >= begin_year then
				table.insert(comp_years, y)
			end
		end
	else
		end_year = end_year or (os.date('!*t').year + options.interval)
		for y = begin_year, end_year, options.interval do
			table.insert(comp_years, y)
		end
	end
	local 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_yearsoptions.disqualified_year = true
	end
	return comp_years, disqualified_years, competition_absences(absences or args)
end
local 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" then
		if year = 1984 and year ' ..
					(absent_last and (absent_first .. '–' .. absent_last) or absent_first) ..
					)
				absent_first, absent_last = nil, nil
			end
			if y then
				local display = tostring(y)
				if y > current_year then
					display =  .. display .. 
				end
				if disqualified_yearsy then
					display =  .. display .. 
				end
				local compName = gameName(y, args.competition)
				local teamOut = teamName(y, args.team)
				table.insert(appearances, string.format(
					'%s',
					y, compName, teamOut, display
				))
			end
		end
	end
	return hlist(appearances)
end
				
function p.main(frame)
	load_data(frame)
	return p._main(frame:getParent().args)
end
return p