Jump to content

Module:PHL sports team

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by McVahl (talk | contribs) at 08:46, 21 February 2020 (Created page with 'require('Module:No globals') local p = {} local constants = { CODE_LEN = 3, SPACE = ' ', SHORT = 'short' } local function stripwhitespace(text) return tex...'). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

require('Module:No globals')

local p = {}
local constants = {	CODE_LEN = 3, SPACE = ' ', SHORT = 'short' }

local function stripwhitespace(text) return text:match("^%s*(.-)%s*$") end

local function load_data(args)
	local data_module = 'Module:PHL sports team/' .. (args['level'] or 'collegiate')
	return mw.loadData(data_module)
end

local function get_key_by_code(code, data)
	for k,v in pairs(data) do
		if v[1]==code then return k end
	end
	return nil
end

local function get_icon(team, size, alt_team)
	local icon = '[[File:%s colors.svg|%s|border|%s school colors|link=]]'
	return string.format(icon, team, size or '11px', alt_team or team)
end

local function add_link(team, name, div, text)
	if (type(div) == 'number' and div >= 3 and div <= 6) then
		if (name ~= nil) then return '[[' .. team[div] .. '|' .. text .. ']]' else return '[[' .. text .. ']]' end
	end
	return '[[' .. team[3] .. '|' .. text .. ']]'
end

function p.main(frame)
	local args = (frame.args[1] ~= nil) and frame.args or frame:getParent().args
	local data = load_data(args)
	local in_team = stripwhitespace(args[1] or '')
	local in_div = stripwhitespace(args[2] or '')
	local in_name = args['name']
	local out, key
	
	if (in_team == '' or nil) then return '—' end
	if (string.len(in_team) == constants.CODE_LEN) then key = get_key_by_code(in_team, data) end
	
	local team = data[in_team] or data[key]
	if (team == nil) then return error(string.format('Invalid team: %s', in_team)) end
	
	if (in_div ~= constants.SHORT) then
		local div = { men = 3, women = 4, junior = 5, boys = 5, girls = 6 }
		in_div = div[in_div] or tonumber(in_div:match('[2-6]') or '3')
		out = in_name or team[in_div]
		if (out == nil) then return error('No team') end
	else
		out = team[7] or key or in_team
	end
	
	out = add_link(team, in_name, in_div, out)

	local yesno = require('Module:Yesno')
	if yesno(args['icon'] or 'y') then
		local icon = get_icon(key or in_team, args['iconsize'])
		out = yesno(args['rt'] or 'n') and out .. constants.SPACE .. icon or icon .. constants.SPACE .. out
	end
	
	return out
end

function p.rt(frame)
	frame.args['rt'] = 'y'
	return frame:preprocess(p.main(frame))
end

function p.name(frame)
	frame.args['icon'] = 'n'
	return frame:preprocess(p.main(frame))
end

function p.colors(frame)
	local key
	local data = load_data(args)
	local in_team = frame.args[1]
	if (string.len(in_team) == constants.CODE_LEN) then key = get_key_by_code(in_team, data) end
	if ((data[in_team] or data[key]) == nil) then return '—' end
	return frame:preprocess(get_icon(key or in_team, frame.args[2], frame.args[3]))
end

return p