Jump to content

Module:Medals table country

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Miria~01 (talk | contribs) at 10:18, 19 June 2025 (with automatic rowspan detection). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

local p = {}

function p.render(frame)
    local args = frame:getParent().args
    local country = args["country"] or "Country"
    local maxRows = 50
    local rows = {}
    local total_gold, total_silver, total_bronze = 0, 0, 0

    -- Step 1: Collect raw row data
    for i = 1, maxRows do
        local games = args["row"..i.."_games"]
        if not games or games == "" then break end

        local participation = args["row"..i.."_participation"]
        local athletes_val = args["row"..i.."_athletes"]
        local gold = tonumber(args["row"..i.."_gold"]) or 0
        local silver = tonumber(args["row"..i.."_silver"]) or 0
        local bronze = tonumber(args["row"..i.."_bronze"]) or 0
        local rank_raw = args["row"..i.."_rank"] or ""
        local year = games:match("(%d%d%d%d)") or ""

        if participation and participation ~= "" then
            table.insert(rows, {
                type = "participation",
                games = games,
                participation = participation
            })
        elseif athletes_val == "no" then
            table.insert(rows, {
                type = "participation",
                games = games,
                participation = "''did not participate''"
            })
        elseif athletes_val then
            local athletes_num = tonumber(athletes_val) or 0
            local athletes_cell = string.format("[[%s at the %s Summer Olympics|%d]]", country, year, athletes_num)

            local total = gold + silver + bronze
            total_gold = total_gold + gold
            total_silver = total_silver + silver
            total_bronze = total_bronze + bronze

            local rank = tonumber(rank_raw)
            local rank_link = rank and string.format("[[%s Summer Olympics medal table|%d]]", year, rank) or rank_raw

            local bgcolor = ""
            if rank == 1 then
                bgcolor = "#F7F6A8"
            elseif rank == 2 then
                bgcolor = "#dce5e5"
            elseif rank == 3 then
                bgcolor = "#ffdab9"
            end

            table.insert(rows, {
                type = "medal",
                games = games,
                athletes = athletes_cell,
                gold = gold,
                silver = silver,
                bronze = bronze,
                total = total,
                rank = rank_link,
                bgcolor = bgcolor
            })
        else
            table.insert(rows, {
                type = "empty",
                games = games
            })
        end
    end

    -- Step 2: Merge consecutive participation rows
    local final_rows = {}
    local i = 1
    while i <= #rows do
        local row = rows[i]
        if row.type == "participation" then
            local rowspan = 1
            for j = i + 1, #rows do
                if rows[j].type == "participation" and rows[j].participation == row.participation then
                    rowspan = rowspan + 1
                else
                    break
                end
            end
            row.rowspan = rowspan
            table.insert(final_rows, row)
            i = i + rowspan
        else
            table.insert(final_rows, row)
            i = i + 1
        end
    end

    -- Step 3: Render table
    local total_medals = total_gold + total_silver + total_bronze
    local total_rank = args["total_rank"] or ""

    local wikitext = '{| class="wikitable" style="text-align:center; font-size:90%;"\n'
    wikitext = wikitext .. "|-\n! Games !! Athletes !! style=\"width:3em; font-weight:bold;\"| Gold !! style=\"width:3em; font-weight:bold;\"| Silver !! style=\"width:3em; font-weight:bold;\"| Bronze !! style=\"width:3em; font-weight:bold;\"| Total !! style=\"width:3em; font-weight:bold;\"| Rank\n"

    for _, row in ipairs(final_rows) do
        if row.type == "participation" then
            local rowspan_attr = row.rowspan > 1 and (" rowspan=" .. row.rowspan) or ""
            wikitext = wikitext .. string.format("|-\n| align=left | %s || colspan=6%s | %s\n", row.games, rowspan_attr, row.participation)
        elseif row.type == "empty" then
            wikitext = wikitext .. string.format("|-\n| align=left | %s || colspan=6 |\n", row.games)
        elseif row.type == "medal" then
            local line = "|-\n| align=left | " .. row.games .. " || " .. row.athletes
            line = line .. string.format(" || %d || %d || %d || %d", row.gold, row.silver, row.bronze, row.total)
            if row.bgcolor ~= "" then
                line = line .. string.format(" || style=\"background-color:%s\" | %s", row.bgcolor, row.rank)
            else
                line = line .. " || " .. row.rank
            end
            wikitext = wikitext .. line .. "\n"
        end
    end

    wikitext = wikitext .. string.format("|-\n! colspan=2 | Total !! %d !! %d !! %d !! %d !! %s\n",
        total_gold, total_silver, total_bronze, total_medals, total_rank)
    wikitext = wikitext .. "|}"

    return wikitext
end

return p