Difference between revisions of "Module:Ice hockey box/sandbox"

From blackwiki
Jump to navigation Jump to search
blackwiki>Frietjes
blackwiki>Frietjes
Line 4: Line 4:
 
local function isnotempty(s)
 
local function isnotempty(s)
 
return s and s:match( '^%s*(.-)%s*$' ) ~= ''
 
return s and s:match( '^%s*(.-)%s*$' ) ~= ''
 +
end
 +
 +
local function mysplit(s)
 +
-- Change <br> tags to slashes
 +
s = mw.ustring.gsub(s or '', '<[\/%s]*[Bb][Rr][^<>]*>', ' / ')
 +
-- Split into a table
 +
s = mw.text.split(s .. ' / ', '[%s]*/[%s]*')
 +
-- Remove empty rows
 +
local t = {}
 +
for k=1,#t do
 +
if isnotempty(s[k]) then
 +
table.insert(t, s[k])
 +
end
 +
end
 +
return t
 
end
 
end
  
Line 9: Line 24:
 
local errorcats = ''
 
local errorcats = ''
 
local root = ''
 
local root = ''
-- Change <br> tags to slashes
 
g1 = mw.ustring.gsub(g1 or '', '<[\/%s]*[Bb][Rr][^<>]*>', ' / ')
 
g2 = mw.ustring.gsub(g2 or '', '<[\/%s]*[Bb][Rr][^<>]*>', ' / ')
 
p  = mw.ustring.gsub(p  or '', '<[\/%s]*[Bb][Rr][^<>]*>', ' / ')
 
 
-- Split into tables
 
-- Split into tables
g1 = mw.text.split(g1 .. ' / ', '[%s]*/[%s]*')
+
local gt1 = mysplit(g1)
g2 = mw.text.split(g2 .. ' / ', '[%s]*/[%s]*')
+
local gt2 = mysplit(g2)
p  = mw.text.split(p  .. ' / ', '[%s]*/[%s]*')
+
local pt  = mysplit(p)
-- Remove empty rows
 
local gt1 = {}
 
local gt2 = {}
 
local pt  = {}
 
for k=1,#g1 do
 
if isnotempty(g1[k]) then
 
table.insert(gt1, g1[k])
 
end
 
end
 
for k=1,#g2 do
 
if isnotempty(g2[k]) then
 
table.insert(gt2,g2[k])
 
end
 
end
 
for k=1,#p do
 
if isnotempty(p[k]) then
 
table.insert(pt,p[k])
 
end
 
end
 
errorcats = errorcats .. '<span style="display:none">GT1 = '
 
for k=1,#gt1 do
 
errorcats = errorcats .. gt1[k] .. ' / '
 
end
 
errorcats = errorcats .. '<span style="display:none">GT2 = '
 
for k=1,#gt2 do
 
errorcats = errorcats .. gt2[k] .. ' / '
 
end
 
errorcats = errorcats .. '<span style="display:none">PT = '
 
for k=1,#pt do
 
errorcats = errorcats .. pt[k] .. ' / '
 
end
 
errorcats = errorcats .. '</span>'
 
 
-- Align goals with scores in progression
 
-- Align goals with scores in progression
score1 = 0
+
local score1, score2 = 0, 0
score2 = 0
 
 
for k = 1,#pt do
 
for k = 1,#pt do
 
local s1 = tonumber(mw.ustring.gsub(pt[k] or '', '^[%s]*([0-9][0-9]*)[^0-9][^0-9]*([0-9][0-9]*)[%s]*$', '%1') or '-1') or -1
 
local s1 = tonumber(mw.ustring.gsub(pt[k] or '', '^[%s]*([0-9][0-9]*)[^0-9][^0-9]*([0-9][0-9]*)[%s]*$', '%1') or '-1') or -1
Line 66: Line 44:
 
end
 
end
 
end
 
end
if #pt > 0 then
+
-- Now build the score table
root = mw.html.create('table')
+
for k=1,#pt do
root
+
if k == 1 then
:attr('cellspacing', '0')
+
root = mw.html.create('table')
:css('width', '100%')
+
root
for k=1,#pt do
+
:attr('cellspacing', '0')
if isnotempty(pt[k]) or isnotempty(gt1[k]) or isnotempty(gt2[k]) then
+
:css('width', '100%')
local row = root:tag('tr'):css('text-align','top')
 
row:tag('td')
 
:css('text-align','right')
 
:css('width', '39%')
 
:wikitext(gt1[k] or '')
 
row:tag('td')
 
:css('text-align','center')
 
:css('width', '22%')
 
:wikitext(pt[k] or '')
 
row:tag('td')
 
:css('text-align','left')
 
:css('width', '39%')
 
:wikitext(gt2[k] or '')
 
end
 
 
end
 
end
 +
local row = root:tag('tr'):css('text-align','top')
 +
row:tag('td')
 +
:css('text-align','right')
 +
:css('width', '39%')
 +
:wikitext(gt1[k] or '')
 +
