Module:MLB standings: Difference between revisions

Content deleted Content added
home / road record column is spilling into a second line
modify to use classes defined in Template:MLB standings/styles.css
 
(30 intermediate revisions by 10 users not shown)
Line 4:
local me = { }
 
local mlbData = mw.loadData('Module:MLB standings/data')
 
-- if mw.loadData() not supported, use require() instead
if mw.loadData then
mlbData = mw.loadData('Module:MLB standings/data')
else
mlbData = require('Module:MLB standings/data')
end
 
local Navbar = require('Module:Navbar')
 
--
-- Temporary workaround for missing mw.text utility functions
-- defaultOutputForInput: table mapping from input format to default output format
mw.text = mw.text or {}
-- (if the output format is not specified in the template arguments)
 
--
if (mw.text.trim == nil) then
mw.text.trim = function(s)
if (s == nil) then
return ''
end
return mw.ustring.match(s, "^%s*(.-)%s*$")
end
end
 
if (mw.text.gsplit == nil) then
mw.text.gsplit = function( text, pattern, plain )
local s, l = 1, mw.ustring.len( text )
return function ()
if s then
local e, n = mw.ustring.find( text, pattern, s, plain )
local ret
if not e then
ret = mw.ustring.sub( text, s )
s = nil
elseif n < e then
-- Empty separator!
ret = mw.ustring.sub( text, s, e )
if e < l then
s = e + 1
else
s = nil
end
else
ret = e > s and mw.ustring.sub( text, s, e - 1 ) or ''
s = n + 1
end
return ret
end
end, nil, nil
end
end
 
