Jump to content

Module:Rugby box collapsible

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Frietjes (talk | contribs) at 18:13, 18 February 2022. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
-- Implements [[Template:Rugbybox collapsible]]
local p = {}

local labels = {
	['try'] = "Try:",
	['con'] = "Con:",
	['pen'] = "Pen:",
	['drop'] = "Drop:",
	['cards'] = "Cards:",
	['aet'] = "([[Overtime (sports)#Rugby union|a.e.t.]])",
	['attendance'] = "Attendance:",
	['referee'] = "Referee:"
}
local colors = {
	["W"] = "CCFFCC",
	["L"] = "FFCCCC",
	["T"] = "FFFFCC", 
	["D"] = "FFFFCC",
	["V"] = "CCCCCC"
}

local function isnotempty(s)
	return s and s:match( '^%s*(.-)%s*$' ) ~= ''
end

local function trim(s)
	if isnotempty(s) then
		s = s:match('^[\'"%s]*(.-)[\'"%s]*$')
		return isnotempty(s) and s or nil
	end
	return nil
end

local function getbackground(result, bg)
	result = result or ''
	local color = colors[result:upper()]
	if color then
		color = '#' .. color
	elseif isnotempty(bg) then
		color = '#' .. bg
	else
		color = 'transparent'
	end
	return color
end

local function getscore(score, try, con, pen, drop, cards)
	if not isnotempty(score) then
		local scoretable = {}
		for i, k in ipairs({'try', 'con', 'pen', 'drop', 'cards'}) do
			if isnotempty(extra[k]) then
				table.insert(scoretable, labels[k] .. ' ' .. extra[k])
			end
		end
		return table.concat(scoretable)
	end
	return score or ''
end

function p.main(frame)
	local args = (frame.args['team1'] or frame.args['team2']) and frame.args or frame:getParent().args
	-- Defaults
	local class = trim(args.class or 'mw-collapsible mw-collapsed')
	local width = trim(args.width or '100%')
	local small = 'font-size:85%;'
	local note_width = trim(args['note-width'] or '8%')
	local date_width = trim(args['date-width'] or '15%')
	local team_width = trim(args['team-width'] or '22%')
	local score_width = trim(args['score-width'] or '11%')
	local stadium_width = trim(args['stadium-width'] or '18%')
	local team1 = (isnotempty(args['team1']) and args['team1']) or args['home'] or ''
	local team2 = (isnotempty(args['team2']) and args['team2']) or args['away'] or ''
	local score = (isnotempty(args['score']) and args['score']) or 'v'
	if isnotempty(args['aet']) then
		score = score .. ' ' .. labels['aet']
	end
	local score1 = getscore(args['homescore'], {
		['try'] = args['try1'],
		['con'] = args['con1'],
		['pen'] = args['pen1'],
		['drop'] = args['drop1'],
		['cards'] = args['cards1']})
	local score2 = getscore(args['awayscore'], {
		['try'] = args['try2'],
		['con'] = args['con2'],
		['pen'] = args['pen2'],
		['drop'] = args['drop2'],
		['cards'] = args['cards2']})
	-- Start box
	local t = {}
	table.insert(t, '{| cellspacing=0')
	if isnotempty(class) then
		table.insert(t, ' class="' .. class .. '"')
	end
	-- Start table style
	table.insert(t, 'style="')
	if (not isnotempty(args.nobars)) then
		table.insert(t, 'border-top: 0px solid 000000;')
		if (not isnotempty(args.stack)) then
			table.insert(t, 'border-bottom: 1px solid #e0e0e0;')
		end
	end
	if isnotempty(width) then
		table.insert(t, 'width: ' .. width .. ';')
	end
	table.insert(t, 'background:' .. getbackground(args.result, args.bg) .. '"\n')
	-- End table style
	-- First row
	table.insert(t, '|- style="vertical-align:top;"\n')
	table.insert(t, '| style="width:' .. note_width .. ';text-align:left;' .. small .. '"'
		.. ' | ' .. (args['note'] or '') .. '\n')
	table.insert(t, '| style="width:' .. date_width .. ';text-align:right;' .. small .. '"'
		.. ' | ' .. (args['date'] or '') .. '\n')
	table.insert(t, '| style="width:' .. team_width .. ';text-align:right;"'
		.. ' | ' .. team1 .. '\n')
	table.insert(t, '| style="width:' .. score_width .. ';text-align:center;font-weight:bold;"'
		.. ' | ' .. score .. '\n')
	table.insert(t, '| style="width:' .. team_width .. ';text-align:left;"'
		.. ' | ' .. team2 .. '\n')
	table.insert(t, '| style="width:' .. stadium_width .. ';' .. small .. '"'
		.. ' | ' .. (args['stadium'] or '') .. '\n')
	table.insert(t, '| style="width:4%" rowspan="2" | \n')
	-- Second row
	table.insert(t, '|- style="vertical-align:top;' .. small .. '"\n')
	table.insert(t, '| style="text-align:left;" | ' 
		.. (args['note2'] or '') .. '\n')
	table.insert(t, '| style="text-align:right;" | ' 
		.. (args['time'] or '') .. '\n')
	table.insert(t, '| style="text-align:right;" | ' 
		.. (score1 or '') .. '\n')
	table.insert(t, '| style="text-align:center;" | ' 
		.. (args['report'] or '') .. '\n')
	table.insert(t, '| style="text-align:left;" | ' 
		.. (score2 or '') .. '\n')
	table.insert(t, '| style="text-align:left;" | ')
	if isnotempty(args['attendance']) then
		table.insert(t, labels['attendance'] .. ' ' .. args['attendance'])
	end
	if isnotempty(args['referee']) then
		if isnotempty(args['attendance']) then
			table.insert('<br />')
		end
		table.insert(t, labels['referee'] .. ' ' .. args['referee'])
	end
	table.insert(t, '\n|}')
	return table.concat(t)
end

return p