row:tag('td')
 +
:css('text-align','center')
 +
:css('width', '22%')
 +
:wikitext(pt[k] or '')
 +
row:tag('td')
 +
:css('text-align','left')
 +
:css('width', '39%')
 +
:wikitext(gt2[k] or '')
 
end
 
end
 +
 
return tostring(root) .. errorcats
 
return tostring(root) .. errorcats
 
end
 
end

Revision as of 16:33, 13 November 2016

Documentation for this module may be created at Module:Ice hockey box/sandbox/doc

-- implements [[template:IceHockeybox]]
local p = {}

local function isnotempty(s)
	return s and s:match( '^%s*(.-)%s*$' ) ~= ''
end

local function mysplit(s)
	-- Change <br> tags to slashes
	s = mw.ustring.gsub(s or '', '<[\/%s]*[Bb][Rr][^<>]*>', ' / ')
	-- Split into a table
	s = mw.text.split(s .. ' / ', '[%s]*/[%s]*')
	-- Remove empty rows
	local t = {}
	for k=1,#t do
		if isnotempty(s[k]) then
			table.insert(t, s[k])
		end
	end
	return t
end

local function scoringtable(g1, g2, p)
	local errorcats = ''
	local root = ''
	-- Split into tables
	local gt1 = mysplit(g1)
	local gt2 = mysplit(g2)
	local pt  = mysplit(p)
	-- Align goals with scores in progression
	local score1, score2 = 0, 0
	for k = 1,#pt do
		local s1 = tonumber(mw.ustring.gsub(pt[k] or '', '^[%s]*([0-9][0-9]*)[^0-9][^0-9]*([0-9][0-9]*)[%s]*$', '%1') or '-1') or -1
		local s2 = tonumber(mw.ustring.gsub(pt[k] or '', '^[%s]*([0-9][0-9]*)[^0-9][^0-9]*([0-9][0-9]*)[%s]*$', '%2') or '-1') or -1
		if s1 == (score1 + 1) and s2 == score2 then
			score1 = s1
			table.insert(gt2, k, '')
		elseif s2 == (score2 + 1) and s1 == score1 then
			score2 = s2
			table.insert(gt1, k, '')
		else
			errorcats = errorcats .. '[[Category:Pages using icehockeybox with improperly formatted progression or goals]]'
			errorcats = errorcats .. '<span style="display:none">E S1 = ' .. s1 .. ' S2 = ' .. s2 .. ' GT1 = ' .. (gt1[k] or '') .. ' GT2 = ' .. (gt2[k] or '') .. '</span>'
		end
	end
	-- Now build the score table
	for k=1,#pt do
		if k == 1 then
			root = mw.html.create('table')
			root
				:attr('cellspacing', '0')
				:css('width', '100%')
		end
		local row = root:tag('tr'):css('text-align','top')
		row:tag('td')
			:css('text-align','right')
			:css('width', '39%')
			:wikitext(gt1[k] or '')
		row:tag('td')
			:css('text-align','center')
			:css('width', '22%')
			:wikitext(pt[k] or '')
		row:tag('td')
			:css('text-align','left')
			:css('width', '39%')
			:wikitext(gt2[k] or '')
	end
	
	return tostring(root) .. errorcats
end

