Jump to content

Module:Sandbox/Miria~01

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Miria~01 (talk | contribs) at 20:57, 20 June 2025 (testing). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
-- Module for {{Medals table country}}
local p = {}
local getArgs = require('Module:Arguments').getArgs
local yesno = require('Module:Yesno')
local html = mw.html

-- Helper to format medal cells
local function renderMedalCell(count, class)
	if not count or count == '' then return '' end
	count = tonumber(count) or 0
	if count == 0 then return '' end
	return tostring(html.create('td')
		:addClass(class)
		:wikitext(count))
end

-- Helper to format total cell
local function renderTotalCell(gold, silver, bronze)
	gold, silver, bronze = tonumber(gold) or 0, tonumber(silver) or 0, tonumber(bronze) or 0
	local total = gold + silver + bronze
	if total == 0 then return '' end
	return tostring(html.create('td'):wikitext(total))
end

-- Helper to format rank cell
local function renderRankCell(rank, isTotal)
	if not rank or rank == '' then return '' end
	local span = html.create('td')
		:attr('data-sort-value', rank)
		:wikitext(mw.ustring.gsub(rank, '^=', ''))
	if isTotal then
		span:addClass('sortbottom')
	end
	return tostring(span)
end

-- Helper to render participation notes
local function renderParticipationCell(text)
	if not text or text == '' then return '' end
	return tostring(html.create('td'):addClass('table-participation'):wikitext(text))
end

-- Helper to check for rowspan merging
local function getRowspanKey(row)
	return (row.games or '') .. '|' .. (row.athletes or '') .. '|' .. (row.gold or '') .. '|' .. (row.silver or '') .. '|' .. (row.bronze or '') .. '|' .. (row.rank or '') .. '|' .. (row.participation or '')
end

-- Main function
function p.main(frame)
	local args = getArgs(frame)
	local maxRows = 50
	local rows = {}

	for i = 1, maxRows do
		local games = args["games" .. i]
		if not games then break end

		local row = {
			games = games,
			athletes = args["athletes" .. i],
			gold = args["gold" .. i],
			silver = args["silver" .. i],
			bronze = args["bronze" .. i],
			rank = args["rank" .. i],
			total_rank = args["total_rank" .. i],
			host = yesno(args["host" .. i] or false),
			participation = args["participation" .. i],
			rowspan = tonumber(args["rowspan" .. i] or ''),
			override_rowspan = args["override_rowspan" .. i]
		}

		table.insert(rows, row)
	end

	-- Optionally, add logic here for automatic rowspan merging
	-- or duplicate suppression based on getRowspanKey

	local out = html.create('table')
		:addClass('wikitable')
		:addClass('sortable')
		:addClass('plainrowheaders')
		:addClass('medaltable')

	-- Header row
	out:tag('tr')
		:tag('th'):wikitext('Games'):done()
		:tag('th'):wikitext('Athletes'):done()
		:tag('th'):addClass('sorttop'):wikitext("🥇"):done()
		:tag('th'):addClass('sorttop'):wikitext("🥈"):done()
		:tag('th'):addClass('sorttop'):wikitext("🥉"):done()
		:tag('th'):addClass('sorttop'):wikitext('Total'):done()
		:tag('th'):addClass('sorttop'):wikitext('Rank'):done()
		:tag('th'):wikitext('Notes'):done()

	for _, row in ipairs(rows) do
		local tr = out:tag('tr')
		if row.host then tr:addClass('medalhost') end

		tr:tag('th'):attr('scope', 'row'):wikitext(row.games)
		tr:tag('td'):wikitext(row.athletes or '')
		tr:node(renderMedalCell(row.gold, 'gold'))
		tr:node(renderMedalCell(row.silver, 'silver'))
		tr:node(renderMedalCell(row.bronze, 'bronze'))
		tr:node(renderTotalCell(row.gold, row.silver, row.bronze))
		tr:node(renderRankCell(row.total_rank or row.rank))
		tr:node(renderParticipationCell(row.participation))
	end

	return tostring(out)
end

return p