Module:Team appearances list

This is an old revision of this page, as edited by Izkala (talk | contribs) at 21:39, 31 May 2015 (Automate greying out of absences). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

-- This module produces a horizontal list of team appearances at a specified
-- sports competition; each item is in the format:
-- [[<team> at the <year> <competition>|<year]]. The module requires the
-- following parameters: begin_year, interval, team and competition. end_year
-- may be optionally defined if either the competition or the team is now
-- defunct. Additionally, any number of positional parameters may be specified,
-- each of which must be a year (or year range) in which the team was absent
-- from the competition.

local p = {}
local compressSparseArray = require('Module:TableTools').compressSparseArray
local hlist = require('Module:List').horizontal
local valueUnion = require('Module:Set').valueUnion

function p.main(frame)
	local args = require('Module:Arguments').getArgs(frame, {parentOnly=true})
	return p._main(args)
end
 
function p._main(args)
	local appearances = {}
	local absences = {}
	for _, v in pairs(compressSparseArray(args)) do
		absences[mw.ustring.sub(v, 1, 4)] = mw.getCurrentFrame():expandTemplate{
			title='Gray', args={v}}
	end

	for i = args.begin_year, (args.end_year or os.date('%Y')+args.interval), 
			args.interval do
		if absences[tostring(i)] then
			table.insert(appearances, absences[tostring(i)])
			absences[tostring(i)] = nil
		else
			table.insert(appearances, mw.ustring.format(
				'[[%s at the %s %s|%s]]', args.team, i, args.competition, i))
		end
	end
	return hlist(valueUnion(absences, appearances))
end

return p