Module:Rugby box

Jump to navigation Jump to search

Usage

See {{rugbybox}}



-- Implements [[Template:rugby box]]
local p = {}
local eventschema = "http://schema.org/SportsEvent"
local labels = {
	['aet'] = '[[Overtime (sports)#Rugby union|a.e.t.]]',
	['penalties'] = '[[Penalty shootout#Rugby union|Penalties]]',
	['attendance'] = 'Attendance:',
	['referee'] = 'Referee:',
	['try'] = 'Try:',
	['con'] = 'Con:',
	['pen'] = 'Pen:',
	['drop'] = 'Drop:',
	['gfm'] = 'Goal from mark:',
	['cards'] = 'Cards:'
}

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 getid(s)
	s = trim(s or '')
	if s and s ~= '' then
		return s
	end
	return nil
end

function p.main(frame)
	local getArgs = require('Module:Arguments').getArgs
	local args = getArgs(frame)
	local id = getid(args['id'])
	-- start box
	local root =
		mw.html.create('div')
			:attr('itemscope', '')
			:attr('itemtype', eventschema)
			:css('width', args['size'] or '100%')
			:css('background-color', args['bg'])
			:addClass('vevent summary')
			:attr('id', id)
	-- date/time/round
	local dtr = {}
	for _, v in pairs({'date', 'time', 'round'}) do
		if args[v] then
			table.insert(dtr, args[v])
		end
	end
	root:tag('table')
		:css('float', 'left')
		:css('width', args['cw1'] or '15%')
		:css('table-layout', 'fixed')
		:tag('tr')
			:tag('td')
				:css('text-align', 'right')
				:wikitext(table.concat(dtr, '<br/>'))
	local block = root:tag('table')
		:css('float', 'left')
		:css('width', args['cw2'] or '61%')
		:css('table-layout', 'fixed')
		:css('text-align', 'center')

	local row = block:tag('tr')
		:css('vertical-align', 'top')
		:css('font-weight', 'bold')
	row:tag('td')
		:css('width', '39%')
		:css('text-align', 'right')
		:addClass('vcard')
			:tag('span')
				:addClass('fn org')
				:wikitext(args['team1'] or args['home'] or args['nohome'])
	row:tag('td')
		:css('width', '22%')
		:wikitext(args['score'] or 'v')
		:wikitext(args['aet'] and ' (' .. labels['aet'] .. ')' or nil)
	row:tag('td')
		:css('width', '39%')
		:css('text-align', 'left')
		:addClass('vcard')
			:tag('span')
				:addClass('fn org')
				:wikitext(args['team2'] or args['away'] or args['noaway'])

	row = block:tag('tr')
		:css('font-size', '85%')
		:css('vertical-align', 'top')
	
	local score = {args['homescore'] or '', args['awayscore'] or ''}
	for k = 1,2 do
		if score[k] == '' then
			local vals = {}
			for _, v in pairs({'try', 'con', 'pen', 'drop', 'gfm', 'cards'}) do
				if args[v .. k] then
					table.insert(vals, '\'\'\'' .. labels[v] .. '\'\'\' ' .. args[v .. k])
				end
			end
			score[k] = table.concat(vals, '<br />')
		end
	end
	row:tag('td'):css('text-align', 'right'):wikitext(score[1])
	row:tag('td'):wikitext(args['report'])
	row:tag('td'):css('text-align', 'left'):wikitext(score[2])
	
	if args['penaltyscore'] then
		row = block:tag('tr')
		row:tag('th')
		row:tag('th'):wikitext(labels['penalties'])
		row:tag('th')
		row = block:tag('tr')
			:css('font-size', '85%')
			:css('vertical-align', 'top')
		row:tag('td'):css('text-align','right'):wikitext(args['penalties1'])
		row:tag('td'):css('font-weight','bold'):wikitext(args['penaltyscore'])
		row:tag('td'):css('text-align','left'):wikitext(args['penalties2'])
	end
	
	-- stadium/attendance/referee
	local extra = {}
	if args['stadium'] then
		table.insert(extra, '<span class="location">' .. args['stadium'] .. '</span>')
	end
	if args['attendance'] then
		table.insert(extra, labels['attendance'] .. ' ' .. args['attendance'])
	end
	if args['referee'] then
		table.insert(extra, labels['referee'] .. ' <span class="attendee">' .. args['referee'] .. '</span>')
	end
	root:tag('table')
		:css('float', 'left')
		:css('width', args['cw3'] or '24%')
		:css('table-layout', 'fixed')
		:tag('tr')
			:tag('td')
				:css('font-size', '85%')
				:wikitext(table.concat(extra, '<br/>'))
	root:tag('div'):css('clear', 'both')
				
	local check = require('Module:Check for unknown parameters')._check
	local tracking = check({
		['unknown']='[[Category:Pages using rugbybox with unknown parameters|_VALUE_ ]]',
		['preview']='Page using [[Template:Rugbybox]] with unknown parameter "_VALUE_"',
		['ignoreblank']='y',
		'aet', 'attendance', 'away', 'awayscore', 'bg', 'cards1', 'cards2', 
		'con1', 'con2', 'cw1', 'cw2', 'cw3', 'date', 'drop1', 'drop2', 
		'gfm1', 'gfm2', 'home', 'homescore', 'id', 'noaway', 'nohome', 
		'pen1', 'pen2', 'penalties1', 'penalties2', 'penaltyscore', 
		'referee', 'report', 'round', 'score', 'size', 'stadium', 
		'team1', 'team2', 'time', 'try1', 'try2'
	}, frame:getParent().args)
	return tostring(root) .. tracking
end

return p