Modulo:Tracce
Questo è un modulo scritto in Lua. Le istruzioni che seguono sono contenute nella sottopagina Modulo:Tracce/man (modifica · cronologia)
Sandbox: Modulo:Tracce/sandbox (modifica · cronologia) · Sottopagine: lista · Test: Modulo:Tracce/test
Questo modulo serve in appoggio al Template:Tracce per permettere l'inserimento di un numero illimitato di tracce.
Vedi il manuale del template per l'uso.
local categories = {}
local discontinuous_num = '[[Categoria:Voci con template Tracce con numerazione discontinua]]'
local missing_title = '[[Categoria:Voci con template Tracce con titolo mancante]]'
local unexpected_markup = '[[Categoria:Voci con template Tracce con markup inaspettato]]'
local unknown_format = '[[Categoria:Voci con template Tracce con formato durata sconosciuto]]'
-- Protegge una stringa che sarà avvolta dal markup wiki '' ... '' del corsivo
local function safeforitalics(str)
if str:sub(1,1) == "'" then str = "<span></span>" .. str end
if str:sub(-1,-1) == "'" then str = str .. "<span></span>" end
return str
end
-- Calcola e formatta la durata di una traccia
local function getTrackLength(args, i)
local string_durata = args["Durata" .. i]
local minuti, secondi
if string_durata then
local string_minuti, string_secondi = mw.ustring.match(string_durata, "^(%d+):(%d%d)$")
minuti = tonumber(string_minuti)
secondi = tonumber(string_secondi)
else
minuti = tonumber(args["Minuti" .. i])
secondi = args["Secondi" .. i] and mw.ustring.match(args["Secondi" .. i], "^%d%d?$") and
tonumber(args["Secondi" .. i])
end
local durata = (minuti or 0) * 60 + (secondi or 0)
if (string_durata and durata == 0) or (not minuti and args["Minuti" .. i]) or
(not secondi and args["Secondi" .. i]) then
categories[#categories+1] = unknown_format
end
return durata, durata > 0 and string.format("%d:%02.f", minuti or 0, secondi or 0) or nil
end
--[[
Riceve una lista di parametri e l'indice del gruppo di parametri da comporre
in una traccia
]]--
local function track(args, i, durata)
local titolo = args["Titolo" .. i]
local row_elements = {}
table.insert(row_elements, '<li value="' .. tostring(i) .. '">')
local interprete = args["Interprete" .. i]
if interprete then table.insert(row_elements, interprete .. " – ") end
local corsivo = args["Corsivo"] ~= "no" and true or false
corsivo = (corsivo and args["Corsivo" .. i] ~= "no") or
(not corsivo and (args["Corsivo" .. i] == "sì" or args["Corsivo" .. i] == "si"))
if titolo:find(corsivo and "''" or "'''") then
categories[#categories+1] = unexpected_markup
end
if corsivo then
titolo = "''" .. safeforitalics(titolo) .. "''"
end
table.insert(row_elements, titolo)
local featuring = args["Featuring" .. i]
if featuring then table.insert(row_elements, " <small>(<abbr title=featuring>feat.</abbr> " .. featuring .. ")</small>") end
local note = args["Note" .. i]
if note then table.insert(row_elements, " <small>(" .. note .. ")</small>") end
if durata then
table.insert(row_elements, " – ")
table.insert(row_elements, durata)
end
local AutoreTesto = args["Autore testo" .. i]
local AutoreMusica = args["Autore musica" .. i]
local AutoreTestoeMusica = args["Autore testo e musica" .. i]
local edizioni = args["Edizioni" .. i]
local no_autore = false
if AutoreTestoeMusica then
table.insert(row_elements, " <small>(" .. AutoreTestoeMusica )
elseif AutoreTesto then
table.insert(row_elements, " <small>(testo: " .. AutoreTesto)
if AutoreMusica then
table.insert(row_elements, " – musica: " .. AutoreMusica)
end
elseif AutoreMusica then
table.insert(row_elements, " <small>(musica: " .. AutoreMusica)
else
no_autore = true
end
if edizioni then
if no_autore then
table.insert(row_elements, " <small>(")
else
table.insert(row_elements, "; ")
end
table.insert(row_elements, "edizioni musicali " .. edizioni .. ")</small>")
elseif not no_autore then
table.insert(row_elements, ")</small>")
end
local extra = args["Extra" .. i]
if extra then table.insert(row_elements, " – " .. extra) end
table.insert(row_elements, "</li>\n")
local ListaMedley = args["ListaMedley" .. i]
if ListaMedley then table.insert(row_elements, '<div style="padding: 0.3em 0px 0.5em 2.5em;">\n' .. ListaMedley .. '</div>') end
return table.concat(row_elements), durata
end
--[[
Riceve una tabella di parametri e restituisce una stringa contenente
l'output da inserire nella voce
]]--
local function componi_tracce(args)
-- array per accumulare le righe della lista man mano che vengono elaborate
local tracks = {}
-- compone la testata dell'elenco tracce
local testata = ""
local no_autore = false
local autoreTestoeMusica = args["Autore testi e musiche"]
if autoreTestoeMusica then
testata = "Testi e musiche di " .. autoreTestoeMusica
else
local autoreTesti, autoreMusiche = args["Autore testi"], args["Autore musiche"]
if autoreTesti then
testata = "Testi di " .. autoreTesti
if autoreMusiche then testata = testata .. ", musiche di " .. autoreMusiche end
elseif autoreMusiche then
testata = "Musiche di " .. autoreMusiche
else
no_autore = true
end
end
local edizioni = args["Edizioni"]
if edizioni then
if no_autore then
testata = "Edizioni musicali " .. edizioni .."."
else
testata = testata .. "; edizioni musicali " .. edizioni .. "."
end
elseif not no_autore then
testata = testata .. "."
end
table.insert(tracks, testata)
-- compone la lista dei parametri
table.insert(tracks, "<ol>")
local visualizza_durata = args["Visualizza durata totale"]
local durata = 0
local somma_durata = false
if visualizza_durata == "si" or visualizza_durata == "sì" then somma_durata = true end
local nums = {}
local validated_nums = {}
for k, v in pairs(args) do
local prefix, num = string.match(k, '^(%D+)([0-9]+)$')
if num and (num == '0' or num:sub(1,1) ~= '0') then
-- se il numero viene modificato da tonumber, è troppo alto
num = tonumber(num)
if not args['Titolo' .. num] then
categories[#categories+1] = missing_title
end
if args[prefix .. num] and not validated_nums[num] then
validated_nums[num] = true
table.insert(nums, num)
end
end
end
table.sort(nums)
if #nums == 0 then
categories[#categories+1] = missing_title
-- categoria di tracciamento se la numerazione delle tracce è discontinua
elseif nums[1] + #nums - 1 ~= nums[#nums] then
categories[#categories+1] = discontinuous_num
end
local tracce = false
-- estrae le tracce dai parametri e inserisce i valori nella tabella tracks
for k, num in ipairs(nums) do
local durata_track, durata_formattata = getTrackLength(args, num)
if args['Titolo' .. num] then
tracce = true
local new_track = track(args, num, durata_formattata)
table.insert(tracks, new_track)
end
if somma_durata then durata = durata + durata_track end
end
table.insert(tracks, "</ol>")
if somma_durata then
table.insert(tracks, "Durata totale: " .. string.format("%d:%02.f", math.floor(durata/60) , durata % 60) .. "</p>")
end
local ns = mw.title.getCurrentTitle().namespace
return (tracce and table.concat(tracks, "\n") or '') .. (ns == 0 and table.concat(categories) or '')
end
local p = {}
--[[
Funzione di interfaccia con template:Tracce
Legge i parametri e li inserisce nella tabella args, che quindi passa a componi_tracce
per l'elaborazione
]]--
function p.tracce(frame)
-- ottiene i parametri del template originale
local pframe = frame:getParent()
-- estrae tutti i parametri e li memorizza in una tabella
local args = {}
for k, v in pairs(pframe.args) do
if v ~= nil and v ~= '' then
args[k] = v
end
end
return componi_tracce(args)
end
-- Tavola di conversione per i parametri di en:template:Track_listing non indicizzati
local conversion_table = {
['all_writing'] = 'Autore testi e musiche',
['all_lyrics'] = 'Autore testi',
['all_music'] = 'Autore musiche',
['total_length'] = 'Visualizza durata totale'
}
-- Tavola di conversione per i parametri di en:template:Track_listing indicizzati
local conversion_table_index ={
['title'] = 'Titolo',
['note'] = 'Note',
['music'] = 'Autore musica',
['writer'] = 'Autore testo e musica',
['lyrics'] = 'Autore testo',
['length'] = 'Durata',
['extra'] = 'Extra'
}
--[[
Funzione di interfaccia con template:Track_listing
Legge i parametri, li converte nei parametri corrispondenti del template:Tracce
e li inserisce nella tabella args, che quindi passa a componi_tracce
per l'elaborazione
]]--
function p.en_tracks(frame)
local pframe = frame:getParent()
-- estrae tutti i parametri e li memorizza in una tabella (pframe ritorna una pseudotabella, vedi documentazione)
local args = {}
local log = {}
for k, v in pairs(pframe.args) do
if v ~= nil and v ~= '' then
if conversion_table[k] then -- Controlla se è un parametro non indicizzato
args[conversion_table[k]] = v
else
-- estrae nome base e indice, se k=title1 allora prefix=title e num=1
local prefix, num = string.match(k, "^(%D+)(%d+)$")
if prefix and conversion_table_index[prefix] then -- controlla se è un parametro indicizzato
args[conversion_table_index[prefix] .. num] = v
elseif not num then -- non è neanche un parametro indicizzato, lo copia così com'è
args[k] = v
end
end
end
end
if args['Visualizza durata totale'] then args['Visualizza durata totale'] = 'si' end
return componi_tracce(args) -- table.concat(log, '\n*') --
end
return p
Content Disclaimer
Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.
- The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
- There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
- It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
- Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
- Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.