Module:Sandbox/ Miria~01/medalsInfobox

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 == 1 then sumGoldS = sumGoldS + tonumber(stringValue) end if i == 2 then sumSilverS = sumSilverS + tonumber(stringValue) end if i == 3 then sumBronzeS = sumBronzeS + 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 == 1 then sumGoldW = sumGoldW + tonumber(stringValue) end if i == 2 then sumSilverW = sumSilverW + tonumber(stringValue) end if i == 3 then sumBronzeW = sumBronzeW + tonumber(stringValue) end if i == 4 then sumTotalW = sumTotalW + tonumber(stringValue) end end end end end

if column == "Gold" then return sumGold end if column == "Silver" then return sumSilver end if column == "Bronze" then return sumBronze end if column == "Total" then return sumTotal end if column == "All" then return '| style="background:#f2f2ce;"|' .. tostring(countGamesS) .. '|| style="background:#f2f2ce;"|' .. tostring(sumGoldS) .. '|| style="background:#f2f2ce;"|' .. tostring(sumSilverS) .. '|| style="background:#f2f2ce;"|' .. tostring(sumBronzeS) .. '|| style="background:#f2f2ce; font-style:bold;"|' .. tostring(sumTotalS) .. '|| style="background:#cedff2;" |' .. tostring(countGamesW) .. '|| style="background:#cedff2;"|' .. tostring(sumGoldW) .. '|| style="background:#cedff2;"|' .. tostring(sumSilverW) .. '|| style="background:#cedff2;"|' .. tostring(sumBronzeW) .. '|| style="background:#cedff2; font-style:bold;|' .. tostring(sumTotalW) .. '||' .. tostring(countGamesS+countGamesW) .. '||' .. tostring(sumGoldS+sumGoldW) .. '||' .. tostring(sumSilverS+sumSilverW) .. '||' .. tostring(sumBronzeS+sumBronzeW) .. '|| style="font-style:bold;"|' .. tostring(sumTotalS+sumTotalW)   end

return tostring(sum) end

return p