Jump to content

Module:Football box collapsible: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
bypass check for most common case
No edit summary
Line 141: Line 141:
local id = trim(args['id'])
local id = trim(args['id'])
local bars = (args['nobars'] == nil) or (args['bars'] == '')
local bars = (args['nobars'] == nil) or (args['bars'] == '')
local class = trim(args['class'] or 'collapsible collapsed') or ''
local class = trim(args['class'] or 'mw-collapsible mw-collapsed') or ''
local width1 = {['1'] = '28%', ['1.1'] = '22%', ['1.2'] = '28%', ['default'] = '19%'}
local width1 = {['1'] = '28%', ['1.1'] = '22%', ['1.2'] = '28%', ['default'] = '19%'}
local width2 = {['1'] = '19%', ['1.1'] = '25%', ['1.2'] = '19%', ['default'] = '23%'}
local width2 = {['1'] = '19%', ['1.1'] = '25%', ['1.2'] = '19%', ['default'] = '23%'}

Revision as of 17:37, 17 October 2018

-- Implements [[Template:Football box collapsible]]
local p = {}
local result_modules = 'Module:Football box collapsible/result/'
local labels = {
	['penalties'] = '[[Penalty shoot-out (association football)|Penalties]]',
	['pen'] = '[[Penalty shoot-out (association football)|p]]',
	['aet'] = '[[Overtime (sports)#Association football|a.e.t.]]',
	['agg'] = '[[Playoff format#Total points series (aggregate)|agg.]]',
	['stadium'] = 'Stadium:', 
	['attendance'] = 'Attendance:',
	['referee'] = 'Referee:',
	['assistantreferees'] = 'Assistant referees:',
	['assistantreferee2'] = '<span style="visibility:hidden">Assistant referees:</span>',
	['fourthofficial'] = 'Fourth official:',
	['fifthofficial'] = 'Fifth official:',
	['goallineassistants'] = 'Goal-line assistants:',
	['goallineassistant2'] = '<span style="visibility:hidden">Goal-line assistants:</span>',
	['motm'] = 'Man of the Match:',
	['mvp'] = 'MVP:',
	['note'] = 'Note:',
	['seealso'] = 'See also:'
}
local tracking = ''

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 bold(s)
	if isnotempty(s) then
		return "'''" .. s .. "'''"
	end
	return ""
end

local function italic(s)
	if isnotempty(s) then
		return "''" .. s .. "''"
	end
	return ""
end

local function small(s)
	if isnotempty(s) then
		return '<span style="font-size:85%">' .. s .. '</span>'
	end
	return ''
end

local function nowrap(s)
	if isnotempty(s) then
		return '<span style="white-space:nowrap;display:inline">' .. s .. '</span>'
	end
	return ''
end

local function background(frame, c, r, bg)
	if c ~= 'default' then
		if not mw.title.new(result_modules .. c) or not mw.title.new(result_modules .. c).exists then
			c = 'default'
			tracking = tracking .. '[[Category:Pages using football box collapsible with unknown competition]]'
		end
	end
	local data = mw.loadData(result_modules .. c)
	local res = data[r:upper()]
	if res then
		res = '#' .. (data[res] or res)
	elseif isnotempty(bg) then
		res = '#' .. bg
	else
		res = 'transparent'
	end
	return res
end

local function teamname(team)
    local prefix, suffix, b = "", "", "'''";

    if mw.ustring.sub(team,1,3) ~= b and mw.ustring.sub(team,-3) ~= b then
        prefix = b .. '<span class="fn org">';
        suffix = "</span>" .. b;
    else
        if mw.ustring.sub(team,1,3) == b then
            prefix = '<span class="fn org">';
            team = mw.ustring.sub(team,4);
        else
            prefix = '<span class="fn org">' .. b;
        end

        if mw.ustring.sub(team,-3) == b then
            suffix = "</span>";
            team = mw.ustring.sub(team,1,mw.ustring.len(team)-3);
        else
            suffix = b .. "</span>";
        end
    end
        
    return prefix .. team .. suffix;
end

local function score(s, a, ag, p)
	s = nowrap(bold(s ~= '' and s or 'v'))
	if a ~= '' then
		s = s .. small(' (' .. labels['aet'] .. ')')
	end
	if ag ~= '' then
		s = s .. '<br>' .. small(' (' .. bold(ag) .. ' ' .. labels['agg'] .. ')')
	end
	if p ~= '' then
		s = s .. '<br>' .. small(' (' .. bold(p) .. ' ' .. labels['pen'] .. ')')
	end
	return s
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

