Edge Rewrite
// HTMLRewriter · presentation

This page was redesigned at the edge.

Cloudflare fetched the original article and streamed it through HTMLRewriter to apply an entirely new visual system without rebuilding the source page.

Jump to content

Module:National squad

Permanently protected module
From Wikipedia, the free encyclopedia

-- This module implements [[Template:National squad]] and 
-- [[Template:National squad no numbers]] to avoid articles being added to 
-- [[:Category:Pages where template include size is exceeded]]
-- when the template is used many times.
local p = {}
local prefixes = {
	['male'] = ' men\'s',
	['men'] = ' men\'s',
	['mens'] = ' men\'s',
	['men\'s'] = ' men\'s',
	['female'] = ' women\'s',
	['women'] = ' women\'s',
	['womens'] = ' women\'s',
	['women\'s'] = ' women\'s',
	[''] = ''
}
-- Recognised sports
local sports = {
	['football'] = 'football',
	['soccer'] = 'football',                    -- alias
	['futsal'] = 'futsal',
	['basketball'] = 'basketball',
	['3x3 basketball'] = '3x3 basketball',
	['3x3'] = '3x3 basketball',                 -- alias
	['wheelchair basketball'] = 'wheelchair basketball',
	['cricket'] = 'cricket',
	['esports'] = 'esports',
	['field hockey'] = 'field hockey',
	['goalball'] = 'goalball',
	['golf'] = 'golf',
	['handball'] = 'handball',
	['pool'] = 'pool',
	['rugby'] = 'rugby',
	['softball'] = 'softball',
	['volleyball'] = 'volleyball',
	['water polo'] = 'water polo',
}
-- Other football codes / phrases that must NOT be treated as
-- association football when auto-detecting the sport from |team link=
local not_football = {
	'rugby', 'american football', 'canadian football', 'flag football',
	'gridiron', 'arena football', 'gaelic', 'australian rules', 'beach soccer',
}
local function link_is_football(teamlink)
	-- Used only when |sport= is unset, to decide whether to default to football.
	-- No team link -> yes (default). A team link naming a different
	-- football code -> no. Otherwise yes if it mentions football/soccer.
	if teamlink == nil or teamlink == '' then
		return true
	end
	local ll = mw.ustring.lower(teamlink)
	for _, term in ipairs(not_football) do
		if ll:find(term, 1, true) then
			return false
		end
	end
	return ll:find('football', 1, true) ~= nil or ll:find('soccer', 1, true) ~= nil