function p.box( frame )
	local args = frame:getParent().args
	local res = ''
	local id = args['id'] or ''
	
	id = mw.ustring.gsub(id,'^"(.-)"$', '%1')
	
	local root = mw.html.create('table')
	root
		:attr('cellspacing', '0')
		:attr('id', id )
		:css('width', '100%')
		:css('background-color', args['bg'] or '#eeeeee')
		:addClass('vevent')
	local row = root:tag('tr'):addClass('summary')
	-- Date and time
	local cell = row:tag('td')
		:css('width', '15%')
		:css('text-align', 'center')
		:css('font-size', '85%')
	cell:wikitext(args['date'] or '')
	cell:wikitext(isnotempty(args['time']) and '<br>' .. args['time'] or '')
	-- Team 1
	cell = row:tag('td')
		:css('width', '25%')
		:css('text-align', 'right')
		:addClass('vcard attendee')
	cell:tag('span'):addClass('fn org'):wikitext(args['team1'] or '')
	-- Score
	cell = row:tag('td')
		:css('width', '15%')
		:css('text-align', 'center')
	if isnotempty(args['score']) then
		cell:tag('b'):wikitext(args['score'])
		cell:wikitext('<br>')
		if isnotempty(args['periods']) then
			cell:tag('small'):wikitext(args['periods'])
		end
	end
	-- Team 2
	cell = row:tag('td')
		:css('width', '25%')
		:css('text-align', 'left')
		:addClass('vcard attendee')
	cell:tag('span'):addClass('fn org'):wikitext(args['team2'] or '')
	-- Stadium and attendance
	cell = row:tag('td')
		:css('font-size', '85%')
	if isnotempty(args['stadium']) then
		cell:tag('span'):addClass('location'):wikitext(args['stadium'])
	end
	if isnotempty(args['attendance']) then
		cell:wikitext('<br>')
		cell:tag('i'):wikitext('Attendance:')
		cell:wikitext(' ' .. args['attendance'])
	end
	res = res .. tostring(root)

	if isnotempty(args['score']) then
		root = mw.html.create('table')
		root
			:addClass('collapsible collapsed')
			:attr('cellspacing', '0')
			:css('width', '100%')
			:css('background-color', args['bg'] or '#eeeeee')
		cell = root:tag('tr'):tag('th')
		cell:attr('colspan', '5')
			:css('style', 'text-align', 'center')
			:css('font-size', '85%')
		if isnotempty(args['reference']) then
			cell:tag('b'):wikitext('[' .. args['reference'] .. ' Game reference]')
		end
		-- Empty spacing
		row = root:tag('tr'):css('font-size', '85%')
		cell = row:tag('td')
			:attr('rowspan', '4')
			:css('width', '15%')
			:css('vertical-align', 'top')

		-- Goalies
		cell = row:tag('td')
			:css('width', '25%')
			:css('vertical-align', 'top')
			:css('text-align', 'right')
			:wikitext(args['goalie1'] or '')
		cell = row:tag('td')
			:css('width', '15%')
			:css('vertical-align', 'top')
			:css('text-align', 'center')
		if isnotempty(args['goalie1']) or isnotempty(args['goalie2']) then
			cell:tag('i'):wikitext('Goalies')
		end
		cell = row:tag('td')
			:css('width', '25%')
			:css('vertical-align', 'top')
			:css('text-align', 'left')
			:wikitext(args['goalie2'] or '')

		-- Officials and linesmen
		cell = row:tag('td')
			:attr('rowspan', '4')
			:css('vertical-align', 'top')
		if isnotempty(args['official']) then
			if isnotempty(args['official2']) then
				cell:tag('i'):wikitext('Referees:')
				cell:wikitext('<br>' .. args['official'] .. '<br>' .. args['official2'])
			else
				cell:tag('i'):wikitext('Referee:')
				cell:wikitext('<br>' .. args['official'])
			end
		end
		if isnotempty(args['linesman']) then
			cell:wikitext('<br>')
			if isnotempty(args['linesman2']) then
				cell:tag('i'):wikitext('Linesmen:')
				cell:wikitext('<br>' .. args['linesman'] .. '<br>' .. args['linesman2'])
			else
				cell:tag('i'):wikitext('Linesman:')
				cell:wikitext('<br>' .. args['linesman'])
			end
		end
		-- Goals and progression
		row = root:tag('tr'):css('font-size', '85%')
		cell = row:tag('td')
			:attr('colspan', '3')
			:css('width', '65%')
			:wikitext(
				scoringtable(args['goals1'] or '', 
							args['goals2'] or '',
							args['progression'] or '')
					)
		-- Penalties
		row = root:tag('tr'):css('font-size', '85%')
		cell = row:tag('td')
			:css('width', '25%')
			:css('vertical-align', 'top')
			:css('text-align', 'right')
		if isnotempty(args['penalties1']) then
			cell:tag('i'):wikitext(args['penalties1'] .. ' min')
		end
		cell = row:tag('td')
			:css('width', '15%')
			:css('vertical-align', 'top')
			:css('text-align', 'center')
		if isnotempty(args['penalties1']) or isnotempty(args['penalties2']) then
			cell:tag('i'):wikitext('Penalties')
		end
		cell = row:tag('td')
			:css('width', '25%')
			:css('vertical-align', 'top')
			:css('text-align', 'left')
		if isnotempty(args['penalties2']) then
			cell:tag('i'):wikitext(args['penalties2'] .. ' min')
		end
		-- Shots
		row = root:tag('tr'):css('font-size', '85%')
		cell = row:tag('td')
			:css('width', '25%')
			:css('vertical-align', 'top')
			:css('text-align', 'right')
		if isnotempty(args['shots1']) then
			cell:tag('i'):wikitext(args['shots1'])
		end
		cell = row:tag('td')
			:css('width', '15%')
			:css('vertical-align', 'top')
			:css('text-align', 'center')
		if isnotempty(args['shots1']) or isnotempty(args['shots2']) then
			cell:tag('i'):wikitext('Shots')
		end
		cell = row:tag('td')
			:css('width', '25%')
			:css('vertical-align', 'top')
			:css('text-align', 'left')
		if isnotempty(args['shots2']) then
			cell:tag('i'):wikitext(args['shots2'])
		end
		res = res .. tostring(root)
	end
	if isnotempty(args['note']) then
		root = mw.html.create('table')
		root
			:attr('cellspacing', '0')
			:css('width', '100%')
			:css('background-color', args['bg'] or '#eeeeee')
		cell = root:tag('tr'):tag('td')
		cell
			:css('text-align', 'left')
			:css('font-size', '100%')
			:tag('i'):wikitext(args['note'])
		res = res .. tostring(root)
	end
	return res
end

return p