« Module:Bandeau » : différence entre les versions

De Starfield Wiki
Annulation des modifications 729 de Kharmitch (discussion)
Balise : Annulation
Aucun résumé des modifications
Ligne 1 : Ligne 1 :
-- Module standardisant la création et le format des bandeaux
-- Inspiré de https://fr.wikipedia.org/wiki/Module:Bandeau
local p = {}
local p = {}


local banner_styles = {
-- Types de bandeau autorisés
["simple"] = "sf-banner-simple",
local shapes = {
["article"] = "sf-banner-article",
article = "banner-article",
["section"] = "sf-banner-section",
note = "banner-note",
section = "banner-section",
simple = "bandeau-simple",
}
}


local banner_levels = {
-- Niveaux d'information autorisés
["neutre"] = "sf-banner-neutral",
local levels = {
["grave"] = "sf-banner-serious",
ebauche = "banner-stub",
["modéré"] = "sf-banner-moderate",
grave = "banner-serious",
["information"] = "sf-banner-info",
information = "banner-info",
["ébauche"] = "sf-banner-stub",
modere = "banner-moderate",
neutre = "banner-neutral",
}
}
levels["modéré"] = levels.modere
levels["ébauche"] = levels.ebauche


local config = {
-- Paramétrage par défaut
local default = {
shape = "simple",
level = "information",
image_format = "[[Fichier:%s|%spx|alt=%s|link=|class=noviewer]]",
image_format = "[[Fichier:%s|%spx|alt=%s|link=|class=noviewer]]",
image_size = "x50",
image_size = "x50",
style_default = "sf-banner-simple",
stub_title = "Cet article est une [[Aide:Ébauche|ébauche]].",
level_default = "sf-banner-neutral",
stub_title_subject = "Cet article est une [[Aide:Ébauche|ébauche]] concernant %s.",
stub_category = "[[Catégorie:Ébauche]]",
stub_category_subject = "[[Catégorie:Ébauche (%s)]]",
stub_text = "Vous pouvez partager vos connaissances en l'améliorant ('''[[Aide:Modifier une page|comment ?]]''') selon nos [[Aide:Accueil#Politique de Starfield Wiki|conventions]].",
}
}


-- Fonction générant un bandeau
-- Fonction retournant un bandeau au format souhaité
function p._banner(args)
function p._banner(args)
-- Contrôle des paramètres obligatoires
local text = args.texte
local text = args.texte
local title = args.titre
local title = args.titre
if not text and not title then
if not text and not title then
error( "Merci de renseigner au moins le titre ou le texte")
error("Paramètres texte ou titre absents.")
end
end
-- Création du conteneur et des cellules qui le compose
local shape = args.forme or default.shape
local shape_class = shapes[shape]
if shape_class == nil then
error("Valeur inconnue pour le paramètre forme.")
end
local res = mw.html.create( "div" )
local level = args.niveau or default.level
local level_class = levels[level]
if level_class == nil then
error("Valeur inconnue pour le paramètre niveau.")
end
 
local res = mw.html.create("div")
local cells = mw.html.create()
local cells = mw.html.create()


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


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


cells
cells
:tag('div')
:tag("div")
:addClass('sf-banner-cell')
:addClass("banner-cell")
:newline()
:newline()
:wikitext(tostring(html_text))
:wikitext(tostring(html_text))
Ligne 84 : Ligne 98 :
end
end


-- Fonction destinnée à être appelée dans les modèles
-- Fonction spécifique au bandeau de type ébauche
function p.banner(frame)
function p._stub(args)
local args
local subjects = mw.loadData("Module:Bandeau/Ébauche")
if frame.args.texte or frame.args.titre then
local subject = args[1]
args = frame.args
if subject ~= nil then
subject = subjects[subject:lower()]
end
local title
local category
if subject ~= nil then
title = string.format(
default.stub_title_subject,
subject.sujet
)
category = string.format(
default.stub_category_subject,
args[1]:lower()
)
else
else
args = frame:getParent().args
title = default.stub_title
category = default.stub_category
end
end
return p._banner(args)
return p._banner({
forme = "article",
niveau = "ébauche",
titre = title,
texte = default.stub_text,
}) .. category
end
 
