Moduuli:Kappalelista

Wikipediasta
Siirry navigaatioon Siirry hakuun

-- This module implements [[Malline:Kappalelista]]
-- Tuotu es-wikistä
-- -------------------------------------------------------------------------- --
-- Lista de canciones
--
-- Plantilla:       {{Lista de canciones}}
-- Versión:         1.0.3
-- Autor:           Jose Buelvas (Usuario:Iho jose)
-- -------------------------------------------------------------------------- --

-- Variables
local p	            = {}
local args

local isCollapsed = 0
local hasColLyricsComposer = 0
local hasColLyrics = 0
local hasColComposer = 0
local hasColArranger = 0
local hasColExtra = 0

-- Union
function union(t1, t2)
	local vals = {}
	for k, v in pairs(t1) do
		vals[v] = true
	end
	
	for k, v in pairs(t2) do
		vals[v] = true
	end
	
	local ret = {}
	for k, v in pairs(vals) do
		table.insert(ret, k)
	end
	
	return ret
end

-- Depuración de etiquetas vacías
local function depuracion(contenido)
	if contenido and contenido ~= '' then
		return contenido
	end
end

-- Obtener números en parámetros {{{1}}}
local function obtenerNumero(prefijo)
	local nums = {}
	for k, v in pairs(args) do
		local num = tostring(k):match('^' .. prefijo .. '([1-9]%d*)$')
		
		if num then
			table.insert(nums, tonumber(num))
		end
	end
	
	table.sort(nums)
	return nums
end

-- helper for checking with mixed languages
local function isEnabled(feature)
	if args[feature] and 
           (args[feature] == 'yes' or args[feature] == 'kyllä') then
		return true
	end

	return false
end

-- fix duration format from colon to dot where necessary:
-- this is for when duration have been imported from somewhere else and need conversion
local function fixDurationformat(duration)
	if duration == nil then
		-- Do nothing if no duration specified
		return nil
	end

	local hours, minutes, seconds

	-- Try to match times like "1:23:45".
	hours, minutes, seconds = duration:match('^(%d+):(%d%d):(%d%d)$')
	if hours and minutes and seconds then
		duration = hours .. '.' .. minutes .. '.' .. seconds
		return duration
	end

	-- The previous attempt didn't match. Try to match times like "1:23".
	minutes, seconds = duration:match('^(%d?%d):(%d%d)$')
	if minutes and seconds then
		-- allow minutes.seconds even if either is zero (it is used in some articles)
		duration = minutes .. '.' .. seconds
		return duration
	end

	-- if there is no colon:
	-- it is not in colon-format and just return as-is (no fix necessary)
	return duration
end

