Module:Sports rivalry series table
Appearance
Implements {{sports rivalry series table}}
-- This module implements {{sports rivalry series table}}
local p = {}
local function isnotempty(s)
return s and s:match( '^%s*(.-)%s*$' ) ~= ''
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
local root = mw.html.create('table')
-- add table classes
root:addClass('wikitable')
-- add table style
root:css('text-align', 'center')
-- 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 / 4)
-- 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'):wikitext('Winner')
row:tag('th'):wikitext('Score')
row:tag('th'):wikitext('Series')
-- 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 '')
-- Winner
local winner = wikitext(args[4*(j-1)+3] or '')
local winnercell = row:tag('td'):wikitext(winner)
-- Score
local scorecell = 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:cssText(team1style)
scorecell:cssText(team1style)
elseif (winner == team2 ) then
team2wins = team2wins + 1
winnercell:cssText(team2style)
scorecell:cssText(team2style)
else
ties = ties + 1
end
-- 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