Modul:National squad


-- 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'] = ' lelaki',
	['men'] = ' lelaki',
	['mens'] = ' lelaki',
	['men\'s'] = ' lelaki',
	['female'] = ' wanita',
	['women'] = ' wanita',
	['womens'] = ' wanita',
	['women\'s'] = ' wanita',
	[''] = ''
}
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 'Jurulatih'
	local comp = args.comp or '{{{comp}}}'
	local sport = args.sport or 'bola sepak'
	local gender = prefixes[args.gender or ''] or ''
	local titlestyle = 'background-color:' .. (args.bg or 'transparent') .. ';'
		.. 'color:' .. (args.fg or 'inherit') .. ';' 
		.. 'border: 1px solid ' .. (args.bordercolor or '#aaa') .. ';'
	local image = args.country ~= 'Unified Team' and frame:expandTemplate{
		title = 'flagicon', 
		args = {args.country or 'none', args.flagvar or '', size = '50px'}
	} or frame:expandTemplate{
		title = 'flagicon image', 
		args = {'Olympic flag.svg', size = '50px'}
	}

	local ospan = '<span style="color:' .. (args.fg or 'inherit') .. '">'
	local cspan = '</span>'	
	local title = string.format('[[%s|%s%s%s]] – [[%s|%s%s%s]]', 
		args['team link'] or ('Pasukan ' .. sport .. gender .. ' kebangsaan ' .. country), 
		ospan, args.title or 'Skuad ' .. country, cspan, 
		args['comp link'] or comp, ospan, comp, cspan)
	
	local haspos = false
	
	-- Tracking and preview warnings
	local knownargs = {['bg']=1, ['fg']=1, ['bordercolor']=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 = '', ''
	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({
				'Laman menggunakan skuad kebangsaan dengan parameter "' .. v .. '" yang tidak diketahui.'
			})
			tracking = tracking .. '[[Kategori:Halaman menggunakan skuad kebangsaan dengan parameter yang tidak diketahui|' .. v .. ']]'
		end
	end
	if (args['title'] == nil and args['team link'] == nil and args.country == nil) or args.comp == nil then
		tracking = tracking .. '[[Kategori:Halaman menggunakan skuad kebangsaan dengan parameter yang tidak diketahui|!]]'
	end
	if not args['comp link'] then
		tracking = tracking .. '[[Kategori:Halaman menggunakan skuad kebangsaan tanpa pautan comp]]'
	end
	if not args['sport'] then
		if not args['team link'] then
			tracking = tracking .. '[[Kategori:Halaman menggunakan skuad kebangsaan tanpa pautan sukan atau pasukan]]'
		end
	end
	if sport ~= 'bola sepak' and sport ~= 'futsal' and sport ~= 'bola keranjang' then
		if not args['team link'] then
			local sortkey = mw.ustring.gsub(sport, '[^A-Za-z]', ' ')
			tracking = tracking .. '[[Kategori:Halaman menggunakan skuad kebangsaan tanpa pautan pasukan dan dengan sukan yang tidak tipikal|' .. sortkey .. ' ]]'
		end
	end
	if prefixes[args.gender or ''] == nil then
		tracking = tracking .. '[[Kategori:Halaman menggunakan skuad kebangsaan dengan jantina tidak disokong]]'
	end
	if args.name == nil then
		tracking = tracking .. '[[Kategori:Halaman menggunakan skuad kebangsaan tanpa nama]]'
	end
	-- if tracking ~= '' and mw.title.getCurrentTitle().namespace > 0 then tracking = '' 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 .. preview
end

return p