Module:Sports series: Difference between revisions

Content deleted Content added
improve matching logic
implement notes feature
Line 152:
score1 = tonumber(score1)
score2 = tonumber(score2)
 
if score1 > score2 then
team1Winner = true
Line 166:
if team1AwayGoals and team2AwayGoals then
team1AwayGoals, team2AwayGoals = tonumber(team1AwayGoals), tonumber(team2AwayGoals)
 
if team1AwayGoals > team2AwayGoals then
team1Winner = true
Line 174:
end
end
 
if (colorWinner or isFBRStyle) and legs == 0 then
isDraw = not team1Winner and not team2Winner
Line 192:
end
return false
end
 
-- Function to add a legend to below the table when isFBRStyle is true
local function createFBRLegend()
return mw.html.create('div')
:css('font-size', '90%')
:css('margin-bottom', '0.5em')
:wikitext("Legend: Blue = home team win; Yellow = draw; Red = away team win.")
end
 
Line 378 ⟶ 370:
 
return score
end
 
-- Function to process notes for aggregate and leg scores
local function processNote(frame, notes, noteKey, noteText, endText, rowIndex, rand_val, noteGroup)
if not noteText then return endText, notes end
if noteText:match("^%s*<sup") or noteText:match("^\127%\'%\"`UNIQ") then
return noteText .. endText, notes
end
 
local function createInlineNote(name)
return frame:extensionTag{
name = 'ref',
args = {
name = name,
group = noteGroup
}
}
end
 
-- Check if noteText is a reference to another note
local referenced_note = noteText:match("^(agg_%d+)$") or noteText:match("^(leg%d+_%d+)$")
if referenced_note then
local referenced_note_id = '"table_note_' .. referenced_note .. '_' .. rand_val .. '"'
return endText .. createInlineNote(referenced_note_id), notes
end
 
local note_id = '"table_note_' .. noteKey .. '_' .. rowIndex .. '_' .. rand_val .. '"'
if not notes[note_id] then
notes[note_id] = noteText
end
 
return endText .. createInlineNote(note_id), notes
end
 
-- Function to generate the footer if necessary
local function createFooter(frame, notes, noteGroup, isFBRStyle, displayNotes, externalNotes, legs)
local needFooter = (isFBRStyle and legs == 0) or displayNotes or (next(notes) ~= nil)
 
if not needFooter then
return '' -- Return an empty string if no footer is needed
end
 
local divContent = mw.html.create('div')
:css('font-size', '90%')
:css('margin-bottom', '0.5em')
 
if isFBRStyle and legs == 0 then
divContent:wikitext("Legend: Blue = home team win; Yellow = draw; Red = away team win.")
end
 
if (next(notes) ~= nil and not externalNotes) or displayNotes then
divContent:wikitext((isFBRStyle and legs == 0) and "<br>Notes:" or "Notes:")
end
 
local footer = tostring(divContent)
 
if next(notes) ~= nil or displayNotes then
local noteDefinitions = {}
for noteId, noteText in pairs(notes) do
if type(noteId) == 'string' and noteId:match('^"table_note') then
table.insert(noteDefinitions, frame:extensionTag{
name = 'ref',
args = {
name = noteId,
group = noteGroup
},
content = noteText
})
end
end
 
if externalNotes then
local hiddenRefs = mw.html.create('span')
:css('display', 'none')
:wikitext(table.concat(noteDefinitions))
if isFBRStyle and legs == 0 then
footer = footer .. tostring(hiddenRefs)
else
footer = tostring(hiddenRefs)
end
else
local reflistArgs = {
refs = table.concat(noteDefinitions),
group = noteGroup
}
footer = footer .. frame:expandTemplate{
title = 'reflist',
args = reflistArgs
}
end
end
 
return footer
end
 
-- Main function that processes input and returns the wikitable
function p.main(frame)
local args = require('Module:Arguments').getArgs(frame, {trim = true})
 
-- Check for section transclusion
Line 414 ⟶ 499:
local baselink = frame:getParent():getTitle()
if mw.title.getCurrentTitle().text == baselink then baselink = '' end
local notes = {}
local noteGroup = args.note_group or 'lower-alpha'
local displayNotes = isTrue(args.note_list)
local externalNotes = args.note_list == 'external'
math.randomseed(os.clock() * 10^8) -- Initialize random number generator
local rand_val = math.random()
 
-- Process the font size parameter
Line 459 ⟶ 550:
local matchesStyle = args.matches_style
local isFBRStyle = matchesStyle and matchesStyle:upper() == "FBR"
local isHA = isTrue(args.h_a) or (isFBRStyle and legs == 0)
local disableAwayGoals = isFalse(args.away_goals)
local disableSmallText = isFalse(args.small_text)
Line 488 ⟶ 579:
if args.id then
table:attr('id', args.id) -- Optional id parameter to allow anchor to table
end
 
-- Add FBR legend if isFBRStyle is true
if isFBRStyle and legs == 0 then
root:node(createFBRLegend())
isHA = true
end
 
Line 562 ⟶ 647:
while anyParameterPresent(i, step, args) do
local rowIndex = math.floor((i - 1) / step) + 1
local aggNote = args['note_agg_' .. rowIndex]
local headingParam = args['heading' .. rowIndex]
 
Line 608 ⟶ 694:
-- Format and rewrite anchor links for aggregate score
aggregateScore, aggregateEndText = format_and_extract_score(aggregateScore, doWrap)
aggregateEndText, notes = processNote(frame, notes, 'agg', aggNote, aggregateEndText, rowIndex, rand_val, noteGroup)
if generateLinks and legs == 0 then
aggregateScore = cleanAndGenerateLinks(team1, team2, aggregateScore, false)
Line 628 ⟶ 715:
team2Style = team2Style .. ' background-color: #CCFFCC;'
end
 
-- Function to create flag template parameters
local function getFlagParams(icon, variant)
Line 686 ⟶ 773:
local legIndex = i + 4 + leg + (noFlagIcons and -2 or 0)
local legScore = args[legIndex]
local legNote = args['note_leg' .. leg .. '_' .. rowIndex]
if legScore ~= "nil" then
if legScore == "null" then
Line 699 ⟶ 787:
local cleanLeg = cleanScore(legScore)
legScore, legEndText = format_and_extract_score(legScore, doWrap)
legEndText, notes = processNote(frame, notes, 'leg' .. leg, legNote, legEndText, rowIndex, rand_val, noteGroup)
if generateLinks then
if leg == 1 then
Line 726 ⟶ 815:
i = i + step
end
 
-- Generate footer text
local footerText = createFooter(frame, notes, noteGroup, isFBRStyle, displayNotes, externalNotes, legs)
root:wikitext(footerText)
 
local tableCode = tostring(root)