« Module:Bandeau » : différence entre les versions
Aller à la navigation
Aller à la recherche
Modèle>Hlm Z. Aucun résumé des modifications |
Modèle>Hlm Z. Aucun résumé des modifications |
||
| Ligne 12 : | Ligne 12 : | ||
local format = mw.ustring.format | local format = mw.ustring.format | ||
local cfg = {} | |||
cfg.classeSimple = 'simple' | |||
cfg.niveauNeutre = 'neutre' | |||
cfg.formatLien = '[[Fichier:%s|40px|alt=%s|link=]]' | |||
cfg.erreurArgument = 'Paramètre <code>|%s=</code> manquant' | |||
local icones = { | |||
grave = 'Fairytale no.svg', | |||
['modéré'] = 'Emblem-important.svg', | |||
information = 'Information icon.svg', | |||
['ébauche'] = 'Nuvola apps kedit.svg' | |||
} | |||
function p._bandeau(argsBandeau) | function p._bandeau(argsBandeau) | ||
| Ligne 23 : | Ligne 32 : | ||
res | res | ||
:addClass('test-bandeau-' .. (args.classe or | :addClass('test-bandeau-' .. (args.classe or cfg.classeSimple)) | ||
:addClass('test-bandeau-niveau-' .. (args.niveau or | :addClass('test-bandeau-niveau-' .. (args.niveau or cfg.niveauNeutre)) | ||
:addClass('plainlinks') | :addClass('plainlinks') | ||
:cssText(args.style) --Provisoire | :cssText(args.style) --Provisoire | ||
| Ligne 35 : | Ligne 44 : | ||
imageFormat:wikitext(args.image) | imageFormat:wikitext(args.image) | ||
else | else | ||
imageFormat:wikitext(format( | imageFormat:wikitext(mw.ustring.format(cfg.formatLien, | ||
args.image, | args.image, | ||
args['légende'] or '')) | args['légende'] or '')) | ||
| Ligne 50 : | Ligne 59 : | ||
function p._bandeauAvertissement(args) | function p._bandeauAvertissement(args) | ||
local htmlTexte = mw.html.create() | local htmlTexte = mw.html.create() | ||
htmlTexte | htmlTexte | ||
:tag('strong') | :tag('strong') | ||
:wikitext(args.titre or erreur(' | :wikitext(args.titre or erreur(true, 'titre')) | ||
:done() | :done() | ||
:tag('br') | :tag('br') | ||
| Ligne 75 : | Ligne 78 : | ||
end | end | ||
function erreur(texte) | function erreur(gestionArg, texte) | ||
local res = mw.html.create('span') | |||
:addClass('error') | |||
if gestionArg then | |||
res:wikitext(format(cfg.erreurArgument, texte)) | |||
else | |||
res:wikitext(texte) | |||
end | |||
return tostring(res) | |||
end | end | ||
Version du 26 juillet 2014 à 13:56
La documentation pour ce module peut être créée à Module:Bandeau/doc
--Ce module implémente le modèle {{Bandeau}}.
--Standardisation des bandeaux ([[Catégorie:Modèle de bandeau]]).
--Créer une fonction exportable pour le modèle {{Bandeau}} (ns:all).
--Créer une fonction exportable pour les bandeaux d'article (ns:0).
--Créer une fonction exportable pour les bandeaux de section (ns:0).
--Créer une fonction exportable pour les bandeaux d'ébauche (ns:0).
--Créer une fonction exportable pour les bandeaux de discussion (ns:1).
--Créer une fonction exportable pour les bandeaux système (ns:8).
local p = {}
local format = mw.ustring.format
local cfg = {}
cfg.classeSimple = 'simple'
cfg.niveauNeutre = 'neutre'
cfg.formatLien = '[[Fichier:%s|40px|alt=%s|link=]]'
cfg.erreurArgument = 'Paramètre <code>|%s=</code> manquant'
local icones = {
grave = 'Fairytale no.svg',
['modéré'] = 'Emblem-important.svg',
information = 'Information icon.svg',
['ébauche'] = 'Nuvola apps kedit.svg'
}
function p._bandeau(argsBandeau)
args = argsBandeau
local res = mw.html.create('div')
res
:addClass('test-bandeau-' .. (args.classe or cfg.classeSimple))
:addClass('test-bandeau-niveau-' .. (args.niveau or cfg.niveauNeutre))
:addClass('plainlinks')
:cssText(args.style) --Provisoire
if args.image then
local imageFormat = res:tag('div')
:addClass('test-bandeau-image')
if mw.ustring.sub(args.image, 1, 1) == '[' then
imageFormat:wikitext(args.image)
else
imageFormat:wikitext(mw.ustring.format(cfg.formatLien,
args.image,
args['légende'] or ''))
end
end
res
:tag('p')
:addClass('test-bandeau-texte')
:wikitext(args.texte or erreur(p.erreurArgumentTitre))
return tostring(res)
end
function p._bandeauAvertissement(args)
local htmlTexte = mw.html.create()
htmlTexte
:tag('strong')
:wikitext(args.titre or erreur(true, 'titre'))
:done()
:tag('br')
:done()
:wikitext(args.texte)
local parametres = {
classe = 'article',
niveau = args.niveau,
image = args['icône'] or icones[niveau],
texte = tostring(htmlTexte)
}
return p._bandeau(parametres)
end
function erreur(gestionArg, texte)
local res = mw.html.create('span')
:addClass('error')
if gestionArg then
res:wikitext(format(cfg.erreurArgument, texte))
else
res:wikitext(texte)
end
return tostring(res)
end
p['_ébauche'] = function (args)
--Construction HTML du bandeau d'ebauche.
local res = mw.html.create('div')
res:addClass('test-bandeau-article test-bandeau-niveau-information plainlinks')
if args.image then
res
:tag('div')
:addClass('test-bandeau-image')
:wikitext(args.image)
end
res
:tag('p')
:addClass('test-bandeau-texte')
:wikitext(args.texte or erreur(p.erreurArgumentTitre))
return tostring(res)
end
--Insertion dans la table p des fonctions appelées par les
--modèles à l'aide d'un adaptateur de fonction.
function adaptateur(nomFonction)
return function (frame)
local args = {}
local argsParent = frame:getParent().args
--Paramètres vides interprétés par Lua
for cle, val in pairs(argsParent) do
if val ~= '' then
args[cle] = mw.text.trim(val)
end
end
return p[nomFonction](args)
end
end
local nomsFonction = {'bandeau', 'bandeauAvertissement', 'ébauche'}
for _, nomFonction in ipairs(nomsFonction) do
p[nomFonction] = adaptateur('_' .. nomFonction)
end
return p