« Module:Infobox » : différence entre les versions
Aucun résumé des modifications |
Annulation des modifications 6068 de J. Hodgson (discussion) Balise : Annulation |
||
(2 versions intermédiaires par 2 utilisateurs non affichées) | |||
Ligne 1 : | Ligne 1 : | ||
-- Module https://fallout-wiki.com/Module:Infobox utilisé comme source et | |||
-- lui-même inspiré de https://fr.wikipedia.org/wiki/Module:Infobox | |||
-- Gère la construction des infobox dans le paramétrage doit être défini | |||
-- un sous-module. | |||
local p = {} | local p = {} | ||
-- données concernant les paramètres passés au modèle | -- données concernant les paramètres passés au modèle | ||
local localdata = {} | local localdata = {} | ||
-- données concernant la page où est affichée l | -- données concernant la page où est affichée l"infobox | ||
local page = { | local page = { | ||
name = mw.title.getCurrentTitle().prefixedText, | name = mw.title.getCurrentTitle().prefixedText, | ||
Ligne 14 : | Ligne 15 : | ||
} | } | ||
local | -- l"objet principal à retourner | ||
local infobox = mw.html.create("div") | |||
-- | --[[ Outils ]] | ||
local function get_value(value, part) | |||
if type(value) == "string" then | |||
local function | |||
if type(value) == | |||
return localdata[value] | return localdata[value] | ||
elseif type(value) == | elseif type(value) == "function" then | ||
return value(localdata, part) | return value(localdata, part) | ||
elseif type(value) == | elseif type(value) == "table" then | ||
-- si plusieurs paramètres possibles (legacy de vieux code), prendre le premier non vide | -- si plusieurs paramètres possibles (legacy de vieux code), | ||
-- prendre le premier non vide | |||
for i, j in pairs(value) do | for i, j in pairs(value) do | ||
if localdata[j] then | if localdata[j] then | ||
Ligne 39 : | Ligne 37 : | ||
function p.separator() | function p.separator() | ||
return mw.html.create( | return mw.html.create("hr"):addClass("separator") | ||
end | end | ||
--[[ | --[[ Construction des blocs ]] | ||
]] | |||
function p. | function p.build_title(part) | ||
local text = | local text = get_value(part.value, part) or part.textdefaultvalue or mw.title.getCurrentTitle().text | ||
local subtext = | local subtext = get_value(part.subtitle, part) or part.subtitledefaultvalue | ||
local classes = "avt-infobox-header " .. (part.class or "") | |||
-- Supprime l"indication d"homonymie | |||
text = mw.ustring.gsub(text, " [(][^()]*[)]$", "") | text = mw.ustring.gsub(text, " [(][^()]*[)]$", "") | ||
if subtext and (subtext ~= text) then | if subtext and (subtext ~= text) then | ||
text = text .. | text = text .. "<br/><small>" .. subtext .. "</small>" | ||
end | end | ||
local title = mw.html.create( | local title = mw.html.create("div") | ||
:addClass( | :addClass(classes) | ||
:tag("div") | |||
:addClass("avt-infobox-header-title") | |||
:tag( | |||
:addClass( | |||
:wikitext(text) | :wikitext(text) | ||
:allDone() | :allDone() | ||
Ligne 90 : | Ligne 63 : | ||
return title | return title | ||
end | end | ||
p. | p.build_title = p.build_title | ||
function p. | function p.build_images(part) | ||
local images = {} | local images = {} | ||
local upright, link, caption, alt | local upright, link, caption, alt | ||
if type(part.imageparameters) == | if type(part.imageparameters) == "string" then | ||
part.imageparameters = { part.imageparameters } | part.imageparameters = { part.imageparameters } | ||
end | end | ||
if not part.imageparameters then -- s | if not part.imageparameters then -- s"il n"y a pas de paramètre image, continuer, peut-être y a-t-il une image par défaut définie dans le module d"infobox | ||
part.imageparameters = {} | part.imageparameters = {} | ||
end | end | ||
Ligne 107 : | Ligne 80 : | ||
-- Images par défaut | -- Images par défaut | ||
if #images == 0 then | if #images == 0 then | ||
if part.defaultimages then | if part.defaultimages then | ||
images = part.defaultimages | images = part.defaultimages | ||
if type(images) == | if type(images) == "string" then | ||
images = { images } | images = { images } | ||
end | end | ||
Ligne 120 : | Ligne 90 : | ||
alt = part.defaultimagealt | alt = part.defaultimagealt | ||
if not alt then | if not alt then | ||
alt = | alt = "une illustration pour cette infobox serait bienvenue" | ||
end | end | ||
end | end | ||
Ligne 129 : | Ligne 99 : | ||
end | end | ||
upright = upright or | upright = upright or get_value(part.uprightparameter) or part.defaultupright or "1.2" | ||
link = link or | link = link or get_value(part.linkparameter) or part.defaultlink | ||
caption = caption or | caption = caption or get_value(part.captionparameter) or part.defaultcaption | ||
alt = alt or | alt = alt or get_value(part.altparameter) or part.defaultalt | ||
if tonumber(upright) then | if tonumber(upright) then | ||
Ligne 142 : | Ligne 112 : | ||
-- Partie image | -- Partie image | ||
local imagesString = | local imagesString = "" | ||
for i,image in pairs(images) do | for i,image in pairs(images) do | ||
if image == | if image == "-" then | ||
return | return | ||
end | end | ||
imagesString = imagesString .. | imagesString = imagesString .. "[[Fichier:" .. image .. "|frameless" | ||
if alt then | if alt then | ||
imagesString = imagesString .. | imagesString = imagesString .. "|alt=" .. alt | ||
end | end | ||
if link then | if link then | ||
imagesString = imagesString .. | imagesString = imagesString .. "|link=" .. link | ||
end | end | ||
if upright then | if upright then | ||
imagesString = imagesString .. | imagesString = imagesString .. "|upright=" .. upright | ||
elseif #images > 1 then | elseif #images > 1 then | ||
imagesString = imagesString .. | imagesString = imagesString .. "|upright=" .. ( 1 / #images ) | ||
end | end | ||
imagesString = imagesString .. | imagesString = imagesString .. "]]" | ||
end | end | ||
local image = mw.html.create( | local image = mw.html.create("div") | ||
:addClass("avt-infobox-images") | :addClass("avt-infobox-images") | ||
:css(style) | :css(style) | ||
Ligne 170 : | Ligne 140 : | ||
local captionobj | local captionobj | ||
if caption then | if caption then | ||
captionobj = mw.html.create( | captionobj = mw.html.create("div") | ||
:wikitext(caption) | :wikitext(caption) | ||
:css(part.legendstyle or {}) | :css(part.legendstyle or {}) | ||
Ligne 182 : | Ligne 152 : | ||
separator = separator(part) | separator = separator(part) | ||
end | end | ||
return mw.html.create( | return mw.html.create("div") | ||
:node(image) | :node(image) | ||
:node(captionobj) | :node(captionobj) | ||
Ligne 188 : | Ligne 158 : | ||
:done() | :done() | ||
end | end | ||
p. | p.build_images = p.build_images | ||
function p. | function p.build_text(part) | ||
local class = part.class or | local class = part.class or "" | ||
local style = { | local style = { | ||
[ | ["text-align"] = "center", | ||
[ | ["font-weight"] = "bold", | ||
} | } | ||
if part.style then | if part.style then | ||
Ligne 201 : | Ligne 171 : | ||
end | end | ||
end | end | ||
local text = | local text = get_value(part.value, part) or part.defaultvalue | ||
if text == | if text == "-" then | ||
return | return | ||
end | end | ||
if not text then | if not text then | ||
return nil | return nil | ||
end | end | ||
local formattedtext = mw.html.create( | local formattedtext = mw.html.create("p") | ||
:addClass(class) | :addClass(class) | ||
:css(style) | :css(style) | ||
Ligne 216 : | Ligne 185 : | ||
return formattedtext | return formattedtext | ||
end | end | ||
p. | p.build_text = p.build_text | ||
function p. | function p.build_row(part) | ||
local class = part.class or | local class = part.class or "" | ||
local style = part.style or {} | local style = part.style or {} | ||
local valueClass = part.valueClass or | local valueClass = part.valueClass or "" | ||
local valueStyle = part.valueStyle or {} | local valueStyle = part.valueStyle or {} | ||
local value, number = | local value, number = get_value(part.value, part) | ||
if not value then | if not value then | ||
Ligne 234 : | Ligne 203 : | ||
number = 0 -- == indéfini | number = 0 -- == indéfini | ||
end | end | ||
local label = part.label | local label = part.label | ||
Ligne 249 : | Ligne 210 : | ||
label = part.singularlabel | label = part.singularlabel | ||
end | end | ||
if type(label) == | if type(label) == "function" then | ||
label = label(localdata, localdata.item) | label = label(localdata, localdata.item) | ||
end | end | ||
-- format | -- format | ||
local formattedvalue = mw.html.create( | local formattedvalue = mw.html.create("div") | ||
-- Le "\n" est requis lorsque value est une liste | |||
-- commençant par "*" ou "#" | |||
:wikitext("\n" .. value) | |||
if part.hidden == true then | if part.hidden == true then | ||
formattedvalue | formattedvalue | ||
:attr({class="NavContent", style="display: none; text-align: left;"}) | :attr({class="NavContent", style="display: none; text-align: left;"}) | ||
formattedvalue = mw.html.create( | formattedvalue = mw.html.create("div") | ||
:attr({class="NavFrame", title="[Afficher]/[Masquer]", style="border: none; padding: 0;"}) | :attr({class="NavFrame", title="[Afficher]/[Masquer]", style="border: none; padding: 0;"}) | ||
:node(formattedvalue) | :node(formattedvalue) | ||
end | end | ||
formattedvalue = mw.html.create( | formattedvalue = mw.html.create("td") | ||
:node(formattedvalue) | :node(formattedvalue) | ||
:addClass(valueClass) | :addClass(valueClass) | ||
Ligne 272 : | Ligne 235 : | ||
local formattedlabel | local formattedlabel | ||
if label then | if label then | ||
formattedlabel = mw.html.create( | formattedlabel = mw.html.create("th") | ||
:attr( | :attr("scope", "row") | ||
:wikitext(label) | :wikitext(label) | ||
:done() | :done() | ||
end | end | ||
local row = mw.html.create( | local row = mw.html.create("tr") | ||
:addClass(class) | :addClass(class) | ||
:css(style) | :css(style) | ||
Ligne 286 : | Ligne 249 : | ||
return row | return row | ||
end | end | ||
p. | p.build_row = p.build_row | ||
function p. | function p.build_succession(part) | ||
if not part.value then | if not part.value then | ||
return nil | return nil | ||
Ligne 294 : | Ligne 257 : | ||
local values = part.value | local values = part.value | ||
local before = | local before = get_value(values["before"], part) | ||
local center = | local center = get_value(values["center"], part) | ||
local after = | local after = get_value(values["after"], part) | ||
if not before and not center and not after then | if not before and not center and not after then | ||
return nil | return nil | ||
end | end | ||
local rowI = mw.html.create( | local rowI = mw.html.create("tr") | ||
local styleI = {} | local styleI = {} | ||
local colspan = | local colspan = "2" | ||
styleI[ | styleI["padding"] = "1px" | ||
cellI = mw.html.create( | cellI = mw.html.create("td") | ||
:attr({colspan = colspan}) | :attr({colspan = colspan}) | ||
:attr({align = | :attr({align = "center"}) | ||
:css(styleI) | :css(styleI) | ||
local styleT = {} | local styleT = {} | ||
styleT[ | styleT["margin"] = "0px" | ||
styleT[ | styleT["background-color"] = "transparent" | ||
styleT[ | styleT["width"] = "100%" | ||
tabC = mw.html.create( | tabC = mw.html.create("table") | ||
:attr({cellspacing = | :attr({cellspacing = "0"}) | ||
:addClass( | :addClass("navigation-not-searchable") | ||
:css(styleT) | :css(styleT) | ||
local row = mw.html.create( | local row = mw.html.create("tr") | ||
local color = part.color | local color = part.color | ||
Ligne 334 : | Ligne 292 : | ||
local arrowRight | local arrowRight | ||
if color == | if color == "default" then | ||
arrowLeft = | arrowLeft = "[[Fichier:SCRWEST.png|13px|alt=Précédent|link=]]" | ||
arrowRight = | arrowRight = "[[Fichier:SCREAST.png|13px|alt=Suivant|link=]]" | ||
else | else | ||
arrowLeft = | arrowLeft = "[[Fichier:SCRWEST.png|13px|alt=Précédent|link=]]" | ||
arrowRight = | arrowRight = "[[Fichier:SCREAST.png|13px|alt=Suivant|link=]]" | ||
style[ | style["background-color"] = color | ||
end | end | ||
local styleTrans = {} | local styleTrans = {} | ||
local widthCell = | local widthCell = "44%" | ||
if center then | if center then | ||
widthCenter = | widthCenter = "28%" | ||
widthCell = | widthCell = "29%" | ||
end | end | ||
local formattedbefore | local formattedbefore | ||
if before then | if before then | ||
formattedbefore = mw.html.create( | formattedbefore = mw.html.create("td") | ||
:attr({valign = | :attr({valign = "middle"}) | ||
:attr({align = | :attr({align = "left"}) | ||
:attr({width = | :attr({width = "5%"}) | ||
:css(style) | :css(style) | ||
:wikitext(arrowLeft) | :wikitext(arrowLeft) | ||
:done() | :done() | ||
row:node(formattedbefore) | row:node(formattedbefore) | ||
formattedbefore = mw.html.create( | formattedbefore = mw.html.create("td") | ||
:attr({width = | :attr({width = "1%"}) | ||
:css(style) | :css(style) | ||
:wikitext( | :wikitext("") | ||
:done() | :done() | ||
row:node(formattedbefore) | row:node(formattedbefore) | ||
formattedbefore = mw.html.create( | formattedbefore = mw.html.create("td") | ||
:attr({align = | :attr({align = "left"}) | ||
:attr({valign = | :attr({valign = "middle"}) | ||
:attr({width = widthCell}) | :attr({width = widthCell}) | ||
:css(style) | :css(style) | ||
Ligne 376 : | Ligne 334 : | ||
row:node(formattedbefore) | row:node(formattedbefore) | ||
else | else | ||
formattedbefore = mw.html.create( | formattedbefore = mw.html.create("td") | ||
:attr({valign = | :attr({valign = "middle"}) | ||
:attr({align = | :attr({align = "left"}) | ||
:attr({width = | :attr({width = "5%"}) | ||
:css(styleTrans) | :css(styleTrans) | ||
:wikitext( | :wikitext("") | ||
:done() | :done() | ||
row:node(formattedbefore) | row:node(formattedbefore) | ||
formattedbefore = mw.html.create( | formattedbefore = mw.html.create("td") | ||
:attr({width = | :attr({width = "1%"}) | ||
:css(styleTrans) | :css(styleTrans) | ||
:wikitext( | :wikitext("") | ||
:done() | :done() | ||
row:node(formattedbefore) | row:node(formattedbefore) | ||
formattedbefore = mw.html.create( | formattedbefore = mw.html.create("td") | ||
:attr({align = | :attr({align = "left"}) | ||
:attr({valign = | :attr({valign = "middle"}) | ||
:attr({width = widthCell}) | :attr({width = widthCell}) | ||
:css(styleTrans) | :css(styleTrans) | ||
:wikitext( | :wikitext("") | ||
:done() | :done() | ||
row:node(formattedbefore) | row:node(formattedbefore) | ||
Ligne 401 : | Ligne 359 : | ||
local formattedcenter | local formattedcenter | ||
formattedcenter = mw.html.create( | formattedcenter = mw.html.create("td") | ||
:attr({width = | :attr({width = "1%"}) | ||
:css(styleTrans) | :css(styleTrans) | ||
:wikitext( | :wikitext("") | ||
:done() | :done() | ||
row:node(formattedcenter) | row:node(formattedcenter) | ||
if center then | if center then | ||
formattedcenter = mw.html.create( | formattedcenter = mw.html.create("td") | ||
:attr({align = | :attr({align = "center"}) | ||
:attr({valign = | :attr({valign = "middle"}) | ||
:attr({width = widthCenter}) | :attr({width = widthCenter}) | ||
:css(style) | :css(style) | ||
Ligne 417 : | Ligne 375 : | ||
:done() | :done() | ||
row:node(formattedcenter) | row:node(formattedcenter) | ||
formattedcenter = mw.html.create( | formattedcenter = mw.html.create("td") | ||
:attr({width = | :attr({width = "1%"}) | ||
:css(styleTrans) | :css(styleTrans) | ||
:wikitext( | :wikitext("") | ||
:done() | :done() | ||
row:node(formattedcenter) | row:node(formattedcenter) | ||
Ligne 427 : | Ligne 385 : | ||
local formattedafter | local formattedafter | ||
if after then | if after then | ||
formattedafter = mw.html.create( | formattedafter = mw.html.create("td") | ||
:attr({align = | :attr({align = "right"}) | ||
:attr({valign = | :attr({valign = "middle"}) | ||
:attr({width = widthCell}) | :attr({width = widthCell}) | ||
:css(style) | :css(style) | ||
Ligne 435 : | Ligne 393 : | ||
:done() | :done() | ||
row:node(formattedafter) | row:node(formattedafter) | ||
formattedbefore = mw.html.create( | formattedbefore = mw.html.create("td") | ||
:attr({width = | :attr({width = "1%"}) | ||
:css(style) | :css(style) | ||
:wikitext( | :wikitext("") | ||
:done() | :done() | ||
row:node(formattedbefore) | row:node(formattedbefore) | ||
formattedafter = mw.html.create( | formattedafter = mw.html.create("td") | ||
:attr({align = | :attr({align = "right"}) | ||
:attr({valign = | :attr({valign = "middle"}) | ||
:attr({width = | :attr({width = "5%"}) | ||
:css(style) | :css(style) | ||
:wikitext(arrowRight) | :wikitext(arrowRight) | ||
Ligne 450 : | Ligne 408 : | ||
row:node(formattedafter) | row:node(formattedafter) | ||
else | else | ||
formattedafter = mw.html.create( | formattedafter = mw.html.create("td") | ||
:attr({align = | :attr({align = "right"}) | ||
:attr({valign = | :attr({valign = "middle"}) | ||
:attr({width = widthCell}) | :attr({width = widthCell}) | ||
:css(styleTrans) | :css(styleTrans) | ||
:wikitext( | :wikitext("") | ||
:done() | :done() | ||
row:node(formattedafter) | row:node(formattedafter) | ||
formattedbefore = mw.html.create( | formattedbefore = mw.html.create("td") | ||
:attr({width = | :attr({width = "1%"}) | ||
:css(styleTrans) | :css(styleTrans) | ||
:wikitext( | :wikitext("") | ||
:done() | :done() | ||
row:node(formattedbefore) | row:node(formattedbefore) | ||
formattedafter = mw.html.create( | formattedafter = mw.html.create("td") | ||
:attr({align = | :attr({align = "right"}) | ||
:attr({valign = | :attr({valign = "middle"}) | ||
:attr({width = | :attr({width = "5%"}) | ||
:css(styleTrans) | :css(styleTrans) | ||
:wikitext( | :wikitext("") | ||
:done() | :done() | ||
row:node(formattedafter) | row:node(formattedafter) | ||
Ligne 484 : | Ligne 442 : | ||
return rowI | return rowI | ||
end | end | ||
p. | p.build_succession = p.build_succession | ||
function p. | function p.build_row1col(part) | ||
if not part.value then | if not part.value then | ||
return nil | return nil | ||
end | end | ||
local class = | local class = "row1col" | ||
if part.class then | if part.class then | ||
class = class .. | class = class .. " " .. part.class | ||
end | end | ||
local text = | local text = get_value(part.value, part) | ||
if not text then | if not text then | ||
Ligne 503 : | Ligne 461 : | ||
local formattedlabel | local formattedlabel | ||
formattedlabel = mw.html.create( | formattedlabel = mw.html.create("th") | ||
:attr({ colspan = | :attr({ colspan = "2" }) | ||
:addClass(class) | :addClass(class) | ||
:wikitext(text) | :wikitext(text) | ||
:done() | :done() | ||
local row = mw.html.create( | local row = mw.html.create("tr") | ||
:node(formattedlabel) | :node(formattedlabel) | ||
:done() | :done() | ||
Ligne 515 : | Ligne 473 : | ||
return row | return row | ||
end | end | ||
p. | p.build_row1Col = p.build_row1col | ||
function p.buildtable(part) | function p.buildtable(part) | ||
Ligne 521 : | Ligne 479 : | ||
if part.collapseparameters then | if part.collapseparameters then | ||
if part.collapseparameters.collapsible == true then | if part.collapseparameters.collapsible == true then | ||
class = | class = "mw-collapsible" | ||
if part.collapseparameters.collapsed == true then | if part.collapseparameters.collapsed == true then | ||
class = class .. | class = class .." mw-collapsed" | ||
end | end | ||
end | end | ||
end | end | ||
local tab = mw.html.create( | local tab = mw.html.create("table") | ||
:addClass(class) | :addClass(class) | ||
:css(part.style or {}) | :css(part.style or {}) | ||
Ligne 539 : | Ligne 497 : | ||
while (i <= #rows) do | while (i <= #rows) do | ||
local l = rows[i] | local l = rows[i] | ||
if type(l) == | if type(l) == "function" then | ||
l = l(localdata, localdata.item) | l = l(localdata, localdata.item) | ||
end | end | ||
if (type(l) == | if (type(l) == "table") and (l.type == "multi") then | ||
table.remove(rows, i) | table.remove(rows, i) | ||
for j, row in ipairs(l.rows) do | for j, row in ipairs(l.rows) do | ||
table.insert(rows, i + j - 1, row) | table.insert(rows, i + j - 1, row) | ||
end | end | ||
elseif type(l) == | elseif type(l) == "nil" then | ||
table.remove(rows, i) | table.remove(rows, i) | ||
elseif type(l) ~= | elseif type(l) ~= "table" then | ||
return error( | return error("les lignes d'infobox ('rows') doivent être des tables, est " .. type(l)) | ||
else | else | ||
i = i + 1 | i = i + 1 | ||
Ligne 583 : | Ligne 541 : | ||
local style = part.titlestyle or {} | local style = part.titlestyle or {} | ||
title = mw.html.create( | title = mw.html.create("caption") | ||
:attr({ colspan = | :attr({ colspan = "2" }) | ||
:css(style) | :css(style) | ||
:wikitext(text) | :wikitext(text) | ||
Ligne 608 : | Ligne 566 : | ||
--[[ | --[[ | ||
Construction de l | Construction de l"infobox | ||
]] | ]] | ||
function p.buildblock(block) | function p.buildblock(block) | ||
if type(block) == | if type(block) == "function" then | ||
block = block(localdata) | block = block(localdata) | ||
end | end | ||
Ligne 618 : | Ligne 576 : | ||
-- list of functions for block buildings | -- list of functions for block buildings | ||
local blocktypes = { | local blocktypes = { | ||
[ | ["images"] = p.build_images, | ||
[ | ["table"] = p.buildtable, | ||
["row"] = p.build_row, | |||
[ | ["row1col"] = p.build_row1col, | ||
[ | ["succession"] = p.build_succession, | ||
[ | ["text"] = p.build_text, | ||
[ | ["title"] = p.build_title, | ||
[ | |||
} | } | ||
if type(block) ~= | if type(block) ~= "table" or (not block.type) or (not blocktypes[block.type]) then | ||
return blocktypes[ | return blocktypes["invalid"](block) | ||
end | end | ||
return blocktypes[block.type](block) | return blocktypes[block.type](block) | ||
Ligne 635 : | Ligne 592 : | ||
function p.build() | function p.build() | ||
localdata = require( | localdata = require("Module:Infobox/Localdata") | ||
item = localdata.item | item = localdata.item | ||
-- chargement du module de paramétrage | -- chargement du module de paramétrage | ||
local moduledata = require( | local moduledata = require("Module:Infobox/" .. localdata.modulename) | ||
moduledata.name = localdata.modulename | moduledata.name = localdata.modulename | ||
-- class | -- class | ||
local class = | local class = "infobox avt-infobox" | ||
if moduledata.class then | if moduledata.class then | ||
class = class .. | class = class .. " " .. moduledata.class | ||
end | end | ||
Dernière version du 1 décembre 2023 à 18:18
Ce module génère une infobox à partir d'un sous-module de paramétrage. Il s'agit d'une version simplifiée de Module:Infobox présent sur Wikipédia.
Créer une infobox[modifier le wikicode]
Pour créer une nouvelle infobox, il faut déclarer son paramétrage comme sous-module du module Infobox. Par exemple, si on souhaite créer Infobox Planète
alors on déclarera son paramétrage dans Module:Infobox/Planète
.
Le sous-module doit retourner une table contenant la structure de l'infobox.
-- Module https://fallout-wiki.com/Module:Infobox utilisé comme source et
-- lui-même inspiré de https://fr.wikipedia.org/wiki/Module:Infobox
-- Gère la construction des infobox dans le paramétrage doit être défini
-- un sous-module.
local p = {}
-- données concernant les paramètres passés au modèle
local localdata = {}
-- données concernant la page où est affichée l"infobox
local page = {
name = mw.title.getCurrentTitle().prefixedText,
namespace = mw.title.getCurrentTitle().namespace
}
-- l"objet principal à retourner
local infobox = mw.html.create("div")
--[[ Outils ]]
local function get_value(value, part)
if type(value) == "string" then
return localdata[value]
elseif type(value) == "function" then
return value(localdata, part)
elseif type(value) == "table" then
-- si plusieurs paramètres possibles (legacy de vieux code),
-- prendre le premier non vide
for i, j in pairs(value) do
if localdata[j] then
return localdata[j]
end
end
end
end
function p.separator()
return mw.html.create("hr"):addClass("separator")
end
--[[ Construction des blocs ]]
function p.build_title(part)
local text = get_value(part.value, part) or part.textdefaultvalue or mw.title.getCurrentTitle().text
local subtext = get_value(part.subtitle, part) or part.subtitledefaultvalue
local classes = "avt-infobox-header " .. (part.class or "")
-- Supprime l"indication d"homonymie
text = mw.ustring.gsub(text, " [(][^()]*[)]$", "")
if subtext and (subtext ~= text) then
text = text .. "<br/><small>" .. subtext .. "</small>"
end
local title = mw.html.create("div")
:addClass(classes)
:tag("div")
:addClass("avt-infobox-header-title")
:wikitext(text)
:allDone()
return title
end
p.build_title = p.build_title
function p.build_images(part)
local images = {}
local upright, link, caption, alt
if type(part.imageparameters) == "string" then
part.imageparameters = { part.imageparameters }
end
if not part.imageparameters then -- s"il n"y a pas de paramètre image, continuer, peut-être y a-t-il une image par défaut définie dans le module d"infobox
part.imageparameters = {}
end
for j, k in ipairs(part.imageparameters) do
table.insert(images, localdata[k])
end
-- Images par défaut
if #images == 0 then
if part.defaultimages then
images = part.defaultimages
if type(images) == "string" then
images = { images }
end
upright = part.defaultimageupright
caption = part.defaultimagecaption
link = part.defaultimagelink
alt = part.defaultimagealt
if not alt then
alt = "une illustration pour cette infobox serait bienvenue"
end
end
end
if #images == 0 then
return nil
end
upright = upright or get_value(part.uprightparameter) or part.defaultupright or "1.2"
link = link or get_value(part.linkparameter) or part.defaultlink
caption = caption or get_value(part.captionparameter) or part.defaultcaption
alt = alt or get_value(part.altparameter) or part.defaultalt
if tonumber(upright) then
upright = tostring( tonumber(upright) / #images )
end
local style = part.style or {}
-- Partie image
local imagesString = ""
for i,image in pairs(images) do
if image == "-" then
return
end
imagesString = imagesString .. "[[Fichier:" .. image .. "|frameless"
if alt then
imagesString = imagesString .. "|alt=" .. alt
end
if link then
imagesString = imagesString .. "|link=" .. link
end
if upright then
imagesString = imagesString .. "|upright=" .. upright
elseif #images > 1 then
imagesString = imagesString .. "|upright=" .. ( 1 / #images )
end
imagesString = imagesString .. "]]"
end
local image = mw.html.create("div")
:addClass("avt-infobox-images")
:css(style)
:wikitext(imagesString)
-- Partie légende
local captionobj
if caption then
captionobj = mw.html.create("div")
:wikitext(caption)
:css(part.legendstyle or {})
:addClass("avt-infobox-images-legend")
:done()
end
-- séparateur
local separator
if part.separator then
separator = separator(part)
end
return mw.html.create("div")
:node(image)
:node(captionobj)
:node(separator)
:done()
end
p.build_images = p.build_images
function p.build_text(part)
local class = part.class or ""
local style = {
["text-align"] = "center",
["font-weight"] = "bold",
}
if part.style then
for i, j in pairs(part.style) do
style[i] = j
end
end
local text = get_value(part.value, part) or part.defaultvalue
if text == "-" then
return
end
if not text then
return nil
end
local formattedtext = mw.html.create("p")
:addClass(class)
:css(style)
:wikitext(text)
:done()
return formattedtext
end
p.build_text = p.build_text
function p.build_row(part)
local class = part.class or ""
local style = part.style or {}
local valueClass = part.valueClass or ""
local valueStyle = part.valueStyle or {}
local value, number = get_value(part.value, part)
if not value then
value = part.defaultvalue
end
if value == nil then
return nil
end
if not number then
number = 0 -- == indéfini
end
local label = part.label
if number > 1 and part.plurallabel then
label = part.plurallabel
elseif number == 1 and part.singularlabel then
label = part.singularlabel
end
if type(label) == "function" then
label = label(localdata, localdata.item)
end
-- format
local formattedvalue = mw.html.create("div")
-- Le "\n" est requis lorsque value est une liste
-- commençant par "*" ou "#"
:wikitext("\n" .. value)
if part.hidden == true then
formattedvalue
:attr({class="NavContent", style="display: none; text-align: left;"})
formattedvalue = mw.html.create("div")
:attr({class="NavFrame", title="[Afficher]/[Masquer]", style="border: none; padding: 0;"})
:node(formattedvalue)
end
formattedvalue = mw.html.create("td")
:node(formattedvalue)
:addClass(valueClass)
:css(valueStyle)
:allDone()
local formattedlabel
if label then
formattedlabel = mw.html.create("th")
:attr("scope", "row")
:wikitext(label)
:done()
end
local row = mw.html.create("tr")
:addClass(class)
:css(style)
:node(formattedlabel)
:node(formattedvalue)
:done()
return row
end
p.build_row = p.build_row
function p.build_succession(part)
if not part.value then
return nil
end
local values = part.value
local before = get_value(values["before"], part)
local center = get_value(values["center"], part)
local after = get_value(values["after"], part)
if not before and not center and not after then
return nil
end
local rowI = mw.html.create("tr")
local styleI = {}
local colspan = "2"
styleI["padding"] = "1px"
cellI = mw.html.create("td")
:attr({colspan = colspan})
:attr({align = "center"})
:css(styleI)
local styleT = {}
styleT["margin"] = "0px"
styleT["background-color"] = "transparent"
styleT["width"] = "100%"
tabC = mw.html.create("table")
:attr({cellspacing = "0"})
:addClass("navigation-not-searchable")
:css(styleT)
local row = mw.html.create("tr")
local color = part.color
local style = {}
local arrowLeft
local arrowRight
if color == "default" then
arrowLeft = "[[Fichier:SCRWEST.png|13px|alt=Précédent|link=]]"
arrowRight = "[[Fichier:SCREAST.png|13px|alt=Suivant|link=]]"
else
arrowLeft = "[[Fichier:SCRWEST.png|13px|alt=Précédent|link=]]"
arrowRight = "[[Fichier:SCREAST.png|13px|alt=Suivant|link=]]"
style["background-color"] = color
end
local styleTrans = {}
local widthCell = "44%"
if center then
widthCenter = "28%"
widthCell = "29%"
end
local formattedbefore
if before then
formattedbefore = mw.html.create("td")
:attr({valign = "middle"})
:attr({align = "left"})
:attr({width = "5%"})
:css(style)
:wikitext(arrowLeft)
:done()
row:node(formattedbefore)
formattedbefore = mw.html.create("td")
:attr({width = "1%"})
:css(style)
:wikitext("")
:done()
row:node(formattedbefore)
formattedbefore = mw.html.create("td")
:attr({align = "left"})
:attr({valign = "middle"})
:attr({width = widthCell})
:css(style)
:wikitext(before)
:done()
row:node(formattedbefore)
else
formattedbefore = mw.html.create("td")
:attr({valign = "middle"})
:attr({align = "left"})
:attr({width = "5%"})
:css(styleTrans)
:wikitext("")
:done()
row:node(formattedbefore)
formattedbefore = mw.html.create("td")
:attr({width = "1%"})
:css(styleTrans)
:wikitext("")
:done()
row:node(formattedbefore)
formattedbefore = mw.html.create("td")
:attr({align = "left"})
:attr({valign = "middle"})
:attr({width = widthCell})
:css(styleTrans)
:wikitext("")
:done()
row:node(formattedbefore)
end
local formattedcenter
formattedcenter = mw.html.create("td")
:attr({width = "1%"})
:css(styleTrans)
:wikitext("")
:done()
row:node(formattedcenter)
if center then
formattedcenter = mw.html.create("td")
:attr({align = "center"})
:attr({valign = "middle"})
:attr({width = widthCenter})
:css(style)
:wikitext(center)
:done()
row:node(formattedcenter)
formattedcenter = mw.html.create("td")
:attr({width = "1%"})
:css(styleTrans)
:wikitext("")
:done()
row:node(formattedcenter)
end
local formattedafter
if after then
formattedafter = mw.html.create("td")
:attr({align = "right"})
:attr({valign = "middle"})
:attr({width = widthCell})
:css(style)
:wikitext(after)
:done()
row:node(formattedafter)
formattedbefore = mw.html.create("td")
:attr({width = "1%"})
:css(style)
:wikitext("")
:done()
row:node(formattedbefore)
formattedafter = mw.html.create("td")
:attr({align = "right"})
:attr({valign = "middle"})
:attr({width = "5%"})
:css(style)
:wikitext(arrowRight)
:done()
row:node(formattedafter)
else
formattedafter = mw.html.create("td")
:attr({align = "right"})
:attr({valign = "middle"})
:attr({width = widthCell})
:css(styleTrans)
:wikitext("")
:done()
row:node(formattedafter)
formattedbefore = mw.html.create("td")
:attr({width = "1%"})
:css(styleTrans)
:wikitext("")
:done()
row:node(formattedbefore)
formattedafter = mw.html.create("td")
:attr({align = "right"})
:attr({valign = "middle"})
:attr({width = "5%"})
:css(styleTrans)
:wikitext("")
:done()
row:node(formattedafter)
end
row:done()
tabC:node(row)
tabC:done()
cellI:node(tabC)
cellI:done()
rowI:node(cellI)
rowI:allDone()
return rowI
end
p.build_succession = p.build_succession
function p.build_row1col(part)
if not part.value then
return nil
end
local class = "row1col"
if part.class then
class = class .. " " .. part.class
end
local text = get_value(part.value, part)
if not text then
return nil
end
local formattedlabel
formattedlabel = mw.html.create("th")
:attr({ colspan = "2" })
:addClass(class)
:wikitext(text)
:done()
local row = mw.html.create("tr")
:node(formattedlabel)
:done()
return row
end
p.build_row1Col = p.build_row1col
function p.buildtable(part)
local class = nil
if part.collapseparameters then
if part.collapseparameters.collapsible == true then
class = "mw-collapsible"
if part.collapseparameters.collapsed == true then
class = class .." mw-collapsed"
end
end
end
local tab = mw.html.create("table")
:addClass(class)
:css(part.style or {})
local rows = part.rows
-- expand parameters so that we have a list of tables
local i = 1
while (i <= #rows) do
local l = rows[i]
if type(l) == "function" then
l = l(localdata, localdata.item)
end
if (type(l) == "table") and (l.type == "multi") then
table.remove(rows, i)
for j, row in ipairs(l.rows) do
table.insert(rows, i + j - 1, row)
end
elseif type(l) == "nil" then
table.remove(rows, i)
elseif type(l) ~= "table" then
return error("les lignes d'infobox ('rows') doivent être des tables, est " .. type(l))
else
i = i + 1
end
end
-- CREATE ROW
local expandedrows = {}
for k, row in ipairs(rows) do
local v = p.buildblock(row)
if v then
table.insert(expandedrows, v)
end
end
if (#expandedrows == 0) then
return nil
end
rows = expandedrows
-- ADD TITLE
local title
if part.title or part.singulartitle or part.pluraltitle then
local text
if #rows > 1 and part.pluraltitle then
text = part.pluraltitle
elseif #rows == 1 and part.singulartitle then
text = part.singulartitle
else
text = part.title
end
local style = part.titlestyle or {}
title = mw.html.create("caption")
:attr({ colspan = "2" })
:css(style)
:wikitext(text)
:done()
end
if title then
tab:node(title)
end
for i, j in pairs (rows) do
tab:node(j)
end
if part.separator then
local separator = p.separator(part)
tab:node(separator)
end
tab:allDone()
return tab
end
p.buildTable = p.buildtable
--[[
Construction de l"infobox
]]
function p.buildblock(block)
if type(block) == "function" then
block = block(localdata)
end
-- list of functions for block buildings
local blocktypes = {
["images"] = p.build_images,
["table"] = p.buildtable,
["row"] = p.build_row,
["row1col"] = p.build_row1col,
["succession"] = p.build_succession,
["text"] = p.build_text,
["title"] = p.build_title,
}
if type(block) ~= "table" or (not block.type) or (not blocktypes[block.type]) then
return blocktypes["invalid"](block)
end
return blocktypes[block.type](block)
end
p.buildBlock = p.buildblock
function p.build()
localdata = require("Module:Infobox/Localdata")
item = localdata.item
-- chargement du module de paramétrage
local moduledata = require("Module:Infobox/" .. localdata.modulename)
moduledata.name = localdata.modulename
-- class
local class = "infobox avt-infobox"
if moduledata.class then
class = class .. " " .. moduledata.class
end
-- style
local style = moduledata.style or {}
-- build infobox
infobox :addClass(class)
:css(style)
for i, j in pairs(moduledata.parts) do
infobox:node(p.buildblock(j))
end
return tostring(infobox)
end
return p