if (mw.text.split == nil) then
mw.text.split = function ( text, pattern, plain )
local ret = {}
for m in mw.text.gsplit( text, pattern, plain ) do
ret[#ret+1] = m
end
return ret
end
end
 
local defaultOutputForInput = {
default = 'default',
Line 70 ⟶ 16:
}
 
--
-- readTeamInfo: table of input parsers
-- Keys are the input formats, values are functions that parse the unnamed parameters
-- that were passed to the template and return a table holding the team name
-- and the win-loss records (either overall, or home and away, depending on the
-- input format).
-- The parsers take the following parameters:
-- args: table holding the parameters (indexed by numeric position)
-- currentIdx: the current index from where the next set of data should be parsed
-- returnData: table that the parser will update to pass additional data back to the caller.
-- returnData.cIndicesRead is updated with the number of parameters that were parsed
--
local readTeamInfo = {
default = function(args, currentIdx, returnData)
Line 109 ⟶ 67:
} -- readTeamInfo object
 
--
-- generateTableHeader: table of functions that generate table header
-- Keys are the output formats, values are functions that return a string with the table header
-- The generator functions take the following parameter:
-- tableHeaderInfo: table that contains the information needed for the header
--
local generateTableHeader = {
default = function(tableHeaderInfo)
return
'{| class="wikitable" width="525emMLBStandingsTable" style="text-align:center;"\
!|+ width="40%" |' .. tableHeaderInfo.navbarText .. '[[' .. tableHeaderInfo.divisionLink
.. '|' .. tableHeaderInfo.division .. ']]\
|- \
! width="7%" | [[Win (baseball)|W]]\
! width="751%" | [[LossTeam (baseball)|L]]\
! width="6%" | [[Win (baseball)|W]]\
! width="6%" | [[Loss (baseball)|L]]\
! width="9%" | [[Winning percentage|Pct.]]\
! width="78%" | [[Games behind|GB]]\
! width="1510%" | [[Home (sports)|Home]]\
! width="1510%" | [[Road (sports)|Road]]\
'
end, -- function generateTableHeader.default()
Line 126 ⟶ 92:
winLossOnly = function(tableHeaderInfo)
return
'{| class="wikitable" width="380emMLBStandingsTable" style="text-align:center;"\
!|+ width="66%" | ' .. tableHeaderInfo.navbarText .. tableHeaderInfo.division .. '\
|- \
! width="66%" | Team\
! width="10%" | [[Win (baseball)|W]]\
! width="10%" | [[Loss (baseball)|L]]\
Line 136 ⟶ 104:
wildCard2012 = function(tableHeaderInfo)
return
'{| class="wikitable" width="375emMLBStandingsTable" style="text-align:center;"\
!|+ width="60%" | ' .. tableHeaderInfo.navbarText .. 'Wild Card teams<br><small>(Top 2 teams qualify for 1-game playoffpostseason)</small>\
|- \
! width="64%" | Team \
! width="8%" | [[Win (baseball)|W]]\
! width="8%" | [[Loss (baseball)|L]]\
! width="1210%" | [[Winning percentage|Pct.]]\
! width="1210%" | [[Games behind|GB]]\
'
end, -- function generateTableHeader.wildCard2012
 
wildCard = function(tableHeaderInfo)
local teamText = 'team'
local numberOfTeamsText = 'team qualifies'
if tableHeaderInfo.wildCardsPerLeague > 1 then
teamText = 'teams'
numberOfTeamsText = tableHeaderInfo.wildCardsPerLeague .. ' teams qualify'
end
return
'{| class="wikitable MLBStandingsTable" \
|+ |' .. tableHeaderInfo.navbarText .. 'Wild Card ' .. teamText .. '<br><small>(Top ' .. numberOfTeamsText ..
' for postseason)</small>\
|- \
! width="64%" | Team \
! width="8%" | [[Win (baseball)|W]]\
! width="8%" | [[Loss (baseball)|L]]\
! width="10%" | [[Winning percentage|Pct.]]\
! width="10%" | [[Games behind|GB]]\
'
end, -- function generateTableHeader.wildCard
 
} -- generateTableHeader object
 
--
-- generateTeamRow: table of functions that generate a table row
-- Keys are the output formats, values are functions that return a string with the table row
-- The generator functions take the following parameter:
-- tableRowInfo: table that contains additional the information needed for the row
-- teamInfo: table that contains the team name and win-loss info
--
local generateTeamRow = {
default = function(teamRowInfo, teamInfo)
Line 154 ⟶ 152:
|| ' .. teamRowInfo.winningPercentage .. '\
|| ' .. teamRowInfo.gamesBehind .. '\
|| ' .. teamInfo.homeWins .. '&zwj;&zwj;' .. teamInfo.homeLosses ..'\
|| ' .. teamInfo.roadWins .. '&zwj;&zwj;' .. teamInfo.roadLosses .. '\n'
 
end, -- function generateTeamRow.default()
Line 175 ⟶ 173:
|| ' .. teamRowInfo.gamesBehind .. '\n'
end, -- function generateTeamRow.wildCard2012
 
wildCard = function(teamRowInfo, teamInfo)
return
'|-' .. teamRowInfo.rowStyle .. '\
|| ' .. teamRowInfo.seedText .. '[[' .. teamRowInfo.teamSeasonPage .. '|' .. teamInfo.name .. ']]\
|| ' .. teamInfo.wins .. ' || ' .. teamInfo.losses .. '\
|| ' .. teamRowInfo.winningPercentage .. '\
|| ' .. teamRowInfo.gamesBehind .. '\n'
end, -- function generateTeamRow.wildCard
} -- generateTeamRow object
 
--
-- parseSeeds: function to parse the seeds template argument
--
local function parseSeeds(seedsArg, seeds)
local seedList = mw.text.split(seedsArg, '%s*,%s*')
Line 194 ⟶ 204:
end -- function parseSeeds()
 
--
-- parseHighlightArg: function to parse the highlight template argument
--
local function parseHighlightArg(highlightArg, teamsToHighlight)
local teamList = mw.text.split(highlightArg, '%s*,%s*')
Line 206 ⟶ 219:
end -- function parseHighlightArg
 
--
-- parseTeamLInks: function to parse the team_links template argument
--
local function parseTeamLinks(teamLinksArg, linkForTeam)
local teamList = mw.text.split(teamLinksArg, '%s*,%s*')
Line 222 ⟶ 238:
end -- function parseTeamLinks
 
local function getWildCardsPerLeagueForYear(year)
if year == '' then
return 0
end
for idx, wildCardInfo in ipairs(mlbData.wildCardInfo) do
if wildCardInfo.startYear <= year and year <= wildCardInfo.endYear then
return wildCardInfo.wildCardsPerLeague;
end
end
-- year not found, thus no wild cards for specified year
return 0;
end -- function getWildCardsPerLeagueForYear
 
--
-- function generateStandingsTable
--
-- Parameters: frame object from template
-- frame.args.input: input format for standings info
-- if not specified, the default is team name followed by home win-loss and road win-loss records
-- - overallWinLoss: team name followed by overall win-loss record
--
-- frame.args.output: output format for standings table
-- if not specified, the output format is based on the input format (see defaultOutputForInput table):
-- - default => games behind and home and road win-loss records displayed
-- - overallWinLoss => overall win-loss records displayed, no games behind column
-- - winLossOnly: overall win-loss records displayed, no games behind column
-- - wildCard: wildcard standings table displayed
-- - wildCard2012: wildcard standings table displayed (effectively the same as wildcard for years from 2012-2021; kept for backwards compatibility)
--
-- frame.args.template_name: name of standings template
-- if not specified, the default is <year> <division name> standings
--
-- frame.args.seeds: list of team seedings
-- frame.args.highlight: list of teams to highlight
-- frame.args.team_links: list of link targets for each team
-- If not specified, the default is just the team name.
-- This is used to generate the season page for each team, in the form
-- <year> <team link target> season
--
function me.generateStandingsTable(frame)
local inputFormat = 'default'
 
-- If the input parameter is specified in the template, use it as the input format.
if (frame.args.input ~= nil) then
local inputArg = mw.text.trim(frame.args.input)
Line 239 ⟶ 296:
local fDisplayNavbar = true
local fDisplayGamesBehind = true
 
-- If the output parameter is specified in the template, use it as the output format.
-- Note no cross validation is performed to check if it is valid given the input format.
if (frame.args.output ~= nil) then
local outputArg = mw.text.trim(frame.args.output)
Line 248 ⟶ 308:
outputFormat = 'wildCard2012'
end
if (outputArg == 'wildCard') then
outputFormat = 'wildCard'
end
end
 
local year = tonumber(mw.text.trim(frame.args.year or '0'))
local division = mw.text.trim(frame.args.division or '')
local divisionLink = mw.text.trim(frame.args.division_link or division)
local wildCardsPerLeague = getWildCardsPerLeagueForYear(year)
 
local seedInfo = {}
Line 272 ⟶ 336:
local currentArgIdx = 1;
 
-- Parse the unnamed parameters from the template. This consists of the
-- team names and their win-loss records.
while (frame.args[currentArgIdx] ~= nil) do
local returnData = { }
Line 291 ⟶ 357:
end
 
-- table to hold list of strings that will be concatenated at the end
-- to create a string with the standings table
local outputBuffer = { }
 
Line 296 ⟶ 364:
division = division,
divisionLink = divisionLink,
wildCardsPerLeague = wildCardsPerLeague,
}
 