end
function p.main(frame)
	local getArgs = require('Module:Arguments').getArgs
	local args = getArgs(frame, {parentFirst = true})
	local country = args.country or '{{{country}}}'
	local coach_label = args.coach_type or 'Coach'
	local comp = args.comp or '{{{comp}}}'
	-- Resolve the sport. If |sport= is not set, default to
	-- football only if |team link= looks like association football
	-- (or there is no team link); otherwise leave the sport undetermined
	-- so football-specific colors/flagvar are not applied to other sports
	local rawsport = args.sport
	if rawsport == nil or rawsport == '' then
		local tl = args['team link']
		if tl and mw.ustring.lower(tl):find('futsal', 1, true) then
			rawsport = 'futsal'
		else
			rawsport = link_is_football(tl) and 'football' or ''
		end
	end
	local sport = sports[mw.ustring.lower(rawsport)]
	local knownsport = sport ~= nil
	if not knownsport then sport = rawsport end
	local gender = prefixes[args.gender or ''] or ''
	if sport == 'football' and gender == " men's" then
	    gender = ''
	end

	-- Centralized team colors for football from [[Module:Sports color/international football]]
	local bg = args.bg
	local fg = args.fg
	local bordercolor = args.bordercolor
	local colortracking = ''
	if sport == 'football' or sport == 'futsal' then
		local colorkey = args.colorvar or args.country
		if colorkey and colorkey ~= '' then
			local teamcolors
			local ok, colordata = pcall(mw.loadData, 'Module:Sports color/international football')
			if ok and type(colordata) == 'table' then
				local entry = colordata[colorkey]
				if type(entry) == 'string' then entry = colordata[entry] end  -- resolve alias
				if type(entry) == 'table' then teamcolors = entry end
			end
			if teamcolors then
				local function withhash(c) return '#' .. (tostring(c):gsub('^#', '')) end
				bg = withhash(teamcolors[1])
				fg = withhash(teamcolors[2])
				bordercolor = withhash(teamcolors[3])
			else
				colortracking = '[[Category:National squad templates using locally-defined colors for football]]'
			end
		end
	end

	local titlestyle = 'background-color:' .. (bg or 'transparent') .. ';'
		.. 'color:' .. (fg or 'inherit') .. ';' 
		.. 'border:1px solid ' .. (bordercolor or '#aaa')
	-- uses flagvar for given sport
	local flagvar = args.flagvar
	if flagvar == nil or flagvar == '' then
		flagvar = sport
	end
	local image = args.country ~= 'Unified Team' and require('Module:Flagg').luaMain(frame, {
		'cxxl',
		args.country or 'none',
		flagvar,
		size = '50px'
	}) or frame:expandTemplate{
		title = 'flagicon image', 
		args = {'Olympic flag.svg', size = '50px'}
	}

	local ospan = '<span style="color:' .. (fg or 'inherit') .. '">'
	local cspan = '</span>'	
	-- if |team link= not present, module will attempt to grab (if present)
	-- the sport's link alias field of the country's data template
	local teamlink = args['team link']
	if teamlink == nil or teamlink == '' then
		local cdparams = {}
		if gender ~= '' then cdparams.mw = gender end
		local data = require('Module:CountryData').gettable(frame, country, cdparams)
		local alias = data and data['link alias-' .. sport]
		teamlink = (alias and alias ~= '' and alias)
			or (country .. gender .. ' national ' .. sport .. ' team')
	end
	local title = string.format('[[%s|%s%s%s]] – [[%s|%s%s%s]]', 
		teamlink, 
		ospan, args.title or country .. ' squad', cspan, 
		args['comp link'] or comp, ospan, comp, cspan)
	
	local haspos = false
	
	-- Tracking and preview warnings
	local knownargs = {['bg']=1, ['fg']=1, ['bordercolor']=1, ['colorvar']=1, ['coach']=1, ['coach_type']=1,
		['comp']=1, ['comp link']=1, ['country']=1, ['flagvar']=1, ['gender']=1, 
		['list']=1, ['name']=1, ['nonumbers'] = 1, ['note']=1, ['sport']=1, ['team link']=1, ['title']=1}
	local badargs = {}
	local numlist = {}
	for k, v in pairs(args) do
		if knownargs[k] then
		elseif type(k) == 'string' then
			local n = tonumber(k:match('^p(%d+)$') or k:match('pos(%d+)') or '-1')
			if k:match('^p%d+$') and n >= 0 and n <= 99 then
				table.insert(numlist, k:match('^p(%d+)$'))
			elseif args.nonumbers and (k:match('^pos%d+$') and n >= 0 and n <= 99) then
				if v and v ~= '' then haspos = true end
			elseif v and v ~= '' then
				table.insert(badargs, k)
			end
		elseif v and v ~= '' then
			table.insert(badargs, k)
		end
	end
	table.sort(
		numlist,
		function (a, b) 
			return tonumber(a) < tonumber(b) or (tonumber(a) == tonumber(b) and #a > #b)
		end
	)
	local pv = require('Module:If preview')
	local preview, tracking, paramtracking = '', '', ''
	tracking = tracking .. colortracking
	if #badargs > 0 then
		for k, v in pairs(badargs) do
			if v == '' then	v = ' '	end
			v = mw.ustring.gsub(v, '[^%w\\-_ ]', '?')
			preview = preview .. pv._warning({
				'Page using national squad with unknown parameter "' .. v .. '".'
			})
			paramtracking = paramtracking .. '[[Category:Pages using national squad with unknown parameters|' .. v .. ']]'
		end
	end
	if (args['title'] == nil and args['team link'] == nil and args.country == nil) or args.comp == nil then
		paramtracking = paramtracking .. '[[Category:Pages using national squad with unknown parameters|!]]'
	end
	if not args['comp link'] then
		tracking = tracking .. '[[Category:Pages using national squad without comp link]]'
	end
	if not args['sport'] then
		if not args['team link'] then
			tracking = tracking .. '[[Category:Pages using national squad without sport or team link]]'
		end
	end
	if not knownsport then
		if not args['team link'] then
			local sortkey = mw.ustring.gsub(sport, '[^A-Za-z]', ' ')
			tracking = tracking .. '[[Category:Pages using national squad without team link and with an atypical sport|' .. sortkey .. ' ]]'
		end
	end
	if prefixes[args.gender or ''] == nil then
		tracking = tracking .. '[[Category:Pages using national squad with unsupported gender]]'
	end
	if args.name == nil then
		tracking = tracking .. '[[Category:Pages using national squad without name]]'
	end
	local ns = mw.title.getCurrentTitle().namespace
	if ns ~= 10 then tracking = '' end
	if ns ~= 0 and ns ~= 10 then paramtracking = '' end
	
	local list1 = args.list or ''
	if list1 == '' then
		for i,k in ipairs(numlist) do
			if args['p' .. k] then
				local n = args.nonumbers and (args['pos' .. k] or '') or tostring(k)
				if n ~= '' or haspos == true then
					list1 = list1 .. string.format(
						'*%s&nbsp;<span class="vcard agent"><span class="fn">%s</span></span>\n',
						n, args['p' .. k])
				else
					list1 = list1 .. string.format(
						'*<span class="vcard agent"><span class="fn">%s</span></span>\n', args['p' .. k])
				end
			end
		end
		if args.coach then
			list1 = list1 .. string.format(
				'*<span class="vcard agent">%s:&nbsp;<span class="fn">%s</span></span>',
				coach_label, args.coach)
		end
	end
	local list3 = args.note and ('' .. args.note .. '') or nil
	
	return require('Module:Navbox')._navbox({
		name = args.name,
		titlestyle = titlestyle, 
		listclass = 'hlist', bodyclass = 'vcard', titleclass = 'fn org',
		image = image, title = title, list1 = list1, list3 = list3
	}) .. tracking .. paramtracking .. preview
end

return p