มอดูล:Football box

-- Implements แม่แบบ:Football box
local p = {}
local lang = nil
local delink = require('มอดูล:Delink')._delink
local eventschema = "http://schema.org/SportsEvent"
local teamschema = "http://schema.org/SportsTeam"
local placeschema = "http://schema.org/Place"
local penalties = 'ลูกโทษ'
local aet = 'ต่อเวลาพิเศษ'
local asdet = 'แพ้ตกรอบ'
local agget = 'ต่อเวลาพิเศษ/โกลเดนโกล'
local asget = 'ต่อเวลาพิเศษ/'
local attendance = 'ผู้ชม:'
local referee = 'ผู้ตัดสิน:'
local templatestyles = 'มอดูล:Football box/styles.css'
local tracking, preview
local function checkarg(k,v)
	if not k or type(k) ~= 'string' then return end
	
	if  k == 'assistantreferees2' then
		table.insert(tracking, '2')
	elseif k == 'aggregatescore' or k == 'assistantreferees' or 
			k == 'fourthofficial' or k == 'game' or k == 'motm' or 
			k == 'nobars' or k == 'note' or k == 'result' then
		-- valid and tracked
		table.insert(tracking, 'หมวดหมู่:หน้าที่ใช้กล่องฟุตบอลด้วย ' .. k .. )
	elseif k == 'size' or k == 'bg' or k == 'id' or k == 'event' or
		k == 'date' or k == 'time' or k == 'round' or k == 'team1' or 
		k == 'team2' or k == 'score1' or k == 'score2' or k == 'score' or k == 'scorenote' or
		k == 'aet' or k == 'asdet' or k == 'agget' or k == 'asget' or k == 'goals1' or k == 'report' or k == 'goals2' or 
		k == 'penaltyscore' or k == 'penalties1' or k == 'penalties2' or 
		k == 'stadium' or k == 'location' or k == 'attendance' or 
		k == 'referee' or k == 'stack' or k == 'section' or k == 'transcludesection' then
		-- valid and not tracked
	else
		-- invalid
		local vlen = mw.ustring.len(k)
		k = mw.ustring.sub(k, 1, (vlen *>', )
	s = mw.ustring.gsub(s, '*>', )
	s = mw.ustring.gsub(s, '*>(^', '%1')
	s = mw.ustring.gsub(s, '\'"', )
	s = mw.ustring.gsub(s, '%s* %s*', ' ')
	s = mw.ustring.gsub(s, '%%^%%%|*%|(^%%*)%%', '%1')
	s = mw.ustring.gsub(s, '%%(^%%*)%%', '%1')
	s = mw.ustring.gsub(s, '\127^\127*UNIQ%-%-^\127*%-%x+%-QINU^\127*\127', )
	s = mw.ustring.gsub(s, '^*', )
	s = mw.ustring.gsub(s, '%s%s*', ' ')
	return s
end
local function checkTitle()
local pageTitle = mw.title.getCurrentTitle().text
local titlePhrases = {
"รอบแบ่งกลุ่ม", "รอบแพ้คัดออก", "รอบแพ้คัดออก", "รอบชิงชนะเลิศ", "รอบลีก",
"รอบ", "รอบลีก", "รอบคัดเลือก", "การคัดเลือก", "เพลย์ออฟ", "เนชันส์ลีก เอ-ดี", "อินเตอร์โตโตคัพ "
}

for _, phrase in ipairs(titlePhrases) do
if mw.ustring.match(mw.ustring.lower(pageTitle), phrase) then
return true
end
end

return false
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 = require('มอดูล:Arguments').getArgs(frame)
	local id = getid(args'id')
	local d = timestamp(args'date', args'time')
	local block
	
	tracking, preview = {}, {}
for k, v in pairs(args) do
	if v ~=  then
		checkarg(k,v)
	end
	end
	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 .. ')'
	elseif args'asdet' then
		score = score .. ' (' .. asdet .. ')'
	elseif args'agget' then
		score = score .. ' (' .. agget .. ')'
	elseif args'asget' then
		score = score .. ' (' .. asget .. ')'
	end
	if args'scorenote' then
		score = score ..  .. args'scorenote'
	end
	
	-- Generate an anchor
	if id and id == 'event' and args'event' then
		id = makeanchor(args'event')
	end
	if (id and id == 'auto') or (not id and checkTitle()) then
		id = makeanchor((args'team1' or 'Team 1') .. ' v ' .. (args'team2' or 'Team 2'))
	end
	
	-- Start box
	local root = 
		mw.html.create('div')
			:attr('itemscope', )
			:attr('itemtype', eventschema)
			:addClass('footballbox')
			:css('width', args'size')
			:css('background-color', args'bg')
			:attr('id', id)
	root:newline()
	
	if args'event' then
		root:tag('div')
			:addClass('ftitle')
			:wikitext(args'event')
	end
	
	-- Start left block
	block = root:tag('div')
		:addClass('fleft')
	
	local timetag = block:tag('time')
		:attr('itemprop', d and 'startDate' or nil)
		:attr('datetime', d)
		
	timetag:tag('div')
		:addClass('fdate')
		:wikitext(args'date')
	
	if args'time' then
		timetag:tag('div')
			:addClass('ftime')
			:wikitext(args'time')
	end
	
	if args'round' then
		block:tag('div')
			:addClass('frnd')
			:wikitext(args'round')
	end
	-- End block
	
	-- Start table
	local rtable = root:tag('table')
		:addClass('fevent')
	local row = rtable:tag('tr')
		:attr('itemprop', 'name')
	row:newline()
	row:tag('th')
		:addClass('fhome')
		:attr('itemprop', 'homeTeam')
		:attr('itemscope', )
		:attr('itemtype', teamschema)
		:tag('span')
			:attr('itemprop', 'name')
			:wikitext(args'team1')
	row:tag('th')
		:addClass('fscore')
		:wikitext(score)
	row:tag('th')
		:addClass('faway')
		:attr('itemprop', 'awayTeam')
		:attr('itemscope', )
		:attr('itemtype', teamschema)
		:tag('span')
			:attr('itemprop', 'name')
			:wikitext(args'team2')
	row = rtable:tag('tr')
		:addClass('fgoals')
		:newline()
	row:tag('td')
		:addClass('fhgoal')
		:wikitext(fmtlist(args'goals1'))
	row:newline()
	row:tag('td')
		:wikitext(makelink(args'report' or , 'รายงาน'))
	row:newline()
	row:tag('td')
		:addClass('fagoal')
		:wikitext(fmtlist(args'goals2'))
	row:newline()	
	
	if args'penaltyscore' then
		rtable
			:tag('tr')
				:tag('th')
					:attr('colspan', 3)
					:wikitext(penalties)
		row = rtable:tag('tr')
			:addClass('fgoals')
		row:newline()
		row:tag('td')
			:addClass('fhgoal')
			:wikitext(fmtlist(args'penalties1'))
		row:newline()
		row:tag('th')
			:wikitext(args'penaltyscore')
		row:newline()
		row:tag('td')
			:addClass('fagoal')
			:wikitext(fmtlist(args'penalties2'))
		row:newline()
	end
	-- End table
	
	-- Start right block
	block = root:tag('div')
		:addClass('fright')
	
	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
	local trackstr = (#tracking > 0) and table.concat(tracking, ) or 
	if #preview > 0 then
		trackstr = require('มอดูล:If preview')._warning({
			'Unknown parameters ' .. table.concat(preview, '; ') .. '.'
		}) .. trackstr
	end
	
	return frame:extensionTag{ name = 'templatestyles', args = { src = templatestyles} } .. tostring(root) .. trackstr
end
return p