« Module:Infobox/Core » : différence entre les versions
De Nefald
Autres actions
Supprimé l'ancienne section d'ajout d'icône avec :wikitext() Ajouté le traitement de l'icône après la construction du HTML Utilisé preprocess() pour traiter le parser function FontAwesome Injecté l'icône dans le HTML final avec gsub() Balises : Modification par mobile Modification par le web mobile |
Ajout catégorisation automatique (namespace 0) selon le type Balises : Modification par mobile Modification par le web mobile |
||
| Ligne 2 : | Ligne 2 : | ||
local getArgs = require('Module:Arguments').getArgs | local getArgs = require('Module:Arguments').getArgs | ||
function p.build(args, config, frame) | -- Fonction de validation du champ "type" | ||
local function validateTypeField(args, config) | |||
local typeValue = args.type or args['type'] | |||
-- Si pas de configuration pour le type, pas de validation | |||
if not config.typeField then | |||
return { valid = true } | |||
end | |||
-- Si le champ est requis et vide | |||
if config.typeField.required and (not typeValue or typeValue == '') then | |||
return { | |||
error = 'Le champ "type" est obligatoire.', | |||
valid = false | |||
} | |||
end | |||
-- Si pas de valeur, mais pas requis, c'est valide | |||
if not typeValue or typeValue == '' then | |||
return { valid = true } | |||
end | |||
-- Nettoyer la valeur | |||
local cleanType = mw.text.trim(mw.ustring.lower(typeValue)) | |||
-- Vérifier si la valeur est dans la liste autorisée | |||
if config.typeField.allowedValues and not config.typeField.allowedValues[cleanType] then | |||
local errorMsg = config.typeField.errorMessage or 'Valeur de type non autorisée : ' .. typeValue | |||
return { | |||
error = errorMsg, | |||
valid = false | |||
} | |||
end | |||
return { | |||
valid = true, | |||
cleanValue = cleanType, | |||
displayValue = config.typeField.allowedValues and config.typeField.allowedValues[cleanType] and config.typeField.allowedValues[cleanType].display or typeValue | |||
} | |||
end | |||
-- Fonction pour gérer la catégorisation automatique | |||
local function addTypeCategories(args, config) | |||
-- Vérifier qu'on est dans le namespace principal (0) | |||
local title = mw.title.getCurrentTitle() | |||
if title.namespace ~= 0 then | |||
return '' | |||
end | |||
-- Pas de configuration de type, pas de catégorie | |||
if not config.typeField or not config.typeField.allowedValues then | |||
return '' | |||
end | |||
local typeValue = args.type or args['type'] | |||
if not typeValue or typeValue == '' then | |||
return '' | |||
end | |||
-- Nettoyer la valeur | |||
local cleanType = mw.text.trim(mw.ustring.lower(typeValue)) | |||
-- Récupérer la catégorie correspondante | |||
local typeConfig = config.typeField.allowedValues[cleanType] | |||
if typeConfig and typeConfig.category then | |||
return '[[Catégorie:' .. typeConfig.category .. ']]' | |||
end | |||
return '' | |||
end | |||
function p.build(args, config, frame) | |||
if not args.nom or args.nom == '' then | if not args.nom or args.nom == '' then | ||
return '<div class="error">Nom requis pour l\'infobox</div>' | return '<div class="error">Nom requis pour l\'infobox</div>' | ||
end | |||
-- Validation du champ type | |||
local typeValidation = validateTypeField(args, config) | |||
if not typeValidation.valid then | |||
return '<div class="error">' .. typeValidation.error .. '</div>' | |||
end | end | ||
| Ligne 33 : | Ligne 110 : | ||
local titleDiv = mw.html.create('div') | local titleDiv = mw.html.create('div') | ||
:addClass('infobox-title') | :addClass('infobox-title') | ||
:wikitext(args.nom) | :wikitext(args.nom) | ||
header:node(titleDiv) | header:node(titleDiv) | ||
| Ligne 89 : | Ligne 166 : | ||
coordinatesDisplayed = true | coordinatesDisplayed = true | ||
-- Traitement normal des autres champs | -- Traitement normal des autres champs avec formatage spécial pour le type | ||
elseif not (hasCoordinates and (champ.cle == 'x' or champ.cle == 'y' or champ.cle == 'z')) and args[champ.cle] and args[champ.cle] ~= '' then | elseif not (hasCoordinates and (champ.cle == 'x' or champ.cle == 'y' or champ.cle == 'z')) and args[champ.cle] and args[champ.cle] ~= '' then | ||
local rowDiv = mw.html.create('div') | local rowDiv = mw.html.create('div') | ||
| Ligne 100 : | Ligne 177 : | ||
local valueDiv = mw.html.create('div') | local valueDiv = mw.html.create('div') | ||
:addClass('infobox-value') | :addClass('infobox-value') | ||
:wikitext(args[champ.cle]) | |||
-- Formatage spécial pour le champ "type" | |||
if champ.cle == 'type' and typeValidation.displayValue then | |||
valueDiv:wikitext(typeValidation.displayValue) | |||
else | |||
valueDiv:wikitext(args[champ.cle]) | |||
end | |||
rowDiv:node(labelDiv):node(valueDiv) | rowDiv:node(labelDiv):node(valueDiv) | ||
| Ligne 135 : | Ligne 218 : | ||
end | end | ||
return finalResult | -- Ajouter les catégories automatiques | ||
local categories = addTypeCategories(args, config) | |||
return finalResult .. categories | |||
end | end | ||
return p | return p | ||