Line 324 ⟶ 393:
local leadingHalfGames = nil;
if (fDisplayGamesBehind) then
local standingsLeaderIdx = 1;
if (outputFormat == 'wildCard2012' and #listOfTeams > 1) then
standingsLeaderIdx = 2;
end
if (outputFormat == 'wildCard' and #listOfTeams >= wildCardsPerLeague) then
standingsLeaderIdx = wildCardsPerLeague
end
 
local teamInfo = listOfTeams[standingsLeaderIdx]
leadingHalfGames = (teamInfo.wins - teamInfo.losses)
Line 333 ⟶ 406:
 
for idx, teamInfo in ipairs(listOfTeams) do
local winningPercentage = string.format(
'%.3f', teamInfo.wins / ( teamInfo.wins + teamInfo.losses )
)
winningPercentage = string.gsub(winningPercentage, '^0', '')
local teamRowInfo = {
teamSeasonPage = year .. ' ' .. teamInfo.teamLink .. ' season',
winningPercentage = string.format(winningPercentage,
'%.3f', teamInfo.wins / ( teamInfo.wins + teamInfo.losses )
),
gamesBehind = '',
seedText = '',
Line 370 ⟶ 445:
 
if (seedInfo[teamInfo.name] ~= nil) then
teamRowInfo.seedText = '<sup>(' .. seedInfo[teamInfo.name] .. ')</sup> &nbsp;'
teamRowInfo.rowStyle = ' styleclass="background:#CCFFCCMLBStandingsHighlightedRow"'
end
 
if (teamsToHighlight[teamInfo.name]) then
teamRowInfo.rowStyle = ' styleclass="background:#CCFFCCMLBStandingsHighlightedRow"'
end
 
Line 383 ⟶ 458:
end -- end of looping over listOfTeams
 
table.insert(outputBuffer, '|}\n')
 
return table.concat(outputBuffer)