Ir al contenido

Módulo:Tabla general de medallas

De Wikipedia, la enciclopedia libre
Icono de documentación de módulo Documentación del módulo[ver] [editar] [historial] [purgar]

A continuación se muestra la documentación transcluida desde la subpágina /doc. [salta a la caja de código]


Uso

Véase: Plantilla:Tabla general de medallas


Esta documentación está transcluida desde Módulo:Tabla general de medallas/doc.
Por favor, añade las categorías en la subpágina de documentación y los interwikis en Wikidata. Subpáginas de este módulo.

require('Módulo:No globals')
local getArgs = require('Módulo:Argumentos').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)
	local Argumentos = frame.args;
	local parent     = frame:getParent();

	if not Argumentos[1] then
		Argumentos = parent.args;
	end

    local team = Argumentos['team'] or 'País'
	local root = mw.html.create()
	local anfitrion = Argumentos['anfitrion'] or ''
	local anfitrionColor = '#ccccff'
	local defaultRowColor = '#f8f9fa'
	local flagTemplate = Argumentos['plantilla_bandera']
	local evento = Argumentos['evento']
	local legendpos = (Argumentos['legend_position'] or 't'):lower()
	local header, footer = '', ''
	local totalOro = 0
	local totalPlata = 0
	local totalBronce = 0
	
	local remainingOro = 0
	local remainingPlata = 0
	local remainingBronce = 0
	local remainingStart = 0
	local remainingEnd = 0
	local limitReached = false
	local showLimit = tonumber(Argumentos['show_limit'])

	-- build the legend
	if anfitrion ~= '' then
		if Argumentos['name_' .. anfitrion] then
			anfitrion = Argumentos['name_' .. anfitrion]
			anfitrion = 'país anfitrión (' .. deflag(anfitrion) .. ')'
		elseif anfitrion:match('^([A-Z][A-Z][A-Z])') then
			anfitrion = frame:expandTemplate{title = flagTemplate, args = {anfitrion, evento} }
			anfitrion = 'país anfitrión (' .. deflag(anfitrion) .. ')'
		end
		anfitrion = anfitrion .. (Argumentos['anfitrion_note'] or '')
		anfitrion = frame:expandTemplate{title = 'color box', args = {anfitrionColor, '&nbsp;*&nbsp;', 'border=darkgray'}} ..'&nbsp;'.. anfitrion
	end
	
	local leading = ''
	if Argumentos['leading'] then
		leading = frame:expandTemplate{title = 'legend', args = {'#E9D66B', "'''Leading in that sport'''"}}
	end
	
	if legendpos == 't' then
		header = header .. anfitrion .. leading
	else
		footer = footer .. anfitrion .. leading
	end
	
	root = root
		:tag('table')
		:addClass('wikitable')
		:addClass('sortable')
		:addClass('plainrowheaders')
		:addClass('jquery-tablesorter')
		:css('text-align', 'center')
	
	root:tag('caption')
		:wikitext(Argumentos['título'])
	-- add the header row
	local row = root:tag('tr')
	
	if Argumentos['hide_rank'] then else
		row:tag('th')
			:attr('scope', 'col')
			:wikitext('Rank')
	end
	row
		:tag('th')
			:attr('scope', 'col')
			:wikitext(team)
		:tag('th')
			:attr('scope', 'col')
			:addClass('headerSort')
			:css('width', '4em')
			:css('background-color', 'gold')
			:wikitext('Oro')
		:tag('th')
			:attr('scope', 'col')
			:addClass('headerSort')
			:css('width', '4em')
			:css('background-color', 'silver')
			:wikitext('Plata')
		:tag('th')
			:attr('scope', 'col')
			:addClass('headerSort')
			:css('width', '4em')
			:css('background-color', '#C96')
			:wikitext('Bronce')
		:tag('th')
			:attr('scope', 'col')
			:css('width', '4em')
			:wikitext('Total')
	
	-- enumerate the rows
	local rowNums = {}
	local IOCs = {}
	for k,v in pairs(Argumentos) do
		k = ''..k
		local IOC = k:match('^oro_([A-Z][A-Z][A-Z])$') or k:match('^oro_(%d+)$')
			or k:match('^plata_([A-Z][A-Z][A-Z])$') or k:match('^plata_(%d+)$')
			or k:match('^bronce_([A-Z][A-Z][A-Z])$') or k:match('^bronce_(%d+)$')
		if IOC and IOCs[IOC] == nil then
			IOCs[IOC] = 1
			local oro   = (tonumber(Argumentos['oro_' .. IOC]) or 0)
			local plata = (tonumber(Argumentos['plata_' .. IOC]) or 0)
			local bronce = (tonumber(Argumentos['bronce_' .. IOC]) or 0)
			local noskip = Argumentos['skip_' .. IOC] and 0 or 1
			local pais = Argumentos['name_' .. IOC] or 
				frame:expandTemplate{title = flagTemplate, args = {IOC, evento} }
			pais = deflag(pais)
			if pais:match('%[%[[^%[%]%|]*%|([^%[%]]*)%]%]') then
				pais = pais:match('%[%[[^%[%]%|]*%|([^%[%]]*)%]%]')
			end
			if pais:match('%[%[([^%[%]%|]*)%]%]') then
				pais = pais:match('%[%[([^%[%]%|]*)%]%]')
			end
			table.insert(rowNums, {oro, plata, bronce, noskip, pais, IOC}) 
		end
	end
	if Argumentos['team'] == 'Games' then
		table.sort(rowNums, function (a,b)
			return  a[5] < b[5]
			end
		)
	else
		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
		)
	end
	
	local lastOro, lastPlata, lastBronce = -1
	local rank = 0
	local lastspan, lastrankcell = 1, nil
	for i, anum in ipairs(rowNums) do
		local IOC = anum[6]
		if Argumentos['skip_' .. IOC] then 
			lastOro, lastPlata, lastBronce, lastspan = -1, -1, -1, 1
		else 
			rank = rank + 1 
		end
		local pais = Argumentos['name_' .. IOC] or 
			frame:expandTemplate{title = flagTemplate, args = {IOC, evento} }
		local oro   = tonumber(Argumentos['oro_' .. IOC]) or 0
		local plata = tonumber(Argumentos['plata_' .. IOC]) or 0
		local bronce = tonumber(Argumentos['bronce_' .. IOC]) or 0
		local isanfitrion = Argumentos['anfitrion_' .. IOC]
		-- this is mainly for the parameter names example so you can override it.
		local total  = Argumentos['total_' .. IOC] or oro + plata + bronce
		local color = isanfitrion and anfitrionColor or defaultRowColor

		if Argumentos['grand_total'] then else
				totalOro = totalOro + oro
				totalPlata = totalPlata + plata
				totalBronce = totalBronce + bronce
		end

		if Argumentos['anfitrión_' .. IOC] then
			pais = pais .. '*'
		end

		if Argumentos['note_' .. IOC] then
			pais = pais .. Argumentos['note_' .. IOC]
		end

		if showLimit and (rank>showLimit) then 
			if remainingStart == 0 then remainingStart = rank end
			limitReached = true
			remainingOro = remainingOro + oro
			remainingPlata = remainingPlata + plata
			remainingBronce = remainingBronce + bronce
		else
			local row
			if Argumentos['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 Argumentos['hide_rank'] then else
				if (oro == lastOro) and (plata == lastPlata) and (bronce == lastBronce) then
					lastspan = lastspan + 1
					lastrankcell:attr('rowspan',lastspan)
				else
					lastspan = 1
					if Argumentos['skip_' .. IOC] then
						lastrankcell = row:tag('td'):wikitext(frame:expandTemplate{title = 'sort', args = {'999', '–'}})
					else
						lastrankcell = row:tag('td'):wikitext(rank)
						lastOro   = oro
						lastPlata = plata
						lastBronce = bronce
					end
				end
			end
			row:tag('th')
					:attr('scope', 'row')
					:css('background-color', color)
					:css('text-align','left')
					:wikitext(pais)
				:tag('td')
					:wikitext(oro)
				:tag('td')
					:wikitext(plata)
				:tag('td')
					:wikitext(bronce)
				: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(Argumentos['remaining_link'] or Argumentos['remaining_text'] or 'Remaining')
				:tag('td')
					:wikitext(remainingOro)
				:tag('td')
					:wikitext(remainingPlata)
				:tag('td')
					:wikitext(remainingBronce)
				:tag('td')
					:wikitext(remainingOro+remainingPlata+remainingBronce)
	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
	if team:match('^games$') then team = 'game' end
	
	local colspan 
	if Argumentos['hide_rank'] then 
		colspan = 1 
	else 
		colspan = 2	
	end
	if Argumentos['hide_totales'] then else
		root:tag('tr')
			:css('background-color', '#eaebef')
			:addClass('sortbottom')
			:tag('th')
				:wikitext('Totales ('..remainingEnd..' '..team..'es)')
				:attr('scope', 'row')
				:css('background-color', '#eaebef')
				:css('font-weight', 'bold')
				:attr('colspan', colspan)
			:tag('td')
				:wikitext(Argumentos['total_oro'] or totalOro)
				:css('font-weight', 'bold')
			:tag('td')
				:wikitext(Argumentos['total_plata'] or totalPlata)
				:css('font-weight', 'bold')
			:tag('td')
				:wikitext(Argumentos['total_bronce'] or totalBronce)
				:css('font-weight', 'bold')
			:tag('td')
				:wikitext(Argumentos['grand_total'] or totalOro+totalPlata+totalBronce)
				:css('font-weight', 'bold')
	end

	-- Build the rest of the footer
	if Argumentos['fuente'] or Argumentos['notes'] then
		if footer ~= '' then
			footer = footer .. '<br>'
		end
		footer = frame:expandTemplate{ title = 'refbegin' } .. footer
		
		if Argumentos['fuente'] then 
			footer = footer .. 'Fuente: ' .. Argumentos['fuente']
		end
		if Argumentos['notes'] then
			if Argumentos['fuente'] then
				footer = footer .. '<br>'
			end
			footer = footer .. 'Notes: ' .. Argumentos['notes']
		end
		footer = footer .. frame:expandTemplate{ title = 'refend' }
	end

	return header .. tostring(root) .. footer
end

return p