Modul:Football box collapsible

-- Implementasi [[Templat:Ksb dilipat]]
local p = {}

local labels = {
	['penalties'] = '[[Adu penalti]]',
	['shootout'] = '[[Adu penalti#Uji coba di Amerika Utara|Adu penalti]]',
	['pen'] = '[[Adu penalti|a.p.]]',
	['so'] = '[[Adu penalti|a.p.]]',
	['aet'] = '[[Perpanjangan waktu (sepak bola)|p.w.]]',
	['agg'] = '<abbr title="Skor agregat">agg.</abbr>',
	['stadium'] = 'Stadion:', 
	['attendance'] = 'Penonton:', ['penonton'] = 'Penonton:',
	['referee'] = 'Wasit:', ['wasit'] = 'Wasit:',
	['assistantreferees'] = 'Asisten wasit:', ['asistenwasit1'] = 'Asisten wasit:',
	['assistantreferee2'] = '<span style="visibility:hidden">Asisten wasit:</span>',['asistenwasit2'] = '<span style="visibility:hidden">Asisten wasit:</span>',
	['fourthofficial'] = 'Wasit keempat:', ['wasitkeempat'] = 'Wasit keempat:',
	['fifthofficial'] = 'Wasit kelima:', ['wasitkelima'] = 'Wasit kelima:',
	['goallineassistants'] = 'Wasit gawang:',
	['goallineassistant2'] = '<span style="visibility:hidden">Wasit gawang:</span>',
	['motm'] = 'Pemain terbaik:',
	['potm'] = 'Pemain terbaik:',
	['mvp'] = '<abbr title="Pemain paling berharga">MVP</abbr>:',
	['note'] = 'Catatan:',
	['seealso'] = 'Lihat juga:'
}
local colors = {
	["M"] = "W", ["W"] = "BBF3BB",  	--menang
	["K"] = "L", ["L"] = "FFBBBB",		--kalah
	["I"] = "D", ["S"] = "D", ["T"] = "D", ["D"] = "FFFFBB",     --imbang/seri
	["SW"] = "PW", ["PW"] = "BBF3FF",
	["DT"] = "P", ["V"] = "P", ["P"] = "BBBBBB"		--ditunda
}

local tracking, preview

local function checkarg(k,v)
	if k and type(k) == 'string' then
		if k == 'id' or k == 'team1' or k == 'tim1' or k == 'team2' or k == 'tim2' or k == 'class' or 
			k == 'nobars' or k == 'size' or 
			k == 'result' or k == 'hasil' or k == 'bg' or k == 'format' or k == 'date' or k == 'tanggal' or k == 'scorenote' or
			k == 'round' or k == 'ronde' or k == 'putaran' or k == 'score' or k == 'skor' or k == 'aet' or k == 'aggregatescore' or k == 'agregat' or 
			k == 'penaltyscore' or k == 'skorpenalti' or k == 'location' or k == 'lokasi' or k == 'stadium' or k == 'stadion' or 
			k == 'time' or k == 'waktu' or k == 'goals1' or k == 'gol1' or k == 'report' or k == 'laporan' or k == 'goals2' or k == 'gol2' or 
			k == 'attendance' or k == 'penonton' or k == 'referee' or k == 'wasit' or k == 'assistantreferees' or k == 'asistenwasit1' or
			k == 'assistantreferee2' or k == 'asistenwasit2' or k == 'fourthofficial' or k == 'wasitkeempat' or 
			k == 'fifthofficial' or k == 'wasitkelima' or k == 'goallineassistants' or 
			k == 'goallineassistant2' or k == 'potm' or k == 'motm' or k == 'mvp' or 
			k == 'penalties1' or k == 'penalti1' or k == 'penalties2' or k == 'penalti2' or k == 'note' or k == 'catatan' or 
			k == 'shootoutscore' or k == 'shootout1' or k == 'shootout2' or
			k == 'seealso' or k == 'lihatjuga' or k == 'stack' or k == 'section' or k == 'transcludedsection' then
			-- valid and not tracked
		else
			-- invalid
			local vlen = mw.ustring.len(k)
			k = mw.ustring.sub(k, 1, (vlen < 25) and vlen or 25) 
			k = mw.ustring.gsub(k, '[^%w%-_ ]', '?')
			table.insert(tracking, '[[Kategori:Halaman menggunakan modul football box collapsible dengan parameter asing|' .. k .. ']]')
			table.insert(preview, '"' .. k .. '"')
		end
	end
end

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 '<small>' .. s .. '</small>'
	end
	return ''
end

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

