Jump to content

Module:Sports rivalry series table/sandbox

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Frietjes (talk | contribs) at 17:41, 12 February 2015 (Created page with '-- This module implements {{sports rivalry series table}} local p = {} local root local function isnotempty(s) return s and s:match( '^%s*(.-)%s*$' ) ~= '' end...'). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
-- This module implements {{sports rivalry series table}}
local p = {}
local root

local function isnotempty(s)
	return s and s:match( '^%s*(.-)%s*$' ) ~= ''
end
local function addheader()
	-- create the header row
	local row = root:tag('tr')
	row:tag('th'):wikitext('#')
	row:tag('th'):wikitext('Date')
	row:tag('th'):wikitext('Location')
	row:tag('th'):attr('colspan', 2):wikitext('Winning team')
	row:tag('th'):attr('colspan', 2):wikitext('Losing team')
	row:tag('th'):wikitext('Series')
end
	
function p.table(frame)
	local args = (frame.args[3] ~= nil) and frame.args or frame:getParent().args
	local team1style = args['team1style'] or ''
	local team1 = mw.ustring.gsub(args['team1'] or '', '^%s*(.-)%s*$', '%1')
	local team2style = args['team2style'] or ''
	local team2 = mw.ustring.gsub(args['team2'] or '', '^%s*(.-)%s*$', '%1')
	local team1wins = 0
	local team2wins = 0
	local ties = 0
 
	-- create the root table
	root = mw.html.create('table')
 
	-- add table classes
	root:addClass('wikitable')
	-- add table style
	root:css('text-align', 'center')
	root:cssText(args['style'])
	
	-- compute the maximum cell index
	local cellcount = 0
	for k, v in pairs( args ) do
		if type( k ) == 'number' then
			cellcount = math.max(cellcount, k)
		end
	end
	-- compute the number of rows
	local rows = math.ceil(cellcount / 6)

	-- add the header
	addheader()
	-- build the table content
	for j=1,rows do
		-- start a new row
		row = root:tag('tr')
		row:css('vertical-align', 'top')
		-- Number
		row:tag('td'):wikitext(j)
		-- Date
		row:tag('td'):wikitext(args[4*(j-1)+1] or '')
		-- Location
		row:tag('td'):wikitext(args[4*(j-1)+2] or '')
		-- Winning team
		local winner = args[4*(j-1)+3] or ''
		local winnercell = row:tag('td'):wikitext(winner)
		-- Winning score
		local winnerscorecell = row:tag('td'):wikitext(args[4*(j-1)+4] or '')
		-- Shading
		winner = mw.ustring.gsub(winner, '^[^A-Za-z]*(.-)[^A-Za-z]*$', '%1')		
		if ( winner == team1 ) then
			team1wins = team1wins + 1
			winnercell:css('font-weight', 'bold')
			winnercell:cssText(team1style)
			winnerscorecell:cssText(team1style)
		elseif (winner == team2 ) then
			team2wins = team2wins + 1
			winnercell:css('font-weight', 'bold')
			winnercell:cssText(team2style)
			winnerscorecell:cssText(team2style)
		else
			ties = ties + 1
		end
		-- Losing team
		row:tag('td'):wikitext(args[4*(j-1)+5] or '')
		-- Losing score
		row:tag('td'):wikitext(args[4*(j-1)+6] or '')
		
		-- Series
		local seriescell = row:tag('td')
		if (team1wins > team2wins) then
			seriescell:wikitext(team1 .. ' ' .. team1wins .. '–' .. team2wins .. ( (ties > 0) and '–' .. ties or '') )
		elseif (team2wins > team1wins) then
			seriescell:wikitext(team2 .. ' ' .. team2wins .. '–' .. team1wins .. ( (ties > 0) and '–' .. ties or '') )
		else
			seriescell:wikitext('Tied ' .. team1wins .. '–' .. team2wins .. ( (ties > 0) and '–' .. ties or '') )
		end
	end
	-- return the root table
	return tostring(root)
end
 
return p