function p.main(frame)
	local args = frame:getParent().args
	local id = trim(args['id'])
	local bars = (args['nobars'] == nil) or (args['bars'] == '')
	local class = trim(args['class'] or 'mw-collapsible mw-collapsed') or ''
	local width1 = {['1'] = '28%', ['1.1'] = '22%', ['1.2'] = '28%', ['default'] = '19%'}
	local width2 = {['1'] = '19%', ['1.1'] = '25%', ['1.2'] = '19%', ['default'] = '23%'}
	local width3 = {['1'] =  '8%', ['1.1'] =  '8%', ['1.2'] =  '8%', ['default'] = '12%'}
	local width4 = {['1'] = '19%', ['1.1'] = '19%', ['1.2'] = '26%', ['default'] = '23%'}
	local width5 = {['1'] = '26%', ['1.1'] = '26%', ['1.2'] = '19%', ['default'] = '23%'}
	local location = nil
	local extra = {}
	
	-- required parameters
	for i, k in ipairs({'team1', 'team2'}) do
		if args[k] == nil then args[k] = '{{{' .. k .. '}}}' end
	end
	-- vcard for stadium
	if isnotempty(args['stadium']) then
		args['stadium'] = tostring(mw.html.create('span'):addClass('location'):wikitext(args['stadium']))
	end

	-- placement of stadium and location information
	if isnotempty(args['location']) then
		location = args['location']
		if isnotempty(args['stadium']) then
			table.insert(extra, labels['stadium'] .. ' ' .. args['stadium'])
		end
	elseif isnotempty(args['stadium']) then
		location = args['stadium']
	end

	-- remaining extra information
	for i, k in ipairs({'attendance', 'referee', 'assistantreferees', 
		'assistantreferee2', 'fourthofficial', 'fifthofficial', 
		'goallineassistants', 'goallineassistant2', 'motm', 'mvp'}) do
		if isnotempty(args[k]) then
			table.insert(extra, labels[k] .. ' ' .. args[k])
		end
	end
	
	-- Start box
	local root = 
		mw.html.create('div')
			:addClass('vevent')
			:attr('id', id)
	root:tag('span')
		:addClass('summary')
		:css('display', 'none')
		:wikitext(args['team1'] .. ' v ' .. args['team2'])
	root:newline()
	
	-- Start table
	local rtable = root:tag('table')
		:attr('cellspacing', 0)
		:addClass(class)
		:css('border-top', bars and '1px solid #999' or nil)
		:css('border-top', bars and '1px solid #999' or nil)
		:css('margin-bottom', bars and '-1px' or nil)
		:css('width', args['size'] or '100%')
		:css('background-color', background(frame, args['competition'] or 'default', args['result'] or '', args['bg'] or ''))

	local row = rtable:tag('tr'):css('vertical-align', 'top')
	row:newline()
	-- date and round
	local cell = row:tag('td')
		:css('width', width1[args['format'] or 'default'] or width1['default'])
	if isnotempty(args['date']) then
		cell:tag('span')
			:css('white-space', 'nowrap')
			:css('float', 'right')
			:css('margin-left', '0.5em')
			:wikitext(args['date'])
		cell:wikitext(' ')
	end
	if isnotempty(args['round']) then
		cell:tag('span')
			:css('font-size', '85%')
			:wikitext(args['round'])
	end
	row:newline()
	-- team1
	row:tag('td')
		:css('width', width2[args['format'] or 'default'] or width2['default'])
		:css('text-align', 'right')
		:addClass('vcard attendee')
		:wikitext(teamname(args['team1']))
	row:newline()
	-- score
	row:tag('td')
		:css('width', width3[args['format'] or 'default'] or width3['default'])
		:css('text-align', 'center')
		:wikitext(score(args['score'] or '', args['aet'] or '', args['aggregatescore'] or '', args['penaltyscore'] or ''))
	row:newline()
	-- team2
	row:tag('td')
		:css('width', width4[args['format'] or 'default'] or width4['default'])
		:addClass('vcard attendee')
		:wikitext(teamname(args['team2']))
	row:newline()
	-- location, stadium
	row:tag('td')
		:css('width', width5[args['format'] or 'default'] or width5['default'])
		:css('font-size', '85%')
		:wikitext(location)

	local row = rtable:tag('tr'):css('vertical-align','top'):css('font-size','85%')
	row:newline()
	-- time
	row:tag('td')
		:css('text-align', 'right')
		:wikitext(args['time'])
	row:newline()	
	-- goals1
	row:tag('td')
		:css('text-align', 'right')
		:wikitext(fmtlist(args['goals1']))	
	row:newline()
	-- report
	row:tag('td')
		:css('text-align', 'center')
		:wikitext(makelink(args['report'] or '', 'Report'))
	row:newline()
	-- goals2
	row:tag('td')
		:wikitext(fmtlist(args['goals2']))
	row:newline()
	-- location, stadium
	row:tag('td')
		:wikitext(table.concat(extra, '<br>'))

	if isnotempty(args['penalties1']) or isnotempty(args['penalties2']) then
		row = rtable:tag('tr')
		row:tag('td'):attr('rowspan', 2)
		row:newline()
		row:tag('td'):attr('colspan', 3):css('text-align', 'center'):wikitext(bold(labels['penalties']))
		row:newline()
		row:tag('td'):attr('rowspan', 2)
		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('td')
			:wikitext('&nbsp;')
		row:newline()
		row:tag('td')
			:wikitext(fmtlist(args['penalties2']))
		row:newline()
	end
	notes = {}
	if isnotempty(args['note']) then
		table.insert(notes, "''" .. labels['note'] .. " " .. args['note'] .. "''")
	end
	if isnotempty(args['seealso']) then
		table.insert(notes, "''" .. labels['seealso'] .. " " .. args['seealso'] .. "''")
	end
	if #notes > 0 then
		row = rtable:tag('tr')
		row:newline()
		row:tag('td')
			:attr('colspan', 5)
			:css('text-align', 'left')
			:css('font-size', '85%')
			:wikitext(table.concat(notes, '<br>'))
		row:newline()
	end
	return tostring(root) .. tracking
end

return p