Content deleted Content added
move data to Module:Team appearances list/data and use mw.loadData to access it so module only loaded once per page |
refactor: absences can be specified in competition table or absences table or template parameters (the first specified is used) |
||
Line 1:
-- This module implements [[Template:Team appearances list]].
local p = {}
Line 23 ⟶ 22:
end
return text
end
Line 95 ⟶ 56:
return first
end
end
local 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) do
if type(item) == 'number' then
absent_years[item] = true
else
item = strip_to_nil(item)
if type(item) == 'string' then
local first, last = extract_range(item)
if not first then
error('Invalid year: "' .. item .. '"')
end
if last then
table.insert(absent_ranges, {first, last})
else
absent_years[first] = true
end
end
end
end
return absent_years, absent_ranges
end
local function competition_information(args)
-- Return three tables with information for the specified competition:
-- * List of competition years.
-- * Table of absent years.
-- * List of pairs of years (absent for each year in range, inclusive).
local absences
local comp_years = {}
local begin_year = tonumber(args.begin_year)
local end_year = tonumber(args.end_year)
local competitions = data_competitions[args.competition]
if competitions then
absences = competitions[args.team] or
data_absences[args.team .. ' ' .. args.competition]
begin_year = begin_year or 0
end_year = end_year or 9999
for _, y in ipairs(competitions) do
if y > end_year then
break
elseif y >= begin_year then
table.insert(comp_years, y)
end
end
else
if not (begin_year and 1800 <= begin_year and begin_year <= 2100) then
error('Parameter begin_year is not a valid year: ' .. tostring(args.begin_year))
end
local interval = tonumber(args.interval)
if not interval then
if not args.interval then
error('Parameter interval is missing')
end
error('Parameter interval is not a number: ' .. tostring(args.interval))
end
if not (1 <= interval and interval <= 30) then
error('Parameter interval is not valid: ' .. interval)
end
end_year = end_year or (os.date('!*t').year + interval)
for y = begin_year, end_year, interval do
table.insert(comp_years, y)
end
end
return comp_years, competition_absences(absences or args)
end
Line 102 ⟶ 134:
load_data() -- in case this function is called by another module
local hlist = require('Module:List').horizontal
local competitions, absent_years, absent_ranges =
local is_absent
if absent_years and absent_ranges then
is_absent = function (y)
if absent_years[y] then
return true
end
for _, range in ipairs(absent_ranges) do
if range[1] <= y and y <= range[2] then
return true
end
end
return false
end
else
return false
end
end
local appearances = {}
local absent_first, absent_last
for i = 1, #competitions + 1 do -- +1 to handle any trailing absences
local y = competitions[i]
|