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 sumGold = 0 local sumSilver = 0 local sumBronze = 0 local sumTotal = 0
-- Define a pattern to capture numbers enclosed in triple apostrophes or unformatted numbers local numPattern = "|%s*([%s']*%d+[%s']*)%s*|" local trimmedRow = "" -- 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="' .. targetClass .. '"') then trimmedRow = row:match('.*class="' .. targetClass .. '"(.*)') -- Extract everything after the class attribute -- Extract the value from the specified column local i = 0 for value in mw.ustring.gmatch(trimmedRow, 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 sumSilver = sumSilver + tonumber(stringValue) end if i
4 then sumTotal = sumTotal + 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