Module:Medals table country
Appearance
![]() | This module is rated as ready for general use. It has reached a mature form and is thought to be relatively bug-free and ready for use wherever appropriate. It is ready to mention on help pages and other Wikipedia resources as an option for new users to learn. To reduce server load and bad output, it should be improved by sandbox testing rather than repeated trial-and-error editing. |
implements:
Usage
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
local skip_rows = {}
for i = 1, maxRows do
if not skip_rows[i] then
local games = args["row"..i.."_games"]
if not games or games == "" then break end
local participation_raw = args["row"..i.."_participation"]
if participation_raw and participation_raw ~= "" then
local participation_text, rowspan = participation_raw:match("^(.-)%s*|%s*rowspan=(%d+)$")
participation_text = participation_text or participation_raw
rowspan = tonumber(rowspan) or 1
-- Mark following rows to be skipped
for j = 1, rowspan - 1 do
skip_rows[i + j] = true
end
table.insert(rows, {
games = games,
participation = participation_text,
rowspan = rowspan
})
else
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 athletes_val == "no" then
table.insert(rows, {
games = games,
participation = "''did not participate''",
rowspan = 1
})
else
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, {
games = games,
athletes = athletes_cell,
gold = gold,
silver = silver,
bronze = bronze,
total = total,
rank = rank_link,
bgcolor = bgcolor
})
end
end
end
end
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(rows) do
if row.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)
else
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