function get_args(frame)
local args = {}
local argsParent = frame:getParent().args
for cle, val in pairs(argsParent) do
if val then
args[cle] = mw.text.trim(val)
end
end
return args
end
 
-- Fonctions destinées à être appelées dans les modèles
 
function p.banner(frame)
return _banner(get_args(frame))
end
 
function p.stub(frame)
return _stub(get_args(frame))
end
end


return p
return p

Version du 26 janvier 2023 à 22:31

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 :


local p = {}

-- Types de bandeau autorisés
local shapes = {
	article = "banner-article",
	note = "banner-note",
	section = "banner-section",
	simple = "bandeau-simple",
}

-- Niveaux d'information autorisés
local levels = {
	ebauche = "banner-stub",
	grave = "banner-serious",
	information = "banner-info",
	modere = "banner-moderate",
	neutre = "banner-neutral",
}
levels["modéré"] = levels.modere
levels["ébauche"] = levels.ebauche

-- Paramétrage par défaut
local default = {
	shape = "simple",
	level = "information",
	image_format = "[[Fichier:%s|%spx|alt=%s|link=|class=noviewer]]",
	image_size = "x50",
	stub_title = "Cet article est une [[Aide:Ébauche|ébauche]].",
	stub_title_subject = "Cet article est une [[Aide:Ébauche|ébauche]] concernant %s.",
	stub_category = "[[Catégorie:Ébauche]]",
	stub_category_subject = "[[Catégorie:Ébauche (%s)]]",
	stub_text = "Vous pouvez partager vos connaissances en l'améliorant ('''[[Aide:Modifier une page|comment ?]]''') selon nos [[Aide:Accueil#Politique de Starfield Wiki|conventions]].",
}

-- Fonction retournant un bandeau au format souhaité
function p._banner(args)
	local text = args.texte
	local title = args.titre
	if not text and not title then
		error("Paramètres texte ou titre absents.")
	end
	
	local shape = args.forme or default.shape
	local shape_class = shapes[shape]
	if shape_class == nil then
		error("Valeur inconnue pour le paramètre forme.")
	end
	
	local level = args.niveau or default.level
	local level_class = levels[level]
	if level_class == nil then
		error("Valeur inconnue pour le paramètre niveau.")
	end

	local res = mw.html.create("div")
	local cells = mw.html.create()

	res	:addClass("banner")
		:addClass(shape_class)
		:addClass(level_class)

	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("banner-cell banner-image")
			:wikitext(default.image_format:format(image, size, alt))
	end

	local html_text = mw.html.create()

	if title then
		html_text = html_text
			:tag("strong")
				:addClass("banner-title")
				:wikitext(title)
			:done()
	end

	if text then
		html_text
			:newline()
			:newline()
			:wikitext(text)
	end

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

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

-- Fonction spécifique au bandeau de type ébauche
function p._stub(args)
	local subjects = mw.loadData("Module:Bandeau/Ébauche")
	local subject = args[1]
	
	if subject ~= nil then
		subject = subjects[subject:lower()]
	end
	
	local title
	local category
	
	if subject ~= nil then
		title = string.format(
			default.stub_title_subject,
			subject.sujet
		)
		category = string.format(
			default.stub_category_subject,
			args[1]:lower()
		)
	else
		title = default.stub_title
		category = default.stub_category
	end
	
	return p._banner({
		forme = "article",
		niveau = "ébauche",
		titre = title,
		texte = default.stub_text,
	}) .. category
end

function get_args(frame)
	local args = {}
	local argsParent = frame:getParent().args
	for cle, val in pairs(argsParent) do
		if val then
			args[cle] = mw.text.trim(val)
		end
	end
	return args
end

-- Fonctions destinées à être appelées dans les modèles

function p.banner(frame)
	return _banner(get_args(frame))
end

function p.stub(frame)
	return _stub(get_args(frame))
end

return p