« Module:Pistes » : différence entre les versions
Apparence
Modèle:Pistes>Od1n correctif pour le cas où le modèle contiendrait inopinément des paramètres non-nommés (problème signalé ici) ; la cause sous-jacente est que les paramètres sont stockés dans une unique table, pour éviter la confusion il faudrait utiliser deux tables séparées (une pour les "foobar42" et une autre pour le reste) |
Modèle:Pistes>Od1n oh le bug traître… : lorsque c'est un paramètre non-nommé, la clé est de type "number", du coup « k:fonctionDeString() » ça plante |
||
| Ligne 26 : | Ligne 26 : | ||
-- all other parameter names, except numerical ones, because args[k] would be confused with args[nb] | -- all other parameter names, except numerical ones, because args[k] would be confused with args[nb] | ||
-- (luckily, the template doesn't use unnamed parameters) | -- (luckily, the template doesn't use unnamed parameters) | ||
elseif | elseif type(k) ~= 'number' then | ||
local trimmed = mw.text.trim(v) | local trimmed = mw.text.trim(v) | ||
if trimmed ~= "" then | if trimmed ~= "" then | ||
Version du 23 juin 2024 à 12:25
La documentation pour ce module peut être créée à Module:Pistes/doc
local p = {}
local langue = require 'Module:Langue'
local nbPistes = 0
function p.main( frame )
-- 1. Lecture des paramètres
local args = {}
for k, v in pairs(frame:getParent().args) do
-- match: foobar42, foo21bar42 (digits in base name), 21foo42 (leading digits in base name), foobar04 (leading zeroes)
-- do not match: 42 (only digits; the regex matches actually, but we reject in the following conditional), foobar (no digits suffix)
local param, nb = mw.ustring.match( k, '^(.-)(%d+)$' )
if param and param ~= '' then
nb = tonumber(nb)
if args[nb] == nil then
if nb > nbPistes then
nbPistes = nb
end
args[nb] = {}
end
if v ~= "" or param == "numero" then -- il est possible de renseigner des paramètres « numeroN » vides
args[nb][param] = v
end
-- all other parameter names, except numerical ones, because args[k] would be confused with args[nb]
-- (luckily, the template doesn't use unnamed parameters)
elseif type(k) ~= 'number' then
local trimmed = mw.text.trim(v)
if trimmed ~= "" then
args[k] = trimmed
end
end
end
-- 2. Initialisation du tableau de la liste des titres
local divPistes = mw.html.create( 'div' )
:addClass( "overflow pistes-marge" )
:css({
['margin-bottom'] = (args.total_temps or args.total_length) and '0.5em' or '1em'
})
local tabPistes = divPistes
:tag( "table" )
:addClass( "tracklist" .. (args.collapsed == 'oui' and " collapsible collapsed" or "") )
:attr( 'cellpadding', "0" )
-- 3. Éventuel code avant l'entête du tableau (titre ou boîte repliable)
if args.titre or args.headline then
tabPistes
:tag( "tr" )
:tag( "th" )
:addClass( "tlheader pistes-titre" )
:attr( "scope", "col" )
:attr( "colspan", "10" )
:wikitext( (args["langue titre"] or args["langue titres"]) and langue.langue({ args["langue titre"] or args["langue titres"], args.titre or args.headline }) or (args.titre or args.headline) )
elseif args.collapsed == 'oui' then
tabPistes
:tag( "tr" )
:tag( "th" )
:addClass( "tlheader pistes-titre" )
:attr( "colspan", "10" )
:wikitext( " " )
end
-- 4. Création de l'entête du tableau
local nbColonnesExtra = ((args.credits_paroles or args.lyrics_credits) and 1 or 0) + ((args.credits_musique or args.music_credits) and 1 or 0) + ((args.credits_ecriture or args.writing_credits) and 1 or 0) + ((args.colonne_extra or args.extra_column) and 1 or 0)
local entetesPistes = tabPistes
:tag( "tr" )
:tag( "th" ) -- Colonne « Numéro »
:addClass( "tlheader pistes-entete" )
:attr( "scope", "col" )
:css({
width = '20px',
['padding-left'] = '10px',
['padding-right'] = '10px',
['text-align'] = 'right'
})
:wikitext( '<abbr class="abbr" title="Numéro">N<sup>o</sup></abbr>' ) -- résultat de {{Numéro avec majuscule|espace=non}}
:done()
:tag( "th" ) -- Colonne « Titre »
:addClass( "tlheader pistes-entete" )
:attr( "scope", "col" )
:css({
width = ({[0]='100%', [1]='60%', [2]='40%', [3]='30%', [4]='20%'})[nbColonnesExtra]
})
:wikitext( "Titre" )
:done()
if args.credits_paroles == 'oui' or args.lyrics_credits == 'oui' then -- Colonne « Paroles »
entetesPistes
:tag( "th" )
:addClass( "pistes-entete" )
:attr( "scope", "col" )
:css({
width = ({[1]='40%', [2]='30%', [3]='20%', [4]='20%'})[nbColonnesExtra]
})
:wikitext( "Paroles" )
:done()
end
if args.credits_musique == 'oui' or args.music_credits == 'oui' then -- Colonne « Musique »
entetesPistes
:tag( "th" )
:addClass( "pistes-entete" )
:attr( "scope", "col" )
:css({
width = ({[1]='40%', [2]='30%', [3]='20%', [4]='20%'})[nbColonnesExtra]
})
:wikitext( "Musique" )
:done()
end
if args.credits_ecriture == 'oui' or args.writing_credits == 'oui' then -- Colonne « Auteur »
entetesPistes
:tag( "th" )
:addClass( "pistes-entete" )
:attr( "scope", "col" )
:css({
width = ({[1]='40%', [2]='30%', [3]='20%', [4]='20%'})[nbColonnesExtra]
})
:wikitext( "Auteur" )
:done()
end
if args.colonne_extra or args.extra_column then -- Colonne extra
entetesPistes
:tag( "th" )
:addClass( "pistes-entete" )
:attr( "scope", "col" )
:css({
width = ({[1]='40%', [2]='30%', [3]='20%', [4]='20%'})[nbColonnesExtra]
})
:wikitext( args.colonne_extra or args.extra_column )
:done()
end
entetesPistes
:tag( "th" ) -- Colonne « Durée »
:addClass( "tlheader pistes-entete" )
:attr( "scope", "col" )
:css({
width = '60px',
['padding-right'] = '10px',
['text-align'] = 'right'
})
:wikitext( "Durée" )
:done()
-- 5. Tracé des lignes du tableau
for i = 1, nbPistes do
if args[i] and (args[i].piste or args[i].title or args[i].note or args[i].temps or args[i].length) then
local ligne = tabPistes:tag( 'tr' )
local numero
if args[i].numero == '' then
numero = ''
else
numero = (args[i].numero or tostring(i)) .. '.'
end
ligne
:addClass( (i%2 == 0) and 'pistes-pair' or 'pistes-impair' )
:tag( 'th' )
:addClass( "pistes-numero" )
:attr{ scope = 'row' }
:wikitext( numero )
local titrePiste = args[i].piste or args[i].title
if titrePiste then
if not langue.nonLatin( titrePiste ) then
titrePiste = "''" .. titrePiste .. "''"
end
if args[i]["langue titre"] or args["langue titres"] then -- "langue titreN" puis "langue titres"
titrePiste = langue.langue({ args[i]["langue titre"] or args["langue titres"], titrePiste })
end
else
titrePiste = 'Sans titre'
end
ligne:tag( 'td' ):wikitext(titrePiste .. (args[i].note and (' <small>(' .. args[i].note .. ')</small>') or '') )
if args.credits_paroles == 'oui' or args.lyrics_credits == 'oui' then
ligne:tag( 'td' ):wikitext( args[i].paroles or args[i].lyrics )
end
if args.credits_musique == 'oui' or args.music_credits == 'oui' then
ligne:tag( 'td' ):wikitext( args[i].musique or args[i].music )
end
if args.credits_ecriture == 'oui' or args.writing_credits == 'oui' then
ligne:tag( 'td' ):wikitext( args[i].auteur or args[i].writer )
end
if args.colonne_extra or args.extra_column then
ligne:tag( 'td' ):wikitext( args[i].extra )
end
ligne:tag('td')
:addClass( 'pistes-duree' )
:wikitext( args[i].temps or args[i].length )
end
end
-- 6. Affichage de la durée totale le cas échéant
if args.total_temps or args.total_length then
tabPistes:tag( 'tr' ):tag('td')
:addClass( 'pistes-dureetotale' )
:attr( 'colspan', '10' )
:wikitext( args.total_temps or args.total_length )
end
return tostring( divPistes )
end
return p