local function background(frame, c, r, bg)
	local data = colors
	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 = '<span class="fn org">' .. b .. ' ';
        suffix = ' ' .. b .. "</span>";
    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, so)
	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'] .. ')')
	elseif so ~= '' then
		s = s .. '<br>' .. small(' (' .. bold(so) .. ' ' .. labels['so'] .. ')')
	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
		-- could also expand plainlist here
		return mw.getCurrentFrame():extensionTag{
			name = 'templatestyles', args = { src = 'Plainlist/styles.css' }
		} .. tostring(  mw.html.create('div'):addClass('plainlist'):newline():wikitext(s))
	end
	return s
end

local function makelink(s,t)
	if s:match('^[Hh][Tt][Tt][Pp][Ss]?:[^ ]*$') then
		return '[' .. s .. ' ' .. t .. ']'
	end
	return s
end

function p.main(frame)
	-- Exit early if we are using section transclusion for a different section
	local tsection = frame:getParent().args['transcludesection'] or frame:getParent().args['section'] or ''
	local bsection = frame.args['section'] or ''
	if( tsection ~= '' and bsection ~= '' ) then
		if( tsection ~= bsection ) then
			return ''
		end
	end

	local args = (frame.args['team1'] or frame.args['team2'] or frame.args['tim1'] or frame.args['tim2']) and frame.args or 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 = {}
	local notes = {}
	
	tracking, preview = {}, {}
    for k, v in pairs(args) do
    	if v ~= '' then
    		checkarg(k,v)
    	end
	end
	
	if args['class'] then
		if args['class'] == 'collapsible' or args['class'] == 'mw-collapsible' or args['class'] == 'uncollapsed' then 
			--valid
		elseif args['class'] == '' then
			table.insert(tracking,'[[Kategori:Halaman menggunakan modul football box collapsible dengan class parameter kosong]]')
		else
			table.insert(tracking,'[[Kategori:Halaman menggunakan modul football box collapsible dengan class parameter]]')
		end
	end
	
	if isnotempty(args['stack']) then
		table.insert(tracking, '[[Kategori:Halaman menggunakan modul football box collapsible dengan parameter tidak didukung]]')
	end
	
	-- parameter utama yang dibutuhkan
	for i, k in ipairs({'team1', 'team2', 'tim1', 'tim2'}) do
		if args[k] == nil then args[k] = '{{{' .. k .. '}}}' end
	end
	-- vcard untuk stadion
	if isnotempty(args['stadium']) or isnotempty(args['stadion']) then
		args['stadium'] = tostring(mw.html.create('span'):addClass('location'):wikitext(args['stadium'] or args['stadion']))
	end

	-- penempatan informasi stadion dan lokasi
	if isnotempty(args['location']) or isnotempty(args['lokasi']) then
		location = args['location'] or args['lokasi']
		if isnotempty(args['stadium'] or args['stadion']) then
			table.insert(extra, labels['stadium'] .. ' ' .. (args['stadium'] or args['stadion']))
		end
	else
		location = args['stadium'] or '&#8202;'
	end

	-- informasi tambahan
	for i, k in ipairs({'attendance', 'penonton', 'referee', 'wasit', 'assistantreferees', 'asistenwasit1',
		'assistantreferees', 'asistenwasit2', 'fourthofficial' or 'wasitkeempat', 'fifthofficial' or 'wasitkelima',
		'goallineassistants', 'goallineassistant2', 'potm', 'motm', 'mvp'}) do
		if isnotempty(args[k]) then
			table.insert(extra, labels[k] .. ' ' .. args[k])
		end
	end
	
	-- skor pertandingan
	local scorestr = ''
	if args['score1'] or args['score2'] then
		scorestr = (args['score1'] or '0') .. '&ndash;' .. (args['score2'] or '0')
	elseif args['score'] or args['skor'] then
		scorestr = (args['score'] or args['skor'])
	end
	if args['scorenote'] then
		scorestr = scorestr .. '<br>' .. args['scorenote']
	end
	
	-- catatan
	if isnotempty(args['note']) or isnotempty(args['catatan']) then
		table.insert(notes, "''" .. labels['note'] .. " " .. (args['note'] or args['catatan']) .. "''")
	end
	if isnotempty(args['seealso']) or isnotempty(args['lihatjuga']) then
		table.insert(notes, "''" .. labels['seealso'] .. " " .. (args['seealso'] or args['lihatjuga']) .. "''")
	end

	-- check if this is a one row table
	if class == '' or class == 'uncollapsed' then
		class = ''
	elseif #extra > 0 or #notes > 0 or isnotempty(args['time']) or isnotempty(args['waktu']) or isnotempty(args['report']) or isnotempty(args['laporan']) or
		isnotempty(args['goals1']) or isnotempty(args['gol1']) or isnotempty(args['goals2']) or isnotempty(args['gol2']) or
		isnotempty(args['penalties1']) or isnotempty(args['penalti1']) or isnotempty(args['penalties2']) or isnotempty(args['penalti2']) or
		isnotempty(args['shootout1']) or isnotempty(args['shootout2']) then
	elseif class == 'collapsible collapsed' or class == 'mw-collapsible mw-collapsed' then
		class = ''
	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'] or args['tim1'] .. ' v ' .. args['team2'] or args['tim2'])
	root:newline()
	
	-- Start table
    local rClassName = 'vevent tmpl-football-box-collapsible'
    local rbg = background(frame, 'default', args['result'] or '', args['hasil'] or '', args['bg'] or '')
    if rbg == 'transparent' then
      rClassName = rClassName .. ' tmpl-football-box-collapsible-transparent'
    end

	local rtable = root:tag('table')
		:attr('cellspacing', 0)
		:addClass(class ~= '' and class or nil)
		:addClass(rClassName)
		:css('border-top', bars and '1px solid #999' or nil)
		:css('border-bottom', bars and '1px solid #999' or nil)
		:css('margin-bottom', bars and '-1px' or nil)
		:css('width', args['size'] or '100%')
		:css('background', rbg):css('color', '#202122')

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

	if #extra > 0 or isnotempty(args['time']) or isnotempty(args['waktu']) or isnotempty(args['report']) or isnotempty(args['laporan']) or 
		isnotempty(args['goals1']) or isnotempty(args['gol1']) or isnotempty(args['goals2']) or isnotempty(args['gol2']) then
		local row = rtable:tag('tr'):css('vertical-align','top'):css('font-size','85%')
		-- time
		row:tag('td')
			:css('text-align', (isnotempty(args['time']) or isnotempty(args['waktu'])) and 'right' or nil)
			:wikitext(args['time'] or args['waktu'])
		row:newline()	
		-- gol1
		row:tag('td')
			:css('text-align', (isnotempty(args['goals1']) or isnotempty(args['gol1'])) and 'right' or nil)
			:wikitext(fmtlist(args['goals1'] or args['gol1']))	
		row:newline()
		-- laporan
		row:tag('td')
			:css('text-align', (isnotempty(args['report']) or isnotempty(args['laporan'])) and 'center' or nil)
			:wikitext(makelink(args['report'] or args['laporan'] or '', 'Laporan'))
		row:newline()
		-- gol2
		row:tag('td')
			:wikitext(fmtlist(args['goals2'] or args['gol2']))
		row:newline()
		-- location, stadium
		row:tag('td')
			:wikitext(table.concat(extra, '<br>'))
	end

	if isnotempty(args['penalties1']) or isnotempty(args['penalti1']) or isnotempty(args['penalties2']) or isnotempty(args['penalti2']) then
		row = rtable:tag('tr')
		row:tag('td'):attr('rowspan', 2)
		row:tag('td'):attr('colspan', 3):css('text-align', 'center'):wikitext(bold(labels['penalties']))
		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', (isnotempty(args['penalties1']) or isnotempty(args['penalti1']) or isnotempty(args['penalties2']) or isnotempty(args['penalti2'])) and 'right' or nil)
			:wikitext(fmtlist(args['penalties1'] or args['penalti1']))
		row:newline()
		row:tag('td')
		row:newline()
		row:tag('td')
			:wikitext(fmtlist(args['penalties2'] or args['penalti2']))
		row:newline()
	elseif isnotempty(args['shootout1']) or isnotempty(args['shootout2']) then
		row = rtable:tag('tr')
		row:tag('td'):attr('rowspan', 2)
		row:tag('td'):attr('colspan', 3):css('text-align', 'center'):wikitext(bold(labels['shootout']))
		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', isnotempty(args['shootout1']) and 'right' or nil)
			:wikitext(fmtlist(args['shootout1']))
		row:newline()
		row:tag('td')
		row:newline()
		row:tag('td')
			:wikitext(fmtlist(args['shootout2']))
		row:newline()
	end
	
	if #notes > 0 then
		row = rtable:tag('tr'):css('font-size', '85%')
		row:tag('td')
			:attr('colspan', 5)
			:wikitext(table.concat(notes, '<br>'))
		row:newline()
	end
	
	local trackstr = (#tracking > 0) and table.concat(tracking, '') or ''
	if #preview > 0 then
		trackstr = require('Modul:If preview')._warning({
			'Unknown parameters ' .. table.concat(preview, '; ') .. '.'
		}) .. trackstr
	end
	
	return tostring(root) .. trackstr
end

return p