Module:Team appearances list/sandbox
![]() | This is the module sandbox page for Module:Team appearances list (diff). |
![]() | This module is rated as ready for general use. It has reached a mature form and is thought to be relatively bug-free and ready for use wherever appropriate. It is ready to mention on help pages and other Wikipedia resources as an option for new users to learn. To reduce server load and bad output, it should be improved by sandbox testing rather than repeated trial-and-error editing. |
![]() | This module is subject to page protection. It is a highly visible module in use by a very large number of pages, or is substituted very frequently. Because vandalism or mistakes would affect many pages, and even trivial editing might cause substantial load on the servers, it is protected from editing. |
This module implements {{Team appearances list}}. Check there for usage documentation.
Modules
- Module:Team appearances list • Module:Team appearances list/sandbox • different (diff)
- Module:Team appearances list/data • Module:Team appearances list/data/sandbox • different (diff)
- Module:Team appearances list/show • Module:Team appearances list/show/sandbox • same content
Module:Team appearances list is invoked by {{Team appearances list}} to display a horizontal list showing the years a specified team participated in a specified competition. Each year of attendance is wikilinked to a corresponding article, while years the team did not compete (absences) are shown as disabled.
The names of the competition and team must be specified. Optionally, information for a competition can be defined in Module:Team appearances list/data, and team information can be included:
begin_year
– The first year to be shown.end_year
– The last year to be shown.- Years the team did not attend the competition.
If begin_year
or end_year
are defined for a team, they set default values that can be overridden with parameters in the template.
If a team is defined for a particular competition, any absent years in the templace call are ignored (instead, the absences defined in the data module are used).
Module:Team appearances list/show is used for testing. It shows the results for all competition/team pairs defined in the data module. The results are displayed at Module talk:Team appearances list/show.
Changes should be performed in the sandbox modules, using the following for testing:
- {{Team appearances list/sandbox}} – Uses the sandbox main and data modules.
- Module talk:Team appearances list/show – Results for all competition/team pairs defined in the sandbox data module.
Errors
Parameters provided by the template are validated using the following rules.
Always: competition required : non-empty string team required : non-empty string If competition is defined in data module: begin_year optional : number from 1800 to 2100 inclusive end_year optional : as above (and end_year >= begin_year) else: begin_year required : as above end_year optional : as above interval required : number from 1 to 30 inclusive
An invalid parameter causes an error to be displayed and places the page in the hidden category Category:Pages with script errors.
-- This module implements [[Template:Team appearances list]].
-- [SublimeLinter luacheck-globals:mw]
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.
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_absences = datamod.absences
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 competition_years(args)
-- Return list of competition years specified in args.
local result = {}
local begin_year = tonumber(args.begin_year)
local end_year = tonumber(args.end_year)
local competitions = data_competitions[args.competition]
if competitions then
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(result, 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(result, y)
end
end
return result
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 dash == '-' or dash == '–' then
if #last ~= 4 then
if #last == 2 then
last = first:sub(1, 2) .. last
else
return
end
end
end
first = tonumber(first)
last = tonumber(last)
if first < last then
return first, last
elseif first == last then
return first
end
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 absent_years, absent_ranges = {}, {}
for _, v in ipairs(args) do
-- Extract absent years from numeric parameters.
-- Skip blank parameters (and non-strings that may be passed by a module).
v = strip_to_nil(v)
if type(v) == 'string' then
local first, last = extract_range(v)
if not first then
error('Invalid year: "' .. v .. '"')
end
if last then
table.insert(absent_ranges, {first, last})
else
absent_years[first] = true
end
end
end
local function is_absent(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
local appearances = {}
local absent_first, absent_last
local competitions = competition_years(args)
for i = 1, #competitions + 1 do -- +1 to handle any trailing absences
local y = competitions[i]
if y and is_absent(y) then
if absent_first then
absent_last = y
else
absent_first = y
end
else
if absent_first then
table.insert(appearances,
'<span style="color:gray">' ..
(absent_last and (absent_first .. '–' .. absent_last) or absent_first) ..
'</span>')
absent_first, absent_last = nil, nil
end
if y then
table.insert(appearances, string.format(
'[[%s at the %d %s|%d]]',
args.team, y, args.competition, y
))
end
end
end
return hlist(appearances)
end
function p.main(frame)
load_data(frame)
return p._main(frame:getParent().args)
end
return p