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
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
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
end
if #last == 2 then
last = first:sub(1, 2) .. last
return
end
end
Line 72 ⟶ 124:
local first, last = extract_range(item)
if not first then
error('
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 =
local end_year =
local competitions =
if competitions then
absences = competitions[
begin_year = begin_year or (absences and absences.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)
for y = begin_year, end_year, options.interval do
table.insert(comp_years, y)
end
Line 129 ⟶ 168:
end
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)
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
local appearances = {}
|