Module:Football box

Revision as of 17:39, 17 October 2018 by en>Plastikspork (Protected "Module:Football box": High-risk Lua module ([Edit=Require template editor access] (indefinite) [Move=Require template editor access] (indefinite)))
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Documentation for this module may be created at Module:Football box/doc

Script error: Lua error: Internal error: The interpreter exited with status 127.

-- Implements [[Template:Football box]]
local p = {}
local lang = nil
local delink = require('Module:Delink')._delink
local eventschema = "http://schema.org/SportsEvent"
local teamschema = "http://schema.org/SportsTeam"
local placeschema = "http://schema.org/Place"
local penalties = '[[Penalty shoot-out (association football)|Penalties]]'
local aet = '[[Overtime (sports)#Association football|a.e.t.]]'
local attendance = 'Attendance:'
local referee = 'Referee:'

local function timestamp(d, t)
	if d then
		lang = lang or mw.language.getContentLanguage() -- lazy initialize
		local success, timestamp = pcall(lang.formatDate, lang, 'c', delink({d .. ' ' .. (t or '')}))
		if success then
			return timestamp
		else
			return nil
		end
	end
	return nil
end

local function fmtlist(s)
	s = mw.ustring.gsub(s or '', '%[%[ *([%?-]) *%]%]', '%1')
	s = mw.ustring.gsub(s, '%[%[ *[%?-] *| *(.-) *%]%]', '%1')
	if mw.ustring.sub(s, 1, 1) == '*' then
		return tostring(mw.html.create('div'):addClass('plainlist'):newline():wikitext(s))
	end
	return s
end

local function makelink(s,t)
	if s:match('^http') then
		return '[' .. s .. ' ' .. t .. ']'
	end
	return s
end

local function trim(s)
	return s:match('^[\'"%s]*(.-)[\'"%s]*$')
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 args = require('Module:Arguments').getArgs(frame)
	local id = getid(args['id'])
	local d = timestamp(args['date'], args['time'])
	local block

	local score = 'v'
	if args['score1'] or args['score2'] then
		score = (args['score1'] or '0') .. '–' .. (args['score2'] or '0')
	elseif args['score'] and args['score'] ~= '' then
		score = args['score']
	end
	if args['aet'] then
		score = score .. ' (' .. aet .. ')'
	end
	
	-- Start box
	local root = 
		mw.html.create('div')
			:attr('itemscope', '')
			:attr('itemtype', eventschema)
			:css('width', args['size'])
			:css('background-color', args['bg'])
			:css('clear', 'both')
			:css('overflow', 'auto')
			:attr('id', id)
	root:newline()
	
	if args['event'] then
		root:tag('div')
			:css('text-align', 'center')
			:css('font-weight', 'bold')
			:wikitext(args['event'])
	end
	
	-- Start left block
	block = root:tag('div')
		:addClass('mobile-float-reset')
		:css('float', 'left')
		:css('width', '15%')
		:css('padding', '2px 0')
		:css('overflow', 'auto')
	
	local timetag = block:tag('time')
		:attr('itemprop', d and 'startDate' or nil)
		:attr('datetime', d)
		:css('display', 'block')
		:css('overflow', 'auto')
		
	timetag:tag('span')
		:addClass('mobile-float-reset')
		:css('display', 'block')
		:css('float', 'right')
		:wikitext(args['date'])
	
	if args['time'] then
		timetag:tag('span')
			:addClass('mobile-float-reset')
			:css('display', 'block')
			:css('clear', 'right')
			:css('float', 'right')
			:wikitext(args['time'])
	end
	
	if args['round'] then
		block:tag('div')
			:addClass('mobile-float-reset')
			:css('clear', 'right')
			:css('float', 'right')
			:wikitext(args['round'])
	end
	-- End block
	
	-- Start table
	local rtable = root:tag('table')
		:css('float', 'left')
		:css('width', '61%')
		:css('table-layout', 'fixed')
		:css('text-align', 'center')
	local row = rtable:tag('tr')
		:attr('itemprop', 'name')
		:css('vertical-align', 'top')
	row:newline()
	row:tag('th')
		:css('width', '39%')
		:css('text-align', 'right')
		:attr('itemprop', 'homeTeam')
		:attr('itemscope', '')
		:attr('itemtype', teamschema)
		:tag('span')
			:attr('itemprop', 'name')
			:wikitext(args['team1'])
	row:tag('th')
		:css('width', '22%')
		:wikitext(score)
	row:tag('th')
		:css('width', '39%')
		:css('text-align', 'left')
		:attr('itemprop', 'awayTeam')
		:attr('itemscope', '')
		:attr('itemtype', teamschema)
		:tag('span')
			:attr('itemprop', 'name')
			:wikitext(args['team2'])

	row = rtable:tag('tr')
		:css('vertical-align', 'top')
		:css('font-size', '85%')
		:newline()
	row:tag('td')
		:css('text-align', 'right')
		:wikitext(fmtlist(args['goals1']))
	row:newline()
	row:tag('td')
		:wikitext(makelink(args['report'] or '', 'Report'))
	row:newline()
	row:tag('td')
		:css('text-align', 'left')
		:wikitext(fmtlist(args['goals2']))
	row:newline()	
	
	if args['penaltyscore'] then
		rtable
			:tag('tr')
				:tag('th')
					:attr('colspan', 3)
					:wikitext(penalties)
		row = rtable:tag('tr')
			:css('vertical-align', 'top')
			:css('font-size', '85%')
		row:newline()
		row:tag('td')
			:css('text-align', 'right')
			:wikitext(fmtlist(args['penalties1']))
		row:newline()
		row:tag('th')
			:wikitext(args['penaltyscore'])
		row:newline()
		row:tag('td')
			:css('text-align', 'left')
			:wikitext(fmtlist(args['penalties2']))
		row:newline()
	end
	-- End table
	
	-- Start right block
	block = root:tag('div')
		:addClass('mobile-float-reset')
		:css('float', 'left')
		:css('font-size', '85%')
		:css('width', '24%')
		:css('padding', '2px 0')
	
	if args['stadium'] then
		local sdiv = block:tag('div')
			:attr('itemprop', 'location')
			:attr('itemscope', '')
			:attr('itemtype', placeschema)
		if args['location'] then
			sdiv:tag('span')
				:attr('itemprop', 'name')
				:wikitext(args['stadium'])
			sdiv:wikitext(', ')
			sdiv:tag('span')
				:attr('itemprop', 'address')
				:wikitext(args['location'])
		else
			sdiv:tag('span')
				:attr('itemprop', 'name address')
				:wikitext(args['stadium'])
		end
	end
	
	if args['attendance'] then
		block:tag('div'):wikitext(attendance ..' ' .. args['attendance'])
	end
	if args['referee'] then
		block:tag('div'):wikitext(referee .. ' ' .. args['referee'])
	end

	return tostring(root)
end

return p