Module:Bandeau

Révision datée du 8 novembre 2022 à 21:08 par Kharmitch (discussion | contributions) (Page créée avec « -- Module standardisant la création et le format des bandeaux -- Inspiré de https://fr.wikipedia.org/wiki/Module:Bandeau local p = {} local banner_styles = { ["simple"] = "sf-banner-simple", ["article"] = "sf-banner-article", ["section"] = "sf-banner-section", } local banner_levels = { ["neutre"] = "sf-banner-neutral", ["grave"] = "sf-banner-serious", ["modéré"] = "sf-banner-moderate", ["information"] = "sf-banner-info", ["ébauche"] = "sf-banner-s... »)
(diff) ← Version précédente | Voir la version actuelle (diff) | Version suivante → (diff)
Documentation[voir] [modifier] [purger]

Ce module implémente les modèles de bandeau.

Utilisation

Fonctions exportables :

  • banner(frame) – voir la documentation du modèle {{Bandeau}} ;
  • stub(frame) – voir la documentation du modèle {{ébauche}}.

Autres fonctions :

  • les fonctions ci-dessus avec un « _ » avant le nom peuvent être appelées directement depuis lua, avec les paramètres dans une table.

Modules externes et autres éléments dont ce module a besoin pour fonctionner :


-- Module standardisant la création et le format des bandeaux
-- Inspiré de https://fr.wikipedia.org/wiki/Module:Bandeau

local p = {}

local banner_styles = {
	["simple"] = "sf-banner-simple",
	["article"] = "sf-banner-article",
	["section"] = "sf-banner-section",
}

local banner_levels = {
	["neutre"] = "sf-banner-neutral",
	["grave"] = "sf-banner-serious",
	["modéré"] = "sf-banner-moderate",
	["information"] = "sf-banner-info",
	["ébauche"] = "sf-banner-stub",
}

local config = {
	image_format = "[[Fichier:%s|%spx|alt=%s|link=|class=noviewer]]",
	image_size = "x50",
	style_default = "sf-banner-simple",
	level_default = "sf-banner-neutral",
}

-- Fonction générant un bandeau
function p._banner(args)
	-- Contrôle des paramètres obligatoires
	
	local text = args.texte
	local title = args.titre
	
	if not text and not title then
		error( "Merci de renseigner au moins le titre ou le texte")
	end
	
	-- Création du conteneur et des cellules qui le compose
	
	local res = mw.html.create( "div" )
	local cells = mw.html.create()

	res	:addClass( "sf-banner" )
		:addClass( banner_styles[args.forme] or config.style_default )
		:addClass( banner_levels[args.niveau] or config.level_default )

	local image = args.image
	if image then
		local alt = args["image légende"] or ""
		local size = args['image taille'] or config.image_size
		image = 
		cells
			:tag( "div" )
				:addClass( "sf-banner-cell sf-banner-image" )
				:wikitext(config.image_format:format(image, size, alt))
	end
	
	local html_text = mw.html.create()
	
	if title then
		html_text = html_text
			:tag( "strong" )
				:addClass( "sf-banner-title" )
				:wikitext(title)
			:done()
	end
	
	if text then
		html_text
			:newline()
			:newline()
			:wikitext(text)
	end

	cells
		:tag('div')
			:addClass('sf-banner-cell')
			:newline()
			:wikitext(tostring(html_text))
			:newline()

	res	:node(cells)
	return tostring(res)
end

-- Fonction destinnée à être appelée dans les modèles
function p.banner(frame)
	local args
	if frame.args.texte or frame.args.titre then
		args = frame.args
	else
		args = frame:getParent().args
	end
	return p._banner(args)
end

return p