Módulo:Recepción crítica en televisión

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

Crea una Plantilla:Recepción crítica en televisión estándar con

{{#invoke:Recepción crítica en televisión|main}}

Con los parámetros definidos en la documentación de la plantilla.

Categoría de seguimiento




Esta documentación está transcluida desde Módulo:Recepción crítica en televisión/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.

-- Este módulo implementa {{Recepción crítica en televisión}}.

require('Módulo:No globals')
local yesno = require('Módulo:Yesno')
local HTMLcolor = mw.loadData( 'Módulo:Color contrast/colors' )

--------------------------------------------------------------------------------
-- Clase CriticalResponse (RecepciónCrítica).
-- La clase principal.
--------------------------------------------------------------------------------

local CriticalResponse = {}

function CriticalResponse.cellspan(SeasonEntries, SeasonEntries_ordered, key, cell, multipart)
	local spanlength = 1
	
	for i = cell+1, #SeasonEntries_ordered do
		local entry = SeasonEntries[SeasonEntries_ordered[i]]
		-- Temporada dividida, entonces temporada común
		if entry.startA then
			if not entry[key..'A'] then spanlength = spanlength + 1
			else break end
			if not entry[key..'B'] then spanlength = spanlength + 1
			else break end
		else
			if not entry[key] and (not entry.special and multipart) then
				spanlength = spanlength + 1
			else break end
		end
	end
	return spanlength
end

-- Función de clasificación
function CriticalResponse.series_sort(op1, op2)
	local n1,s1 = string.match(op1,"(%d+)(%a*)")
	local n2,s2 = string.match(op2,"(%d+)(%a*)")
	local n1N,n2N = tonumber(n1),tonumber(n2)

	if n1N == n2N then
		return s1 < s2
	else
		return n1N < n2N
	end
end

-- Función para agregar texto o {{TableTBA}} a la celda
function CriticalResponse.season_cell(text, frame)
	local cell
	
	if string.find(text or '', 'table-na', 0, true) ~= nil then
		local findpipe = string.find(text, ' | ', 0, true)
		if findpipe ~= nil then
			cell = CriticalResponse.series_attributes( frame:expandTemplate{title='TableTBA',args={string.sub(text,findpipe+3)}} )
		else
			cell = CriticalResponse.series_attributes( frame:expandTemplate{title='TableTBA'} )
		end
	else
		cell = mw.html.create('td'):wikitext(text)
	end
	
	return cell
end

-- Permitir usos de celdas {{TableTBA}}
function CriticalResponse.series_attributes(infoParam)
	local entries = {}
	local infoCell = mw.html.create('td')
	local attrMatch = '([%a-]*)="([^"]*)"'
	
	while true do
		local a,b = string.match(infoParam,attrMatch)
		if a == nil or b == nil then break end
		infoCell:attr(a,b)
		infoParam = string.gsub(infoParam,attrMatch,'',1)
	end

	infoParam = string.gsub(infoParam,'%s*|%s*','',1)
	infoCell:wikitext(infoParam)
	
	return infoCell
end

function CriticalResponse.new(frame, args)
	args = args or {}
	
	local initialArticle = args['1'] or ''
	local categories = ''
	local title = mw.title.getCurrentTitle()
	local hide_rotten_tomatoes = yesno(args.ocultar_rotten_tomatoes)
	local hide_metacritic = yesno(args.ocultar_metacritic)

	-- Crear tabla de recepción crítica
	local root = mw.html.create((args.multiserie or not args.serie) and 'table' or '')
	local cellPadding = '0 8px'
	local basePadding = '0.2em 0.4em'
	
	root
		:addClass('wikitable')
		:addClass('plainrowheaders')
		:css('text-align', 'center')
		
	-- Eliminar flotante si hay varias series
	if not args.multiserie then
		root:css('float', 'right')
		root:css('margin', '10px')
	end
	
	-- Clasificable (sortable)
	if args.sortable or args.multiserie then
		root:addClass('sortable');
	end
	
	-- Ancho
	if args.ancho then
		root:css('width', args.ancho)
	end

	-- Subtítulo
	if args['subtítulo'] then
		root:tag('caption'):wikitext(args['subtítulo'])
	end

	-- Extraiga información de temporadas y colóquela en una matriz 3D
	local SeasonEntries = {}
	for k,v in pairs(args) do
		local str, num, str2 = string.match(k, '([^%d]*)(%d*)(%a*)')
		if num ~= '' then
			-- Especial (Special)
			local special = false
			if string.sub(str2,1,1) == 'E' then
				special = true
				num = num .. str2
				str2 = ''
			end
			-- Agregar a las entradas, crearlas si es necesario
			if not SeasonEntries[num] then
				SeasonEntries[num] = {}
			end
			SeasonEntries[num][str .. str2] = v
			if special then
				SeasonEntries[num]['special'] = 'y'
			end
		end
	end

	-- Ordenar tabla por número de temporada
	local SeasonEntries_ordered = {}
	for k in pairs(SeasonEntries) do
		table.insert(SeasonEntries_ordered, k)
	end
	table.sort(SeasonEntries_ordered,CriticalResponse.series_sort)
	
	local firstRow = args.multiserie and {} or SeasonEntries[SeasonEntries_ordered[1]]

	-- HEncabezados
	do
		if args.multiserie or not args.serie then
			local headerRow = root:tag('tr')
			headerRow
				:css('text-align', 'center')
			
			-- Encabezado de múltiples series
			if args.multiserie then
				headerRow:tag('th')
					:attr('scope', 'col')
					:css('padding', cellPadding)
					:wikitext('Series')
			end
			
			-- Encabezado de temporada
			headerRow:tag('th')
				:attr('scope', 'col')
				:attr('colspan', 2)
				:css('min-width', '50px')
				:css('padding', cellPadding)
				:addClass('unsortable')
				:wikitext(args.serieT or args.temporadaT or 'Temporada')

			-- Encabezado de Rotten Tomatoes
			if not hide_rotten_tomatoes then
				headerRow:tag('th')
					:attr('scope', 'col')
					:wikitext('[[Rotten Tomatoes]]')
			end

			-- Encabezado de Metacritic
			if not hide_metacritic then
				headerRow:tag('th')
					:attr('scope', 'col')
					:wikitext('[[Metacritic]]')
			end
		end
	end

	-- Filas de temporada
	do
		if args.multiserie then
			-- Entradas individuales de series múltiples
			if args.multiserie ~= "y" then
				root:node(args.multiserie)
			end
		else
			-- Entradas de una fila, solo clasificadas en el espacio principal
			if title.namespace == 0 and #SeasonEntries == 1 then
				categories = categories .. '[[Categoría:Wikipedia:Artículos que usan Plantilla:Recepción crítica en televisión con solo una fila]]'
			end
		
			--Determinar el número de filas en toda la tabla
			local SeasonEntriesRows = 0
			for X = 1, #SeasonEntries_ordered do
				local season, entry = SeasonEntries_ordered[X], SeasonEntries[SeasonEntries_ordered[X]]
				SeasonEntriesRows = SeasonEntriesRows + 1
			end
			
			for X = 1, #SeasonEntries_ordered do
				local season, entry = SeasonEntries_ordered[X], SeasonEntries[SeasonEntries_ordered[X]]
				
				-- Filas de temporada para cada temporada
				for k0 = string.byte('A')-1, string.byte('Z') do
					local k = string.char(k0)
					if k0 == string.byte('A')-1 then k = '' end
					
					-- Nueva fila de temporada
					local seasonRow = (entry['rotten_tomatoes' .. k] or entry['metacritic' .. k]) and root:tag('tr') or mw.html.create('tr')
					
					-- Nombre de la serie para resumenes de grupo
					if X == 1 and (k == '' or k == 'A') and args.serie then
						seasonRow:tag('th')
							:attr('scope', 'row')
							:attr('rowspan', SeasonEntriesRows)
							:wikitext(args.serie)
					end
					
					-- Celda de color
					if entry['color' .. k] ~= nil and HTMLcolor[entry['color' .. k]] == nil then 
						entry['color' .. k] = '#'..(mw.ustring.match(entry['color' .. k], '^[%s#]*([a-fA-F0-9]*)[%s]*$') or '')
					end
					seasonRow:tag('td')
						:css('background',entry['color' .. k])
						:css('width','10px')

					-- Enlace de número de temporada, incluido solo en la primera fila
					if k == '' or k == 'A' then
						seasonRow:tag('th')
							:attr('scope', 'row')
							:attr('colspan', entry.special or 1)
							:css('text-align', 'center')
							:wikitext((entry.enlace and '[[' .. entry.enlace .. '|' .. (entry.enlaceT or season) .. ']]' or (entry.enlaceT or season)) .. (entry.enlaceR or ''))
					end
				
					-- Rotten Tomatoes
					if not hide_rotten_tomatoes and entry['rotten_tomatoes' .. k] ~= 'metacritic' then
						if entry['rotten_tomatoes' .. k] then
							local thisCell = CriticalResponse.season_cell(entry['rotten_tomatoes' .. k], frame)
								:css('padding',basePadding)
							seasonRow:node(thisCell)
						else
							local infoCell = CriticalResponse.series_attributes( frame:expandTemplate{title='TableTBA'} )
							infoCell:css('padding',basePadding)
							seasonRow:node(infoCell)
						end
					end
					
					-- Metacritic
					if not hide_metacritic and entry['metacritic' .. k] ~= 'rotten_tomatoes' then
						if entry['metacritic' .. k] then
							local thisCell = CriticalResponse.season_cell(entry['metacritic' .. k], frame)
								:css('padding',cellPadding)
							seasonRow:node(thisCell)
						else
							local infoCell = CriticalResponse.series_attributes( frame:expandTemplate{title='TableTBA'} )
							infoCell:css('padding',cellPadding)
							seasonRow:node(infoCell)
						end
					end
				
				end -- End k0 string.byte
			end -- End 'for' SeasonEntries_ordered
		end -- End 'if' multiseries
	end -- End 'do' season rows

	return tostring(root) .. categories
end

--------------------------------------------------------------------------------
-- Exports
--------------------------------------------------------------------------------

local p = {}

function p.main(frame)
	local args = require('Módulo:Arguments').getArgs(frame, {
		wrappers = 'Plantilla:Recepción crítica en televisión'
	})
	return CriticalResponse.new(frame, args)
end

return p