Module:Team appearances list: Difference between revisions

Content deleted Content added
refactor: absences can be specified in competition table or absences table or template parameters (the first specified is used)
update from Module:Team appearances list/sandbox: absences are integrated with competitions; parameters are validated
Line 3:
local p = {}
 
local data_competitions, data_absences
local function load_data(frame)
-- Load data module (or its sandbox) and set variables from its exported data.
Line 11:
local datamod = mw.loadData('Module:Team appearances list/data' .. sandbox)
data_competitions = datamod.competitions
data_absences = datamod.absences
end
end
Line 22 ⟶ 21:
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 = args[name]
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 <= arg and arg <= max) then
error('Parameter ' .. name .. ' is not valid')
end
return arg
end
local function valid_text(name)
local arg = args[name]
if arg == nil or arg == '' then
error('Parameter ' .. name .. ' is missing')
end
if type(arg) ~= 'string' then
error('Parameter ' .. name .. ' is not a string')
end
return arg
end
options.competition = valid_text('competition')
options.team = valid_text('team')
options.competitions = data_competitions[options.competition]
local begin_optional
if options.competitions then
begin_optional = true
else
options.interval = valid_integer('interval', 1, 30)
end
options.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 then
if options.begin_year > options.end_year then
error('Parameter end_year must not be before begin_year')
end
end
return options
end
 
Line 40 ⟶ 91:
end
dash = strip_to_nil(dash)
if not (dash == '-' or dash == '–') then
return
if #last ~= 4 then
end
if #last == 2 then
if #last ~= first:sub(1,4 2) .. lastthen
if #last == 2 then
else
last = first:sub(1, 2) .. last
return
endelse
return
end
end
Line 72 ⟶ 124:
local first, last = extract_range(item)
if not first then
error('InvalidYear year: "' .. item .. '" is not valid')
end
if last then
Line 90 ⟶ 142:
-- * Table of absent years.
-- * List of pairs of years (absent for each year in range, inclusive).
local options = make_options(args)
local absences
local comp_years = {}
local begin_year = tonumber(argsoptions.begin_year)
local end_year = tonumber(argsoptions.end_year)
local competitions = data_competitions[argsoptions.competition]competitions
if competitions then
absences = competitions[argsoptions.team] or
begin_year = begin_year or (absences and absences.begin_year) or 0
data_absences[args.team .. ' ' .. args.competition]
begin_year = begin_year or 0
end_year = end_year or 9999
for _, y in ipairs(competitions) do
Line 108 ⟶ 160:
end
else
end_year = end_year or (os.date('!*t').year + options.interval)
if not (begin_year and 1800 <= begin_year and begin_year <= 2100) then
for y = begin_year, end_year, options.interval do
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
Line 129 ⟶ 168:
end
 
-- TODO Missing parameters generate an ugly error (say if args.team is nil).
-- Should show error if begin_year > end_year.
function p._main(args)
load_data() -- in case this function is called by another module
local hlist = require('Module:List').horizontal
local competitions, absent_years, absent_ranges = competition_information(args)
local function is_absent(y)
if absent_years and absent_ranges[y] then
return true
is_absent = function (y)
end
if absent_years[y] then
for _, range in ipairs(absent_ranges) do
if range[1] <= y and y <= range[2] 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
is_absent = function (y)
return false
end
return false
end
local appearances = {}