local p =
function p.calculateSum(frame) local args = frame.args local targetClass = args[1] or -- Moved targetClass to the first argument local column = args[2] local wikitext = mw.title.new(args[3]):getContent local sum = 0 local sumGoldS = 0 local sumSilverS = 0 local sumBronzeS = 0 local sumTotalS = 0 local sumGoldW = 0 local sumSilverW = 0 local sumBronzeW = 0 local sumTotalW = 0 local countGamesS = 0 local countGamesW = 0
-- Define a pattern to capture numbers enclosed in triple apostrophes or unformatted numbers local numPattern = "|%s*([%s']*%d+[%s']*)%s*|"
-- Iterate through the rows of the table for row in mw.text.gsplit(wikitext, "\n") do -- Check if the row contains the specified class if mw.ustring.find(row, 'class="summerMedals"') then -- Extract the value from the specified column local i = 0 countGamesS = countGamesS + 1 for value in mw.ustring.gmatch(row, numPattern) do i = i + 1 if value then -- Remove triple apostrophes, but treat as a string local stringValue = value:gsub("['%s]", "") if i
2 then sumSilverS = sumSilverS + tonumber(stringValue) end if i
4 then sumTotalS = sumTotalS + tonumber(stringValue) end end end end if mw.ustring.find(row, 'class="winterMedals"') then -- Extract the value from the specified column local i = 0 countGamesW = countGamesW + 1 for value in mw.ustring.gmatch(row, numPattern) do i = i + 1 if value then -- Remove triple apostrophes, but treat as a string local stringValue = value:gsub("['%s]", "") if i
2 then sumSilverW = sumSilverW + tonumber(stringValue) end if i
4 then sumTotalW = sumTotalW + tonumber(stringValue) end end end end end
if column
"Silver" then return sumSilver end if column
"Total" then return sumTotal end if column
return tostring(sum)end
return p