跳至內容

模組討論:Medals table

頁面內容不支援其他語言。
維基百科,自由的百科全書

編輯請求

請求已處理--Xiplus#Talk 2018年11月16日 (五) 08:32 (UTC)[回覆]

請加入這個編輯

內容
要加入的編輯
require('Module:No globals')
local getArgs = require('Module:Arguments').getArgs

local p = {}

local function deflag(s)
	s = mw.ustring.gsub(s or '', '%[%[[Fe][Ii][Ll][Ee]:[^%[%]]*%]%]', '')
	s = mw.ustring.gsub(s, '%[%[[Ii][Mm][Aa][Gg][Ee]:[^%[%]]*%]%]', '')
	s = mw.ustring.gsub(s, '<[^<>]*>', '')
	s = mw.ustring.gsub(s, '%s*%([A-Z][A-Z][A-Z]%)%s*$', '')
	while s:match('^%s*&[Nn][Bb][Ss][Pp];%s*') do
		s = mw.ustring.gsub(s, '^%s*&[Nn][Bb][Ss][Pp];%s*', '')
	end
	s = mw.ustring.gsub(s, '%s*&[Nn][Bb][Ss][Pp];%s*$', '')
	s = mw.ustring.gsub(s, '%s%s+', ' ')
	s = mw.ustring.gsub(s, '^%s*(.-?)%s*$', '%1')
	return s
end

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 = args['name_' .. host]
			host = 'Host nation (' .. deflag(host) .. ')'
		elseif host:match('^([A-Z][A-Z][A-Z])') then
			host = frame:expandTemplate{title = flagTemplate, args = {host, event} }
			host = 'Host nation (' .. deflag(host) .. ')'
		end
		host = host .. (args['host_note'] or '')
		host = frame:expandTemplate{title = 'color box', args = {hostColor, '&nbsp;*&nbsp;', 'border=darkgray'}} ..'&nbsp;'.. host
	end
	
	local leading = ''
	if args['leading'] then
		leading = frame:expandTemplate{title = 'legend', args = {'#E9D66B', "'''Leading in that sport'''"}}
	end
	
	if legendpos == 't' then
		header = header .. host .. leading
	else
		footer = footer .. host .. leading
	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
	local row = root:tag('tr')
	
	if args['hide_rank'] then else
		row:tag('th')
			:wikitext('Rank')
	end
	row
		:tag('th')
			:wikitext(team)
		:tag('th')
			:addClass('headerSort')
			:css('width', '5em')
			:css('background-color', 'gold')
			:wikitext('Gold')
		:tag('th')
			:addClass('headerSort')
			:css('width', '5em')
			:css('background-color', 'silver')
			:wikitext('Silver')
		:tag('th')
			:addClass('headerSort')
			:css('width', '5em')
			:css('background-color', '#c96')
			:wikitext('Bronze')
		:tag('th')
			:css('width', '5em')
			:wikitext('Total')
	
	-- enumerate the rows
	local rowNums = {}
	
	for k,v in pairs(args) do
		k = ''..k
		local IOC = k:match('^gold_([A-Z][A-Z][A-Z])$') or k:match('^gold_(%d+)$')
		if IOC then
			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 
				frame:expandTemplate{title = flagTemplate, args = {IOC, event} }
			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
	
	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[6]
		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 (rank>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')
				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)
			
			if args['hide_rank'] then else
				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('th')
					:attr('scope', 'row')
					: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]$') or team:match('>[A-Z][A-Z][A-Z]<') then else team = team:lower() end
	
	local colspan 
	if args['hide_rank'] then 
		colspan = 1 
	else 
		colspan = 2	
	end
	if args['hide_totals'] then else
		root:tag('tr')
			:css('background-color', '#eaebef')
			:addClass('sortbottom')
			:tag('th')
				:wikitext('Totals ('..remainingEnd..' '..team..'s)')
				:attr('scope', 'row')
				:css('background-color', '#eaebef')
				:css('font-weight', 'bold')
				:attr('colspan', colspan)
			:tag('td')
				:wikitext(args['total_gold'] or totalGold)
				:css('font-weight', 'bold')
			:tag('td')
				:wikitext(args['total_silver'] or totalSilver)
				:css('font-weight', 'bold')
			:tag('td')
				:wikitext(args['total_bronze'] or totalBronze)
				:css('font-weight', 'bold')
			:tag('td')
				:wikitext(args['grand_total'] or totalGold+totalSilver+totalBronze)
				:css('font-weight', 'bold')
	end

	-- 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

