モジュール:Medals table

require('strict')
local getArgs = require('Module:Arguments').getArgs
local flagg = require('Module:Flagg').luaMain
local p = {}
local function deflag(s)
	s = mw.ustring.gsub(s or , '%%FfIiLlEe:^%%*%%', )
	s = mw.ustring.gsub(s, '%%IiMmAaGgEe:^%%*%%', )
	s = mw.ustring.gsub(s, '*>', )
	s = mw.ustring.gsub(s, '%s*%(A-ZA-ZA-Z%)%s*$', )
	while s:match('^%s*&NnBbSsPp;%s*') do
		s = mw.ustring.gsub(s, '^%s*&NnBbSsPp;%s*', )
	end
	s = mw.ustring.gsub(s, '%s*&NnBbSsPp;%s*$', )
	s = mw.ustring.gsub(s, '%s%s+', ' ')
	s = mw.ustring.gsub(s, '^%s*(.-?)%s*$', '%1')
	return s
end
local function ifexists(arg)
	return ((arg or ) ~= ) and arg or false
end
function p.createTable(frame, args)
	if not args then
		args = getArgs(frame)
	end
local team = args'team' or '国/地域'
	local root = mw.html.create()
	local host = args'host' or 
	local host1 = args'host1' or 
	local host2 = args'host2' or 
	local hostColor = '#ccccff'
	local defaultRowColor = 'inherit'
	local flagTemplate = ifexists(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')
	local lang = mw.getContentLanguage()
	local function getFlag(code)
		if (flagTemplate == 'flagteam' or mw.ustring.lower(flagTemplate) == 'flagcountry') then
			local alias = ifexists(event) or '23px'
			local fout = flagg(frame,{'usce', code, mw.ustring.match(alias, "12%d%d%d") or alias})
			if (flagTemplate == 'flagteam') then
				fout = fout..' ('..code..')'
			end
			return fout
		end
		return frame:expandTemplate{title = flagTemplate, args = {code, event} }
	end
	-- build the legend
	if host2 ~=  and host1 ~=  then
		if args'name_' .. host1 then
			host1 = args'name_' .. host1
		elseif host1:match('^(A-ZA-ZA-Z)') then
			host1 = getFlag(host1)
		end
		if args'name_' .. host2 then
			host2 = args'name_' .. host2
		elseif host2:match('^(A-ZA-ZA-Z)') then
			host2 = getFlag(host2)
		end
		host = '開催国/地域 (' .. deflag(host1) .. '、' .. deflag(host2) .. ')'
		host = frame:expandTemplate{title = 'color box', args = {hostColor, ' * ', 'border=darkgray'}} ..' '.. host
	elseif host ~=  then
		if args'name_' .. host then
			host = args'name_' .. host
		elseif host:match('^(A-ZA-ZA-Z)') then
			host = getFlag(host)
		end
		host = '開催国/地域 (' .. deflag(host) .. ')'
		host = host .. (args'host_note' or )
		host = frame:expandTemplate{title = 'color box', args = {hostColor, ' * ', 'border=darkgray'}} ..' '.. host
	end
	
	local leading = 
	if args'leading' then
		leading = frame:expandTemplate{title = 'legend', args = {'#E9D66B', "本競技を主導"}}
	end
	
	if legendpos == 't' then
		header = header .. host .. leading
	else
		footer = footer .. host .. leading
	end
	
	local sticky = frame:extensionTag{ name = "templatestyles", args = {src = "template:sticky header/styles.css" } }
	
	root = root
		:tag('table')
		:addClass('wikitable')
		:addClass('sortable')
		:addClass('sticky-header-multi')
		:addClass('plainrowheaders')
		:addClass('jquery-tablesorter')
		:css('text-align', 'center')
		:css('font-size', args'font-size' or )
	root:tag('caption')
		:wikitext(args'caption')
	-- add the header row
	local row = root:tag('tr')
	
	if not args'hide_rank' then
		row:tag('th')
			:attr('scope', 'col')
			:css('color', 'inherit')
			:wikitext('順位')
	end
	row
		:tag('th')
			:attr('scope', 'col')
			:wikitext(team)
		:tag('th')
			:attr('scope', 'col')
			:addClass('headerSort')
			:css('width', '4em')
			:css('background-color', 'gold')
			:css('color', '#333')
			:wikitext('金')
		:tag('th')
			:attr('scope', 'col')
			:addClass('headerSort')
			:css('width', '4em')
			:css('background-color', 'silver')
			:css('color', '#333')
			:wikitext('銀')
		:tag('th')
			:attr('scope', 'col')
			:addClass('headerSort')
			:css('width', '4em')
			:css('background-color', '#c96')
			:css('color', '#333')
			:wikitext('銅')
		:tag('th')
			:attr('scope', 'col')
			:css('width', '4em')
			:wikitext('計')
	
	-- enumerate the rows
	local rowNums = {}
	local IOCs = {}
	for k,v in pairs(args) do
		k = ..k
		local IOC = k:match('^gold_(A-ZA-ZA-Z)$') or k:match('^gold_(%d+)$')
			or k:match('^silver_(A-ZA-ZA-Z)$') or k:match('^silver_(%d+)$')
			or k:match('^bronze_(A-ZA-ZA-Z)$') or k:match('^bronze_(%d+)$')
		if IOC and IOCsIOC == nil then
			IOCsIOC = 1
			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 noskip = args'skip_' .. IOC and 0 or 1
			local nation = args'name_' .. IOC or getFlag(IOC)
			nation = deflag(nation)
			if nation:match('%%^%%%|*%|(^%%*)%%') then
				nation = nation:match('%%^%%%|*%|(^%%*)%%')
			end
			if nation:match('%%(^%%%|*)%%') then
				nation = nation:match('%%(^%%%|*)%%')
			end
			table.insert(rowNums, {gold, silver, bronze, noskip, nation, IOC}) 
		end
	end
	if args'team' == 'Games' then
		table.sort(rowNums, function (a,b)
			return  a5  b1 or (a1 == b1 and a2 > b2) 
				or (a1 == b1 and a2 == b2 and a3 > b3) 
				or (a1 == b1 and a2 == b2 and a3 == b3 and a4 > b4)
				or (a1 == b1 and a2 == b2 and a3 == b3 and a4 == b4 and a5 showLimit) then 
			if remainingStart == 0 then remainingStart = rank end
			limitReached = true
			remainingGold = remainingGold + gold
			remainingSilver = remainingSilver + silver
			remainingBronze = remainingBronze + bronze
		else
			local row
			if args'leading_' .. IOC then
				row = root:tag('tr'):css('background-color', '#E9D66B'):css('color', '#202122')
				color = '#E9D66B'
			else
				row = root:tag('tr')
			end
				
			--Don't put the color on the row because of ranks spanning multiple rows. 
			--:css('background-color', color):css('color', '#202122')
			
			if not args'hide_rank' then
				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
			end
			row:tag('td')
					:attr('scope', 'row')
					:css('background-color', color)
					:css('color', 'inherit')
					:css('text-align','left')
					:wikitext(nation)
				:tag('td')
					:wikitext(lang:formatNum(gold))
				:tag('td')
					:wikitext(lang:formatNum(silver))
				:tag('td')
					:wikitext(lang:formatNum(bronze))
				:tag('td')
					:wikitext(lang:formatNum(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 'その他')
				:tag('td')
					:wikitext(lang:formatNum(remainingGold))
				:tag('td')
					:wikitext(lang:formatNum(remainingSilver))
				:tag('td')
					:wikitext(lang:formatNum(remainingBronze))
				:tag('td')
					:wikitext(lang:formatNum(remainingGold + remainingSilver + remainingBronze))
	end
	if not (team:match('^A-ZA-ZA-Z$') or team:match('>A-ZA-ZA-Z'
		end
		footer = frame:expandTemplate{ title = 'refbegin' } .. footer
		
		if args'source' then 
			footer = footer .. '出典: ' .. args'source'
		end
		if args'notes' then
			if args'source' then
				footer = footer .. 
			end
			footer = footer .. '注釈: ' .. args'notes'
		end
		footer = footer .. frame:expandTemplate{ title = 'refend' }
	end
	return header .. sticky .. tostring(root) .. footer
end
return p