Module:Infobox/Core
De Nefald
Autres actions
La documentation pour ce module peut être créée à Module:Infobox/Core/doc
local p = {}
function p.build(args, config)
if not config then
return '<div class="error">Configuration manquante pour ce type d\'infobox</div>'
end
-- Création de la table principale
local html = mw.html.create('table')
:addClass('infobox_v2')
:attr('cellspacing', '7')
-- Titre principal avec classe spécifique
local nom = args.nom or mw.title.getCurrentTitle().text
if nom and nom ~= '' then
local headerClass = 'entete ' .. (config.headerClass or 'monde')
local headerStyle = string.format(
'background-color:%s; color:#fff; font-size:1.4em; text-shadow:0.1em 0.1em 0.1em #000',
config.headerColor or '#86C705'
)
html:tag('tr')
:tag('th')
:attr('colspan', '2')
:addClass(headerClass)
:cssText(headerStyle)
:wikitext(nom)
end
-- Image principale
p.addImage(html, args)
-- Sections
if config.sections then
for _, section in ipairs(config.sections) do
p.addSection(html, args, config, section)
end
end
-- Champs personnalisés
p.addCustomFields(html, args)
return tostring(html)
end
function p.addImage(html, args)
if args.image and args.image ~= '' then
local imageSize = args.tailleimage or '280x200px'
local caption = args.legende or args.legendecarte or mw.title.getCurrentTitle().text
html:tag('tr')
:tag('td')
:attr('colspan', '5')
:cssText('font-size: smaller; text-align: center;')
:wikitext(string.format('[[Fichier:%s|%s|%s]]', args.image, imageSize, caption))
end
end
function p.addSection(html, args, config, section)
if not section.fields then return end
-- Vérifier si la section a du contenu
local hasContent = false
for _, fieldKey in ipairs(section.fields) do
local field = p.findField(config.fields, fieldKey)
if field and args[fieldKey] and args[fieldKey] ~= '' then
if not field.optional or (field.optional and args[fieldKey] ~= '') then
hasContent = true
break
end
end
end
if not hasContent then return end
-- Ajouter le titre de section
html:tag('tr')
:tag('th')
:attr('colspan', '2')
:addClass('infoboxcat')
:cssText('text-align:center; background-color:transparent; padding:3px; margin-top: 20px; border-bottom:1px solid #ccc; color:#099DFF;')
:wikitext(section.title)
-- Ajouter les champs de la section
for _, fieldKey in ipairs(section.fields) do
local field = p.findField(config.fields, fieldKey)
if field then
p.addField(html, args, field, fieldKey)
end
end
end
function p.addField(html, args, field, fieldKey)
local value = args[fieldKey]
-- Champs spéciaux avec conditions
if fieldKey == 'coordonnees' or fieldKey == 'spawn' then
if args.x and args.y and args.z then
local worldName = args.monde or 'Harlan2'
local linkText = string.format('%s, %s, %s', args.x, args.y, args.z)
local mapUrl = string.format(
'https://map.nefald.fr/?worldname=%s&mapname=surface&zoom=4&x=%s&y=%s&z=%s',
worldName, args.x, args.y, args.z
)
value = string.format("''[%s %s]''", mapUrl, linkText)
else
return -- Pas de coordonnées complètes
end
end
-- Vérifier si le champ doit être affiché
if not value or value == '' then
if field.optional then
return -- Champ optionnel vide, on l'ignore
end
end
-- Formatage spécial pour certains champs
if field.format then
if field.format == 'seed' then
value = string.format('<span style="color:#099DFF;">\'\'%s\'\'</span>', value)
elseif field.format == 'link' and value then
value = string.format('[[%s]]', value)
end
end
-- Ajouter la ligne du champ
local row = html:tag('tr')
row:tag('th')
:attr('scope', 'row')
:wikitext(field.label)
row:tag('td')
:wikitext(value or '')
end
function p.addCustomFields(html, args)
-- Champs personnalisés (champ1/valeur1, champ2/valeur2, etc.)
for i = 1, 10 do
local champKey = 'champ' .. i
local valeurKey = 'valeur' .. i
if args[champKey] and args[valeurKey] and args[champKey] ~= '' and args[valeurKey] ~= '' then
local row = html:tag('tr')
row:tag('th')
:attr('scope', 'row')
:wikitext(args[champKey])
row:tag('td')
:wikitext(args[valeurKey])
end
end
end
-- Fonction pour ajouter des images spéciales (comme carte)
function p.addSpecialImage(html, args, imageKey, sizeKey, captionKey)
if args[imageKey] and args[imageKey] ~= '' then
local imageSize = args[sizeKey] or '280x200px'
local caption = args[captionKey] or mw.title.getCurrentTitle().text
html:tag('tr')
:tag('td')
:attr('colspan', '5')
:cssText('font-size: smaller; text-align: center;')
:wikitext(string.format('[[Fichier:%s|%s|%s]]', args[imageKey], imageSize, caption))
end
end
function p.findField(fields, key)
for _, field in ipairs(fields) do
if field.key == key then
return field
end
end
return nil
end
return p