Module:Medals table
Appearance
| This module is rated as ready for general use. It has reached a mature state, is considered relatively stable and bug-free, and may be used wherever appropriate. It can be mentioned on help pages and other Wikipedia resources as an option for new users. To minimise server load and avoid disruptive output, improvements should be developed through sandbox testing rather than repeated trial-and-error editing. |
| This module is currently protected from editing. See the protection policy and protection log for more details. Please discuss any changes on the talk page; you may submit an edit request to ask an administrator to make an edit if it is uncontroversial or supported by consensus. You may also request that this page be unprotected. |
| This Lua module is used on approximately 16,000 pages and changes may be widely noticed. Test changes in the module's /sandbox or /testcases subpages, or in your own module sandbox. Consider discussing changes on the talk page before implementing them. |
Usage
require('Module:No globals')
local getArgs = require('Module:Arguments').getArgs
local p = {}
function p.createTable(frame, args)
if not args then
args = getArgs(frame)
end
local team = args['team'] or 'Nation'
local root = mw.html.create()
local host = args['host'] or ''
local hostColor = '#ccccff'
local defaultRowColor = '#f8f9fa'
local flagTemplate = args['flag_template'] or 'flagteam'
local event = args['event']
local legendpos = (args['legend_position'] or 't'):lower()
local header, footer = '', ''
local totalGold = 0
local totalSilver = 0
local totalBronze = 0
local remainingGold = 0
local remainingSilver = 0
local remainingBronze = 0
local remainingStart = 0
local remainingEnd = 0
local limitReached = false
local showLimit = tonumber(args['show_limit'])
-- build the legend
if host ~= '' then
if args['name_' .. host] then
host = 'Host nation (' .. args['name_' .. host] .. ')'
elseif host:match('^([A-Z][A-Z][A-Z])') then
host = frame:expandTemplate{title = 'country alias', args = {host, event} }
host = 'Host nation (' .. host .. ')'
end
host = host .. (args['host_note'] or '')
host = frame:expandTemplate{title = 'color box', args = {hostColor, ' * ', 'border=darkgray'}} ..' '.. host
end
if legendpos == 't' then
header = header .. host
else
footer = footer .. host
end
root = root
:tag('table')
:addClass('wikitable')
:addClass('sortable')
:addClass('plainrowheaders')
:addClass('jquery-tablesorter')
:css('text-align', 'center')
root:tag('caption')
:wikitext(args['caption'])
-- add the header row
root:tag('tr')
:tag('th')
:wikitext('Rank')
:tag('th')
:wikitext(team)
:tag('th')
:addClass('headerSort')
:css('width', '6em')
:css('background-color', 'gold')
:wikitext('Gold')
:tag('th')
:addClass('headerSort')
:css('width', '6em')
:css('background-color', 'silver')
:wikitext('Silver')
:tag('th')
:addClass('headerSort')
:css('width', '6em')
:css('background-color', '#c96')
:wikitext('Bronze')
:tag('th')
:css('width', '6em')
:wikitext('Total')
-- enumerate the rows
local rowNums = {}
for k,v in pairs(args) do
k = ''..k
local nation = k:match('^gold_([A-Z][A-Z][A-Z])$')
if nation then
local gold = (tonumber(args['gold_' .. nation]) or 0)
local silver = (tonumber(args['silver_' .. nation]) or 0)
local bronze = (tonumber(args['bronze_' .. nation]) or 0)
local noskip = args['skip_' .. nation] and 0 or 1
table.insert(rowNums, {gold, silver, bronze, noskip, nation})
end
end
table.sort(rowNums, function (a, b)
return a[1] > b[1] or (a[1] == b[1] and a[2] > b[2])
or (a[1] == b[1] and a[2] == b[2] and a[3] > b[3])
or (a[1] == b[1] and a[2] == b[2] and a[3] == b[3] and a[4] > b[4])
or (a[1] == b[1] and a[2] == b[2] and a[3] == b[3] and a[4] == b[4] and a[5] < b[5])
end
)
local lastGold, lastSilver, lastBronze = -1
local rank = 0
local lastspan, lastrankcell = 1, nil
for i, anum in ipairs(rowNums) do
local IOC = anum[5]
if args['skip_' .. IOC] then
lastGold, lastSilver, lastBronze, lastspan = -1, -1, -1, 1
else
rank = rank + 1
end
local nation = args['name_' .. IOC] or
frame:expandTemplate{title = flagTemplate, args = {IOC, event} }
local gold = tonumber(args['gold_' .. IOC]) or 0
local silver = tonumber(args['silver_' .. IOC]) or 0
local bronze = tonumber(args['bronze_' .. IOC]) or 0
local isHost = args['host_' .. IOC]
-- this is mainly for the parameter names example so you can override it.
local total = args['total_' .. IOC] or gold + silver + bronze
local color = isHost and hostColor or defaultRowColor
if args['grand_total'] then else
totalGold = totalGold + gold
totalSilver = totalSilver + silver
totalBronze = totalBronze + bronze
end
if args['host_' .. IOC] then
nation = nation .. '*'
end
if args['note_' .. IOC] then
nation = nation .. args['note_' .. IOC]
end
if showLimit and (i>showLimit) then
if remainingStart == 0 then remainingStart = i end
limitReached = true
remainingGold = remainingGold + gold
remainingSilver = remainingSilver + silver
remainingBronze = remainingBronze + bronze
else
local row = root:tag('tr')
--Don't put the color on the row because of ranks spanning multiple rows.
--:css('background-color', color)
if (gold == lastGold) and (silver == lastSilver) and (bronze == lastBronze) then
lastspan = lastspan + 1
lastrankcell:attr('rowspan',lastspan)
else
lastspan = 1
if args['skip_' .. IOC] then
lastrankcell = row:tag('td'):wikitext(frame:expandTemplate{title = 'sort', args = {'999', '-'}})
else
lastrankcell = row:tag('td'):wikitext(rank)
lastGold = gold
lastSilver = silver
lastBronze = bronze
end
end
row:tag('td')
:css('background-color', color)
:css('text-align','left')
:wikitext(nation)
:tag('td')
:wikitext(gold)
:tag('td')
:wikitext(silver)
:tag('td')
:wikitext(bronze)
:tag('td')
:wikitext(total)
end
remainingEnd = rank
end
if limitReached then
root:tag('tr')
:tag('td')
:wikitext(remainingStart..'–'..remainingEnd)
:tag('td')
:css('font-style', 'italic')
:css('text-align','left')
:wikitext(args['remaining_link'] or args['remaining_text'] or 'Remaining')
:tag('td')
:wikitext(remainingGold)
:tag('td')
:wikitext(remainingSilver)
:tag('td')
:wikitext(remainingBronze)
:tag('td')
:wikitext(remainingGold+remainingSilver+remainingBronze)
end
if team:match('^[A-Z][A-Z][A-Z]$') then else team = team:lower() end
root:tag('tr')
:addClass('sortbottom')
:tag('th')
:wikitext('Totals ('..remainingEnd..' '..team..'s)')
:attr('colspan',2)
:tag('th')
:wikitext(args['total_gold'] or totalGold)
:tag('th')
:wikitext(args['total_silver'] or totalSilver)
:tag('th')
:wikitext(args['total_bronze'] or totalBronze)
:tag('th')
:wikitext(args['grand_total'] or totalGold+totalSilver+totalBronze)
-- Build the rest of the footer
if args['source'] or args['notes'] then
if footer ~= '' then
footer = footer .. '<br>'
end
footer = frame:expandTemplate{ title = 'refbegin' } .. footer
if args['source'] then
footer = footer .. 'Source: ' .. args['source']
end
if args['notes'] then
if args['source'] then
footer = footer .. '<br>'
end
footer = footer .. 'Notes: ' .. args['notes']
end
footer = footer .. frame:expandTemplate{ title = 'refend' }
end
return header .. tostring(root) .. footer
end
return p