-- Información previa
function informacion(parent)
	local infodisco = parent:wikitext()
	if args['kaikki_säv&san'] or args['all_writing'] then -- {{{todas_escritas}}}
		local writer = ''
		if args['all_writing'] then
			writer = args['all_writing']
		elseif args['kaikki_säv&san'] then
			writer = args['kaikki_säv&san']
		end
		infodisco:wikitext('Kaikki kappaleet säv. ja san. ' .. writer .. '. ')
	else
		if args['kaikki_sanat'] or args['all_lyrics'] then -- {{{todas_letras}}}
			local lyrics = ''
			if args['all_lyrics'] then
				lyrics = args['all_lyrics']
			elseif args['kaikki_sanat'] then
				lyrics = args['kaikki_sanat']
			end
			
			infodisco:wikitext('Kaikki sanat ' .. lyrics)
			if not args['all_music'] and not args['kaikki_sävellykset'] then
				infodisco:wikitext('.')
			end
		end
		
		local compositor = ''
		if args['all_music'] then
			compositor = args['all_music']
		elseif args['kaikki_sävellykset'] then
			compositor = args['kaikki_sävellykset']
		end

		-- huom, voi jatkaa edellisestä..		
		if args['all_music'] or args['kaikki_sävellykset'] then -- {{{all_music| {{{toda_la_música}}}
			if args['kaikki_sanat'] or args['all_lyrics'] then -- Unión de las dos frases con coma (,)
				infodisco:wikitext(', kaikki sävellykset ' .. compositor .. '. ')
			else
				infodisco:wikitext('Kaikki sävellykset ' .. compositor .. '. ')
			end
		end
	end
end

-- Agregar filas a la tabla de contenido global
-- (rivin solut)
local function agregarPista(parent, filaArgs)
	-- Variables globales internas
	local pista = parent:tag('tr')
	pista
		:tag('td')
			:cssText('padding-right: 10px; text-align: right; cursor: default; color: black; background-color: ' .. filaArgs.bgcolor)
			:wikitext(filaArgs.numero .. '.') -- Número de la pista (  1.)
	
	-- Título de la pista y notas
	local titulopista, notaspista
	if filaArgs.titulo and filaArgs.titulo ~= '' then
		titulopista = filaArgs.titulo
	else
		titulopista = "''Kappaleen nimi''"
	end
	if filaArgs.notas and filaArgs.notas ~= '' then
		notaspista = ' <span style="font-size: smaller; color: #444">(' .. filaArgs.notas .. ')</span> '
	else
		notaspista = ''
	end
	
	local cellcolor = 'color: black; background-color: ' .. filaArgs.bgcolor
	local nombrepista = pista:tag('td')
	nombrepista
		:cssText('text-align: left; ' .. cellcolor)
		:wikitext('' .. titulopista .. '' .. notaspista)
	
	-- Créditos letras
	if hasColLyrics == true then
		local letraspista = pista:tag('td')
		letraspista
			:cssText(cellcolor)
			:wikitext(filaArgs.letraspor)
	end
	
	-- Créditos música
	if hasColComposer == true then
		local musicapista = pista:tag('td')
		musicapista
			:cssText(cellcolor)
			:wikitext(filaArgs.musicapor)
	end
	
	-- Créditos escritores
	if hasColLyricsComposer == true then
		local escritopista = pista:tag('td')
		escritopista
			:cssText(cellcolor)
			:wikitext(filaArgs.escritopor)
	end

	if hasColArranger == true then
		local cellarranger = pista:tag('td')
		cellarranger
			:cssText(cellcolor)
			:wikitext(filaArgs.arranger)
	end
	
	-- Columna extra
	if hasColExtra == true then
		local columnapista = pista:tag('td')
		columnapista
			:cssText(cellcolor)
			:wikitext(filaArgs.extrapor)
	end
	
	-- Duración de la pista
	local duracionpista = pista:tag('td')
	duracionpista
		:cssText('padding-right: 10px; text-align: right; ' .. cellcolor)
		:wikitext(filaArgs.duracionpor)
end

-- Hacer títulos de la tabla
-- (otsikko, sarakkeiden nimet)
local function titulosContenido(parent)
        local listHeading = ''
        if isCollapsed == true then
                listHeading = args['headline'] or args['otsake'] or 'Kappalelista'
        else
                listHeading = args['headline'] or args['otsake'] or ''
	end
	local encabezado = parent:tag('tr')
	encabezado
		:tag('th')
			:addClass('tlheader')
			:attr('colspan', 10)
			:cssText('text-align: left; background-color: #fff; width: 100%;')
			:wikitext(listHeading)
	
	local estilopista, totalnum, estilomore
	totalnum  = 0
	if hasColLyrics == true then
		totalnum  = totalnum + 1
	end
	if hasColComposer == true then
		totalnum  = totalnum + 1
	end
	if hasColLyricsComposer == true then
		totalnum  = totalnum + 1
	end
	if hasColArranger == true then
		totalnum  = totalnum + 1
	end
	if hasColExtra == true then
		totalnum  = totalnum + 1
	end
	-- Estilizado
	if totalnum == 0 then
		estilopista = 'width: 100%'
		estilomore = ''
	elseif totalnum == 1 then
		estilopista = 'width: 60%'
		estilomore = 'width: 40%'
	elseif totalnum == 2 then
		estilopista = 'width: 40%'
		estilomore = 'width: 30%'
	elseif totalnum == 3 then
		estilopista = 'width: 30%'
		estilomore = 'width: 20%'
	elseif totalnum >= 4 then
		estilopista = 'width: 20%'
		estilomore = 'width: 20%'
	end
	
	local cellcolor = 'background-color: #eee; color: black;'
	local titulares = parent:tag('tr')
	titulares
		:tag('th')
		:cssText('width: 20px; padding-left: 10px; padding-right: 10px; text-align: center; height: 20px; ' .. cellcolor)
		:wikitext('Nro') -- Número de la pista
		
	local saraketyyli = 'text-align: left; ' .. cellcolor .. ' ' .. estilomore
	local nombrepista = titulares:tag('th')
	nombrepista
		:cssText('text-align: left; ' .. cellcolor .. ' ' .. estilopista)
		:wikitext('Nimi')
	
	if hasColLyrics == true then
		local letrista = titulares:tag('th')
		letrista
			:cssText(saraketyyli)
			:wikitext('Sanat')
	end
	if hasColComposer == true then
		local musicapista = titulares:tag('th')
		musicapista
			:cssText(saraketyyli)
			:wikitext('Sävel')
	end
	if hasColLyricsComposer == true then
		local escritorespista = titulares:tag('th')
		escritorespista
			:cssText(saraketyyli)
			:wikitext('Säv&san')
	end
	if hasColArranger == true then
		local colarr = titulares:tag('th')
		colarr
			:cssText(saraketyyli)
			:wikitext('Sovitus')
	end
	if hasColExtra == true then
		local columnaextra = titulares:tag('th')
		columnaextra
			:cssText(saraketyyli)
			:wikitext(args['extra_column'] or args['ylim_sarake'])
	end
	
	local duraciones = titulares:tag('th')
	duraciones
		:cssText('padding-right: 5px; width: 60px; text-align: center; ' .. cellcolor)
		:wikitext('Kesto')
	
	local cel5 = titulares:tag('td')
	cel5:addClass('mbox-empty-cell')
end

-- Hacer las filas consecutivamente según sean escritas
local function hacerLista(parent)
	-- Detectar idioma del parámetro (inglés/español)
	local numeroPista = union(obtenerNumero('nimi'), obtenerNumero('title'))
	table.sort(numeroPista)
	
	for k, num in ipairs(numeroPista) do
		-- Color de fondo de la fila (Pista)
		local colorfondo
		if (num % 2) == 0 then
			colorfondo = '#f7f7f7'
		else
			colorfondo = '#fff'
		end
		
		-- Agrega los datos según el parámetro (en inglés o español)
		agregarPista(parent, {
			bgcolor		= colorfondo,
			numero		= num,
			titulo		= depuracion(args['nimi' .. num] or args['title' .. num]),
			notas		= depuracion(args['huom' .. num] or args['note' .. num]),
			letraspor	= depuracion(args['sanat' .. num] or args['lyrics' .. num]),
			musicapor	= depuracion(args['sävellys' .. num] or args['music' .. num]),
			escritopor	= depuracion(args['säv&san' .. num] or args['writer' .. num]),
			arranger    = depuracion(args['sovitus' .. num] or args['arranger' .. num]),
			extrapor	= depuracion(args['ylim' .. num] or args['extra' .. num]),
			duracionpor	= depuracion(fixDurationformat(args['pituus' .. num] or args['length' .. num]))
		})
	end
end

-- Duración total
local function duracionTotal(parent)
	local duraciontotal = parent:tag('tr')
	if args['total_length'] or args['kokonaiskesto'] then
		local kesto = ''
		if args['total_length'] then
			kesto = args['total_length']
		elseif args['kokonaiskesto'] then
			kesto = args['kokonaiskesto']
		end

		-- fix timeformat from colon-format to dot-format where necessary
		kesto = fixDurationformat(kesto)
		duraciontotal
			:tag('td')
				:attr('colspan', 10)
				:cssText('padding-right: 10px; text-align: right; background-color: #fff; color: black; border-width: 0; font-weight: bold')
				:wikitext(kesto)
		local cel8 = duraciontotal:tag('td')
		cel8:addClass('mbox-empty-cell')
	end
end

-- Vericando que el parámetro tenga datos
function existenciaDatos(pista)
	-- TRUE si tiene datos FALSE por defecto
	if depuracion(pista[1]) or depuracion(pista['otsake']) then
		return true
	end
	
	return false
end

-- Hacer lista de pistas en la tabla
function hacerListaTabla(parent, tabla)
	for k, pista in ipairs(tabla) do
		if existenciaDatos(pista) then
			agregarPista(parent, {
				titulo		= depuracion(pista['nimi'] or pista['title']),
				notas		= depuracion(args['huom'] or args['note']),
				letraspor	= depuracion(args['sanat'] or args['lyrics']),
				musicapor	= depuracion(args['sävellys'] or args['music']),
				escritopor	= depuracion(args['säv&san'] or args['writer']),
				arranger	= depuracion(args['sovitus'] or args['arranger']),
				extrapor	= depuracion(args['ylim'] or args['extra']),
				duracionpor	= depuracion(args['pituus'] or args['length'])
			})
		end
	end
end

-- Plantilla de lista de canciones
function _canciones()
	--Variables
	local listclass
        local estilos
	
	-- Condicionales en _canciones
	if isEnabled('collapsed') or isEnabled('piilotettu') then
		listclass = 'collapsible collapsed'
		estilos = 'border: #aaa 1px solid; padding: 3px;'
		isCollapsed = true
	else
		listclass = ''
		estilos = 'padding: 4px;'
	end

	if isEnabled('lyrics_credits') or isEnabled('sarake_sanat') then
		hasColLyrics = true
	end
	if isEnabled('music_credits') or isEnabled('sarake_sävellys') then
		hasColComposer = true
	end
	if isEnabled('arranger_credits') or isEnabled('sarake_sovitus') then
		hasColArranger = true
	end
	if isEnabled('writing_credits') or isEnabled('sarake_säv&san') then
		hasColLyricsComposer = true
	end
	if args['extra_column'] or args['ylim_sarake'] then
		hasColExtra = true
	end
	
	-- Constructor global HtmlBuilder
	local rootnode = mw.html.create()

	-- Información
	informacion(rootnode)

	local divParent = rootnode:tag('div')
	divParent
		:addClass('tracklist')
		:css('max-width', args['leveys'] or '50em')
                :cssText(estilos)
	
	-- Tabla contenedora
	local listataulu = divParent:tag('table')
	listataulu
		:addClass('tracklist ')
		:addClass(listclass)
		:attr('cellpadding', 0)
		:cssText('width: 100%; border-width: 0px; border-collapse: collapse;')
	
	-- Indexar secciones
	titulosContenido(listataulu)
	if not args[1] then
		hacerLista(listataulu)
	else
		hacerListaTabla(listataulu, args)
	end
	duracionTotal(listataulu)
	
	-- Indexar todo en variable global
	return tostring(rootnode)
end

-- Index
function p.lista(frame)
	local origArgs
	-- Llamado en #invoke par uso de etiquetas de plantilla
	if frame == mw.getCurrentFrame() then
		origArgs = frame:getParent().args
	else
		origArgs = frame
	end
	-- For pairs
	args = {}
	for k, v in pairs(origArgs) do
		if v ~= ''  then
			args[k] = v
		end
	end
	
	-- Indexar plantilla de lista de canciones
	return _canciones()
end

return p