« 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 |
m map |
||
| (6 versions intermédiaires par le même utilisateur non affichées) | |||
| Ligne 2 : | Ligne 2 : | ||
local getArgs = require('Module:Arguments').getArgs | local getArgs = require('Module:Arguments').getArgs | ||
function | -- Fonction pour générer le lien de carte dynamique | ||
local function buildMapLink(args) | |||
-- Vérifier les champs obligatoires | |||
if not args.x or args.x == '' then return nil end | |||
if not args.y or args.y == '' then return nil end | |||
if not args.z or args.z == '' then return nil end | |||
if not args.worldname or args.worldname == '' then return nil end | |||
-- Valeurs par défaut pour les champs optionnels | |||
local mapname = args.mapname or 'flat' | |||
local zoom = args.zoom or '4' | |||
-- Construction de l'URL | |||
local url = string.format( | |||
'https://map.nefald.fr/?worldname=%s&mapname=%s&zoom=%s&x=%s&y=%s&z=%s', | |||
mw.uri.encode(args.worldname, 'PATH'), | |||
mw.uri.encode(mapname, 'PATH'), | |||
zoom, | |||
args.x, | |||
args.y, | |||
args.z | |||
) | |||
-- Retourner le lien formaté | |||
return string.format('[%s %s, %s, %s]', url, args.x, args.y, args.z) | |||
end | |||
-- 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 addCategories(args, config, frame) | |||
-- Vérifier qu'on est dans le namespace principal (0) | |||
local title = mw.title.getCurrentTitle() | |||
if title.namespace ~= 0 then | |||
return '' | |||
end | |||
local categoryString = '' | |||
-- Nouvelles catégories configurables (fonction categories) | |||
if type(config.categories) == 'function' then | |||
local cats = config.categories(args, config, frame) | |||
if cats then | |||
for _, cat in ipairs(cats) do | |||
categoryString = categoryString .. '[[Catégorie:' .. cat .. ']]' | |||
end | |||
end | |||
end | |||
return categoryString | |||
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 130 : | ||
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 49 : | Ligne 146 : | ||
local body = mw.html.create('div') | local body = mw.html.create('div') | ||
:addClass('infobox-body') | :addClass('infobox-body') | ||
-- Vérifier si on a des coordonnées complètes | |||
local mapLink = buildMapLink(args) | |||
local coordinatesDisplayed = false | |||
for _, section in ipairs(config.sections or {}) do | for _, section in ipairs(config.sections or {}) do | ||
| Ligne 57 : | Ligne 158 : | ||
body:node(sectionDiv) | body:node(sectionDiv) | ||
end | end | ||
for _, champ in ipairs(section.champs or {}) do | for _, champ in ipairs(section.champs or {}) do | ||
-- | -- Gestion spéciale pour les coordonnées | ||
if | if mapLink and not coordinatesDisplayed and (champ.cle == 'x' or champ.cle == 'y' or champ.cle == 'z' or champ.cle == 'worldname' or champ.cle == 'coordonnees') then | ||
local rowDiv = mw.html.create('div') | local rowDiv = mw.html.create('div') | ||
:addClass('infobox-row') | :addClass('infobox-row') | ||
| Ligne 74 : | Ligne 171 : | ||
local valueDiv = mw.html.create('div') | local valueDiv = mw.html.create('div') | ||
:addClass('infobox-value') | :addClass('infobox-value') | ||
:wikitext(mapLink) | |||
rowDiv:node(labelDiv):node(valueDiv) | rowDiv:node(labelDiv):node(valueDiv) | ||
| Ligne 89 : | Ligne 177 : | ||
coordinatesDisplayed = true | coordinatesDisplayed = true | ||
-- Traitement normal des autres champs ( | -- Traitement normal des autres champs (ignorer x, y, z, worldname s'ils sont déjà affichés comme coordonnées) | ||
elseif not ( | elseif not (mapLink and (champ.cle == 'x' or champ.cle == 'y' or champ.cle == 'z' or champ.cle == 'worldname' or champ.cle == 'mapname' or champ.cle == 'zoom')) then | ||
local rawValue = args[champ.cle] | |||
-- Si le champ a une valeur OU une fonction process qui peut générer une valeur | |||
if (rawValue and rawValue ~= '') or champ.process then | |||
local finalValue = rawValue | |||
-- Appliquer la fonction process si elle existe | |||
if champ.process and type(champ.process) == 'function' then | |||
finalValue = champ.process(rawValue, args, frame) | |||
end | |||
-- N'afficher que si on a une valeur finale | |||
if finalValue and finalValue ~= '' then | |||
local rowDiv = mw.html.create('div') | |||
:addClass('infobox-row') | |||
local labelDiv = mw.html.create('div') | |||
:addClass('infobox-label') | |||
:wikitext(champ.label) | |||
local valueDiv = mw.html.create('div') | |||
:addClass('infobox-value') | |||
-- Formatage spécial pour le champ "type" | |||
if champ.cle == 'type' and typeValidation.displayValue then | |||
valueDiv:wikitext(typeValidation.displayValue) | |||
else | |||
valueDiv:wikitext(finalValue) | |||
end | |||
rowDiv:node(labelDiv):node(valueDiv) | |||
body:node(rowDiv) | |||
end | |||
end | |||
end | end | ||
end | end | ||
| Ligne 135 : | Ligne 244 : | ||
end | end | ||
return finalResult | -- Ajouter les catégories automatiques | ||
local categories = addCategories(args, config, frame) | |||
return finalResult .. categories | |||
end | end | ||
return p | return p | ||