« Module:Infobox/Core » : différence entre les versions
De Nefald
Autres actions
Page créée avec « 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... » Balises : Modification par mobile Modification par le web mobile |
Aucun résumé des modifications Balises : Modification par mobile Modification par le web mobile |
||
Ligne 6 : | Ligne 6 : | ||
end | end | ||
-- Création de la table principale | -- Création de la table principale avec la classe CSS appropriée | ||
local html = mw.html.create('table') | local html = mw.html.create('table') | ||
:addClass(' | :addClass('infobox-table') | ||
-- Titre principal | -- Titre principal (nom) | ||
local nom = args.nom or mw.title.getCurrentTitle().text | local nom = args.nom or mw.title.getCurrentTitle().text | ||
if nom and nom ~= '' then | if nom and nom ~= '' then | ||
html:tag('tr') | html:tag('tr') | ||
:tag('th') | :tag('th') | ||
:attr('colspan', '2') | :attr('colspan', '2') | ||
:addClass( | :addClass('infobox-table-name') | ||
:wikitext(nom) | :wikitext(nom) | ||
end | end | ||
Ligne 50 : | Ligne 42 : | ||
html:tag('tr') | html:tag('tr') | ||
:attr('id', 'infobox-table-img-bg') | |||
:tag('td') | :tag('td') | ||
:attr('colspan', ' | :attr('colspan', '2') | ||
: | :addClass('infobox-table-img') | ||
:wikitext(string.format('[[Fichier:%s|%s|%s]]', args.image, imageSize, caption)) | :wikitext(string.format('[[Fichier:%s|%s|%s]]', args.image, imageSize, caption)) | ||
end | end | ||
Ligne 65 : | Ligne 58 : | ||
local field = p.findField(config.fields, fieldKey) | local field = p.findField(config.fields, fieldKey) | ||
if field and args[fieldKey] and args[fieldKey] ~= '' then | if field and args[fieldKey] and args[fieldKey] ~= '' then | ||
hasContent = true | |||
break | |||
end | end | ||
end | end | ||
Ligne 75 : | Ligne 66 : | ||
-- Ajouter le titre de section | -- Ajouter le titre de section | ||
html:tag('tr') | if section.title then | ||
html:tag('tr') | |||
:tag('th') | |||
:attr('colspan', '2') | |||
:wikitext(section.title) | |||
end | |||
-- Ajouter les champs de la section | -- Ajouter les champs de la section | ||
for _, fieldKey in ipairs(section.fields) do | for _, fieldKey in ipairs(section.fields) do | ||
local field = p.findField(config.fields, fieldKey) | local field = p.findField(config.fields, fieldKey) | ||
if field then | if field and args[fieldKey] and args[fieldKey] ~= '' then | ||
p.addField(html, args, field | p.addField(html, args, fieldKey, field) | ||
end | end | ||
end | end | ||
end | end | ||
function p.addField(html, args, field | function p.addField(html, args, fieldKey, field) | ||
local value = args[fieldKey] | local value = args[fieldKey] | ||
if not value or value == '' then return end | |||
-- | -- Traitement spécial pour les coordonnées | ||
if fieldKey == 'coordonnees' | if fieldKey == 'coordonnees' and args.x and args.y and args.z then | ||
value = string.format( | |||
'[https://map.nefald.fr/?worldname=Harlan2&mapname=surface&zoom=4&x=%s&y=%s&z=%s %s, %s, %s]', | |||
args.x, args.y, args.z, args.x, args.y, args.z | |||
) | |||
end | end | ||
-- | -- Traitement spécial pour les liens wiki | ||
if | if field.link then | ||
value = '[[' .. value .. ']]' | |||
end | end | ||
local row = html:tag('tr') | local row = html:tag('tr') | ||
row:tag(' | row:tag('td'):wikitext(field.label) | ||
row:tag('td'):wikitext(value) | |||
row:tag('td') | |||
end | end | ||
function p.addCustomFields(html, args) | function p.addCustomFields(html, args) | ||
-- Champs personnalisés (champ1/valeur1, champ2/valeur2, etc.) | -- Champs personnalisés (champ1/valeur1, champ2/valeur2, etc.) | ||
for i = 1, | for i = 1, 5 do | ||
local champKey = 'champ' .. i | local champKey = 'champ' .. i | ||
local valeurKey = 'valeur' .. i | local valeurKey = 'valeur' .. i | ||
if args[champKey] and args[valeurKey] and args[champKey] ~= '' and args[valeurKey] ~= '' then | if args[champKey] and args[valeurKey] and | ||
args[champKey] ~= '' and args[valeurKey] ~= '' then | |||
local row = html:tag('tr') | local row = html:tag('tr') | ||
row:tag(' | row:tag('td'):wikitext(args[champKey]) | ||
row:tag('td'):wikitext(args[valeurKey]) | |||
row:tag('td') | |||
end | end | ||
end | end | ||
end | end | ||
function p.findField(fields, key) | function p.findField(fields, key) | ||
if not fields then return nil end | |||
for _, field in ipairs(fields) do | for _, field in ipairs(fields) do | ||
if field.key == key then | if field.key == key then |
Version du 23 septembre 2025 à 16:45
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 avec la classe CSS appropriée
local html = mw.html.create('table')
:addClass('infobox-table')
-- Titre principal (nom)
local nom = args.nom or mw.title.getCurrentTitle().text
if nom and nom ~= '' then
html:tag('tr')
:tag('th')
:attr('colspan', '2')
:addClass('infobox-table-name')
: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')
:attr('id', 'infobox-table-img-bg')
:tag('td')
:attr('colspan', '2')
:addClass('infobox-table-img')
: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
hasContent = true
break
end
end
if not hasContent then return end
-- Ajouter le titre de section
if section.title then
html:tag('tr')
:tag('th')
:attr('colspan', '2')
:wikitext(section.title)
end
-- Ajouter les champs de la section
for _, fieldKey in ipairs(section.fields) do
local field = p.findField(config.fields, fieldKey)
if field and args[fieldKey] and args[fieldKey] ~= '' then
p.addField(html, args, fieldKey, field)
end
end
end
function p.addField(html, args, fieldKey, field)
local value = args[fieldKey]
if not value or value == '' then return end
-- Traitement spécial pour les coordonnées
if fieldKey == 'coordonnees' and args.x and args.y and args.z then
value = string.format(
'[https://map.nefald.fr/?worldname=Harlan2&mapname=surface&zoom=4&x=%s&y=%s&z=%s %s, %s, %s]',
args.x, args.y, args.z, args.x, args.y, args.z
)
end
-- Traitement spécial pour les liens wiki
if field.link then
value = '[[' .. value .. ']]'
end
local row = html:tag('tr')
row:tag('td'):wikitext(field.label)
row:tag('td'):wikitext(value)
end
function p.addCustomFields(html, args)
-- Champs personnalisés (champ1/valeur1, champ2/valeur2, etc.)
for i = 1, 5 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('td'):wikitext(args[champKey])
row:tag('td'):wikitext(args[valeurKey])
end
end
end
function p.findField(fields, key)
if not fields then return nil end
for _, field in ipairs(fields) do
if field.key == key then
return field
end
end
return nil
end
return p