Module:Bandeau

De Wikimedica

La documentation pour ce module peut être créée à Module:Bandeau/Documentation

--Ce module implémente les modèles de 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).
--Gérer les images multiples.

local p = {}

local trim = require('Module:Outils').trim
local yesno = require('Module:yesno')
local class = mw.loadData('Module:Bandeau/Class')

local cfg = {
	forme = 'bandeau-simple',
	niveau = 'bandeau-niveau-neutre',
	formatLien = '[[Fichier:%s|%spx|alt=%s|class=noviewer]]',
	tailleIcone = '45x45',
	public = 'domaine public',
	erreurArgument = 'Paramètre <code>|%s=</code> manquant',
	erreurEbaucheParam = 'le thème « %s » du modèle [[Modèle:Ébauche|{{ébauche}}]] n’est pas [[Aide:Ébauche/Aide paramètres|défini]].',
	erreurEbaucheType = 'le thème « %s » doit apparaître en tête de liste du modèle [[Modèle:Ébauche|{{ébauche}}]].',
	ebaucheImage = '<span style="white-space:nowrap;word-spacing:5px">%s</span>',
	ebaucheTitre = '%s est une [[Aide:Ébauche|ébauche]].',
	ebaucheTitreSujet = '%s est une [[Aide:Ébauche|ébauche]] concernant %s.',
	ebaucheCateg = '[[Catégorie:Wikipédia:ébauche %s]]',
}

local function erreur(texte, formatstring, tag)
	local res = mw.html.create(tag or 'span')
	res	:addClass('error')
		:wikitext('Erreur : ')

	if formatstring then
		res:wikitext(formatstring:format(texte))
	else
		res:wikitext(texte)
	end

	return tostring(res)
end

function p._bandeau(args)
	
	local res = mw.html.create('div')
	local cells = mw.html.create()
	local icone = trim(args.image) or trim(args['icône'])
	local backgroundIcone = class.icones[icone] or ''
	local texte = (trim(args.texte) or erreur('texte', cfg.erreurArgument))

	res	:attr{ id = args.id }
		:addClass(class.formes[args.forme] or cfg.forme)
		:addClass(class.niveau[args.niveau] or cfg.niveau)
		:addClass('plainlinks')
		:addClass(args.class)
		:cssText(args.style)
	
	if yesno(args.centrer) then
		cells = mw.html.create('div')
			:addClass('bandeau-centrer')
	elseif trim(args.droite) then
		cells = mw.html.create('div')
			:css{ display = 'table', width = '100%' }
	end
	if icone and backgroundIcone == '' then
		local iconeWiki = icone
		if not icone:match('%[') then
			local alt = args.alt or args['légende'] or ''
			if alt:lower() == cfg.public then
				alt = '|link='
			end
			if yesno( args['domaine public'] ) then
				alt = alt .. '|link='
			end
			local taille = args['taille icône'] or cfg.tailleIcone
			iconeWiki = cfg.formatLien:format(icone, taille, alt)
		end
		cells
			:tag('div')
				:addClass('bandeau-cell bandeau-icone')
				:wikitext(iconeWiki)
			:done()
	elseif backgroundIcone ~= '' and args.forme == 'section' then
		texte = '&ensp;' .. texte
	end
	cells
		:tag('div')
			:addClass('bandeau-cell ' .. backgroundIcone)
			:wikitext(texte)
			:newline()
		:done()
	if trim(args.droite) then
		cells
			:tag('div')
				:addClass('bandeau-cell')
				:css( 'padding-left', '1em' )
				:newline()
				:wikitext(args.droite)
			:done()
	end
		
	res	:node(cells)
	if trim(args['supplément']) then
		res	:tag('p')   
				:wikitext(args['supplément'])
			:done()
	end
	
	return mw.getCurrentFrame():expandTemplate{
		title = 'Boîte',
		args = {contenu = tostring(res)}
	}
end

function p._bandeauAvertissement(args)
	local htmlTexte = mw.html.create()

	htmlTexte
		:tag('strong')
			:addClass('bandeau-titre')
			:wikitext(trim(args.titre) or erreur('titre', cfg.erreurArgument))
		:done()
		:wikitext(args.date)
		:newline():newline()
		:wikitext(args.texte)

	local parametres = {
		forme = 'article',
		niveau = args.niveau,
		id = args.id,
		class = ( args.class or '' ) .. ' metadata',
		['icône'] = trim(args['icône']) or trim(args['icône-complexe']) or args.niveau,
		alt = args.alt or args['légende'],
		['domaine public'] = args['domaine public'],
		texte = tostring(htmlTexte),
		['supplément'] = args['supplément'],
	}
	
	return p._bandeau(parametres)
end

function p._bandeauSection(args)
	local res = mw.html.create('div')
	local icone = trim(args.image) or trim(args['icône'])
	local backgroundIcone = class.icones[icone] or ''
	local texte = '&ensp;' .. (trim(args.texte) or erreur('texte', cfg.erreurArgument))

	res	:addClass('bandeau-section metadata')
		:addClass(class.niveau[args.niveau] or cfg.niveau)
	
	if icone and backgroundIcone == '' then
		local iconeWiki = icone
		if not icone:match('%[') then
			local alt = args.alt or args['légende'] or ''
			if alt:lower() == cfg.public then
				alt = '|link='
			end
			iconeWiki = cfg.formatLien:format(icone, 'text-top|20x17', alt)
		end
		res	:wikitext(iconeWiki)
			:done()
	end
	res	:addClass(backgroundIcone)
		:wikitext(texte)
		:done()
	
	return mw.getCurrentFrame():expandTemplate{
		title = 'Boîte',
		args = {contenu = tostring(res)}
	}
end

-- Insertion dans la table p des fonctions appelées par les
-- modèles à l'aide d'un adaptateur de fonction.
local function adaptateur(nomFonction)
	return function (frame)
		local args
		if frame.args.texte or frame.args.titre then
			args = frame.args
		else
			args = frame:getParent().args
		end
		return p[nomFonction](args)
	end
end
 
local nomsFonction = {'bandeau', 'bandeauAvertissement', 'bandeauSection' } 
for _, nomFonction in ipairs(nomsFonction) do
	p[nomFonction] = adaptateur('_' .. nomFonction)
end

return p