Module:Sports rivalry series table/sandbox
Appearance
![]() | This is the module sandbox page for Module:Sports rivalry series table (diff). |
Implements {{sports rivalry series table}}
-- This module implements {{sports rivalry series table}}
local p = {}
local root = nil
local lang =mw.getContentLanguage()
local function formatnumR(num)
num = mw.ustring.gsub(num, '^[^%d]*([%d,%.]+)[^%d]*$', '%1')
return lang:parseFormattedNumber(num)
end
local function isnotempty(s)
return s and s:match( '^%s*(.-)%s*$' ) ~= ''
end
local function addheader(header1, header2, series_summary)
-- 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(header1 or 'Winning team')
row:tag('th'):attr('colspan', 2):wikitext(header2 or 'Losing team')
if(not series_summary) then
row:tag('th'):wikitext('Series')
end
end
local function series_text(team1name, team1wins, team2name, team2wins, ties, leads)
local res = ''
if (team1wins > team2wins) then
res = team1name .. ' ' .. (leads and 'leads ' or '') .. team1wins .. '–' .. team2wins .. ( (ties > 0) and '–' .. ties or '')
elseif (team2wins > team1wins) then
res = team2name .. ' ' .. (leads and 'leads ' or '') .. team2wins .. '–' .. team1wins .. ( (ties > 0) and '–' .. ties or '')
else
res = 'Tied ' .. team1wins .. '–' .. team2wins .. ( (ties > 0) and '–' .. ties or '')
end
return res
end
local function small_rank(team)
team = mw.ustring.gsub(team or '', '(%(.-%))', '<small style="font-size:85%; font-weight:normal;">%1</small>')
return team
end
local function get_name(team)
team = mw.ustring.gsub(team or '', '%(.-%)%s*', '')
team = mw.ustring.gsub(team, '^[^A-Za-z]*(.-)[^A-Za-z]*$', '%1')
return team
end
function p.table(frame)
local args = (frame.args[3] ~= nil) and frame.args or frame:getParent().args
local team1style = args['team1style'] or ''
local team1name = mw.ustring.gsub(args['team1'] or '', '^%s*(.-)%s*$', '%1')
local team2style = args['team2style'] or ''
local team2name = mw.ustring.gsub(args['team2'] or '', '^%s*(.-)%s*$', '%1')
local team1wins = tonumber(args['team1win_start']) or 0
local team2wins = tonumber(args['team2win_start']) or 0
local ties = tonumber(args['tie_start']) or 0
local series_summary = isnotempty(args['series_summary'])
local cols = tonumber(args['cols'] or '') or 0
local res = ''
if (cols < 1 ) then cols = 1 end
-- 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)
-- compute the number of rows per column
local totalrows = rows
if (series_summary ) then totalrows = totalrows + 1 end
if (isnotempty(args['note'])) then totalrows = totalrows + 1 end
local percol = math.ceil( totalrows / cols )
-- build the table content
for j=1,rows do
if(math.fmod(j - 1, percol) == 0 ) then
-- create the root table
res = res .. (root and tostring(root) or '')
root = mw.html.create('table')
root:addClass('wikitable')
root:css('text-align', 'center')
:css('font-size', '90%')
root:cssText(args['style'])
if(cols > 1) then
root:css('float', 'left')
root:css('margin-right', '1em')
end
addheader(args['header1'], args['header2'], series_summary)
end
-- 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[6*(j-1)+1] or '')
-- Location
row:tag('td'):wikitext(args[6*(j-1)+2] or '')
-- Team1 / Team2 / Score1 / Score2
local team1 = get_name(args[6*(j-1)+3])
local score1 = mw.ustring.gsub(args[6*(j-1)+4] or '', '^%s*(.-)%s*$', '%1')
local team2 = get_name(args[6*(j-1)+5])
local score2 = mw.ustring.gsub(args[6*(j-1)+6] or '', '^%s*(.-)%s*$', '%1')
local shade1 = nil
local shade2 = nil
if( isnotempty( args['win' .. j] ) ) then
local win = mw.ustring.gsub(args['win' .. j], '^%s*(.-)%s*$', '%1')
if( win == team1 ) then
if( team1 == team1name ) then
shade1 = 'font-weight:bold;' .. team1style
team1wins = team1wins + 1
elseif(team1 == team2name ) then
shade1 = 'font-weight:bold;' .. team2style
team2wins = team2wins + 1
end
elseif( win == team2 ) then
if( team2 == team1name ) then
shade2 = 'font-weight:bold;' .. team1style
team1wins = team1wins + 1
elseif(team2 == team2name ) then
shade2 = 'font-weight:bold;' .. team2style
team2wins = team2wins + 1
end
elseif( win == 'tie' ) then
ties = ties + 1
end
elseif( score1 ~= '' and score2 ~= '') then
local score1num = tonumber(formatnumR(score1)) or 0
local score2num = tonumber(formatnumR(score2)) or 0
if ( score1num > score2num ) then
if( team1 == team1name ) then
shade1 = 'font-weight:bold;' .. team1style
team1wins = team1wins + 1
elseif (team1 == team2name) then
shade1 = 'font-weight:bold;' .. team2style
team2wins = team2wins + 1
end
elseif ( score2num > score1num ) then
if( team2 == team1name ) then
shade2 = 'font-weight:bold;' .. team1style
team1wins = team1wins + 1
elseif (team2 == team2name) then
shade2 = 'font-weight:bold;' .. team2style
team2wins = team2wins + 1
end
else
ties = ties + 1
end
end
-- Team 1
row:tag('td')
:cssText(shade1)
:wikitext(small_rank(args[6*(j-1)+3]))
-- Team 1 score
row:tag('td')
:cssText(shade1)
:wikitext(score1)
-- Team 2
row:tag('td')
:cssText(shade2)
:wikitext(small_rank(args[6*(j-1)+5]))
-- Team 2 score
row:tag('td')
:cssText(shade2)
:wikitext(score2)
-- Series
if(not series_summary) then
local seriescell = row:tag('td')
if( score1 ~= '' and score2 ~= '') then
seriescell:wikitext(series_text(team1name, team1wins, team2name, team2wins, ties, nil))
end
end
end
if( series_summary and root) then
local ftext = '\'\'\'Series:\'\'\' '
local ftext = ftext .. series_text(team1name, team1wins, team2name, team2wins, ties, 1)
if(args['footnote']) then
ftext = ftext .. args['footnote']
end
row = root:tag('tr')
row:tag('td')
:attr('colspan', 7)
:css('background-color', '#f0f0f0')
:wikitext(ftext)
end
if(isnotempty(args['note']) and root) then
row = root:tag('tr')
row:tag('td')
:attr('colspan', 7)
:wikitext(args['note'])
end
res = res .. (root and tostring(root) or '')
if (cols > 1 ) then
res = res .. tostring(mw.html.create('br'):attr('clear', 'left'))
end
-- return the root table
return res
end
return p