--2001:B011:300D:2027:B4EE:1774:7DD9:5EA1留言2018年10月9日 (二) 00:30 (UTC)[回覆]

雖然我不反對創建模塊,但現在情況下我不支持讓Medals table模板正式使用,中文維基百科有一些特殊性,直接從英文維基百科那邊直接搬模板模塊進去恐怕會出一些問題(參考country alias模塊),這裏應該給懂代碼的先去修改一些地方以適應中文版,待測試沒問題之後再在條目名字空間中正式使用。--№.N留言2018年10月9日 (二) 03:12 (UTC)[回覆]
姑且先協助建立了,但若要啟用且變更重大,請徵得共識。--Xiplus#Talk 2018年11月16日 (五) 08:32 (UTC)[回覆]

新增first_place、second_place、third_place參數

因為主題不同,希望能針對條目修改金銀銅欄位顯示文字,新增first_place、second_place、third_place三個參數,望各位提供建議及確認:

Terry850324留言2022年2月18日 (五) 07:02 (UTC)[回覆]

我覺得可以。錦標賽多用冠亞季軍,綜合賽事多用金銀銅牌(如果我沒理解錯的話)。--#Beijing2022Day14 2022年2月18日 (五) 08:08 (UTC)[回覆]
我的理解也是這樣,所以已經在{{MedalistTable}}先加了這些參數,主要是沒編輯過Module,怕有疏漏。-Terry850324留言2022年2月18日 (五) 10:07 (UTC)[回覆]
不過實話實說,這個地方的自定義不需要設置的太過自由。是我的話會搞個比如medal_display_text之類的參數(參數名字隨便),1是金牌、銀牌、銅牌;2是冠軍、亞軍、季軍;3是第一名、第二名、第三名……然後默認值是1。當然如果真的有自由定義的需求的話(比如還有其他的情形需要設置成其他的文字),這樣搞也不是不行。--#Beijing2022Day14 2022年2月18日 (五) 10:24 (UTC)[回覆]
要找案例的話,目前羽球公開賽獎項只有頒到冠、亞軍,沒有季軍一說,這個情況若要套用此模組,可能會把third_place設會「準決賽(半決賽)」、「四強」,但目前公開賽條目幾乎未使用這個模組,我也很少接觸羽毛球以外主題,所以不敢確定。-Terry850324留言2022年2月18日 (五) 10:51 (UTC)[回覆]
確實哦,有的比賽不設三四名決賽,會有兩個並列季軍的。可能不用「季軍」這個術語的哦……具體怎麼設計方便一些還是視乎需求吧……--#Beijing2022Day14 2022年2月18日 (五) 11:48 (UTC)[回覆]
我覺得最初的建議不錯,如果不頒發前三獎項的賽事理應不會用到這個模組,我照這個模式修改,感謝建議。-Terry850324留言2022年2月19日 (六) 16:45 (UTC)[回覆]
目前來看,沒見過哪個獎牌榜只統計前二或前一的,全部都是統計前三,統計前四的也是設了並列第三的。--#Beijing2022Day16 2022年2月20日 (日) 02:04 (UTC)[回覆]

最終修改為新增medal_display_text參數,1 = 金牌、銀牌、銅牌;2 = 冠軍、亞軍、季軍;3 = 第一名、第二名、第三名。已更新至模組。另外修改{{MedalistTable}}為相同功能。-Terry850324留言2022年2月20日 (日) 04:46 (UTC)[回覆]

使用代碼並非IOC代碼

Template:Medals_table/doc】中提示使用IOC代碼,但該模塊實際使用的應該是ISO 3166-1代碼。IOC代碼中,BRN是巴林,但該模板會顯示汶萊。——三獵留言2024年7月21日 (日) 15:38 (UTC)[回覆]

@Liu116君是否有空關注一下?——三獵留言2024年7月21日 (日) 15:39 (UTC)[回覆]
只能說這屬於技術問題了,建議flag_template填flagIOC系列模板,如果不填或者填的是flagcountry、flag等模板,那就只能先用BHR來代替BRN了。當然,體育領域中BRN只會是巴林,按理來說應該調整相關代碼,不過我最近沒什麼時間去搞這事。--#Paris2024Countdown05Days 2024年7月21日 (日) 16:47 (UTC)[回覆]
@Liu116 那我改一下文檔,加一段説明。——三獵留言2024年7月22日 (一) 05:30 (UTC)[回覆]