Module:Rugby box collapsible

-- 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, other)
	if not score then
		local details = {}
		for i, k in ipairs({'try', 'con', 'pen', 'drop', 'cards'}) do
			if other[k] then
				table.insert(details, "'''" .. labels[k] .. "''' " .. other[k])
			end
		end
		return table.concat(details, '<br />')
	end
	return score or ''
end

local function getextra(attendance, referee)
	local extra = {}
	if attendance then
		table.insert(extra, labels['attendance'] .. ' ' .. attendance)
	end
	if referee then
		table.insert(extra, labels['referee'] .. ' ' .. referee)
	end
	return table.concat(extra, '<br />')
end

function p.main(frame)
	local getArgs = require('Module:Arguments').getArgs
	local args = getArgs(frame)
	-- Defaults and arg processing
	local class = trim(args.class or 'mw-collapsible mw-collapsed')
	local width = trim(args.width or '100%')
	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 = args['team1'] and args['team1'] or args['home'] or ''
	local team2 = args['team2'] and args['team2'] or args['away'] or ''
	local score = args['score'] and args['score'] or 'v'
	if args['score'] and 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']})
	local extra = getextra(args['attendance'], args['referee'])

	-- Styles
	local small = 'font-size:85%;'
	local left = 'text-align:left;'
	local right = 'text-align:right;'
	local center = 'text-align:center;'
	
	-- 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 args['nobars']) and (not args['stack'])) then
		table.insert(t, 'border-bottom: 1px solid #e0e0e0;')
	end
	if isnotempty(width) then
		table.insert(t, 'width: ' .. width .. ';')
	end
	table.insert(t, 'color:inherit;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 .. ';' .. left .. small .. '"'
		.. ' | ' .. (args['note'] or '') .. '\n')
	table.insert(t, '| style="width:' .. date_width .. ';' .. right .. small .. '"'
		.. ' | ' .. (args['date'] or '') .. '\n')
	table.insert(t, '| style="width:' .. team_width .. ';' .. right .. '"'
		.. ' | ' .. team1 .. '\n')
	table.insert(t, '| style="width:' .. score_width .. ';' .. center .. 'font-weight:bold;"'
		.. ' | ' .. score .. '\n')
	table.insert(t, '| style="width:' .. team_width .. ';' .. left .. '"'
		.. ' | ' .. team2 .. '\n')
	table.insert(t, '| style="width:' .. stadium_width .. ';' .. small .. '"'
		.. ' | ' .. (args['stadium'] or '') .. '\n')
	table.insert(t, '! style="width:4%" rowspan="2" |&nbsp;\n') -- show/hide spacer
	-- Second row
	table.insert(t, '|- style="vertical-align:top;' .. small .. '"\n')
	table.insert(t, '| style="' .. left .. '" | ' 
		.. (args['note2'] or '') .. '\n')
	table.insert(t, '| style="' .. right .. '" | ' 
		.. (args['time'] or '') .. '\n')
	table.insert(t, '| style="' .. right .. '" | ' 
		.. (score1 or '') .. '\n')
	table.insert(t, '| style="' .. center .. '" | ' 
		.. (args['report'] or '') .. '\n')
	table.insert(t, '| style="' .. left .. '" | ' 
		.. (score2 or '') .. '\n')
	table.insert(t, '| rowspan="2" style="' .. left .. '" | '
		.. (extra or '') .. '\n')
	table.insert(t, '|}')
	return table.concat(t)
end

return p