« Module:Infobox » : différence entre les versions
Aucun résumé des modifications |
Aucun résumé des modifications |
||
| Ligne 6 : | Ligne 6 : | ||
local localdata = {} | local localdata = {} | ||
local pagename = mw.title.getCurrentTitle().prefixedText | local pagename = mw.title.getCurrentTitle().prefixedText | ||
local maincolor -- la couleur par défaut des titres, sous-titres etc. | |||
local i18n = { | local i18n = { | ||
| Ligne 13 : | Ligne 13 : | ||
['edit item'] = 'modifier Wikidata' | ['edit item'] = 'modifier Wikidata' | ||
} | } | ||
local function getWikidataValue(params) -- helper function | local function getWikidataValue(params) -- helper function | ||
| Ligne 36 : | Ligne 37 : | ||
local function buildtitle(params) | local function buildtitle(params) | ||
local text = text or localdata[params.value] or params.textdefaultvalue or mw.title.getCurrentTitle().text | |||
local class = params.class or "entete" | local class = params.class or "entete" | ||
local style = params.style or {} | local style = params.style or {} | ||
local text = localdata[params. | if not style['background-color'] then | ||
style['background-color'] = maincolor | |||
end | |||
local text = localdata[params.value] or params.textdefaultvalue or mw.title.getCurrentTitle().text | |||
local title = mw.html.create('p') | local title = mw.html.create('p') | ||
:addClass(class) | :addClass(class) | ||
| Ligne 48 : | Ligne 53 : | ||
local function buildnavbox(params) | local function buildnavbox(params) | ||
local class = params.class or | local class = "overflow nav " .. (params.class or '') | ||
local style = params.style or {['border-width'] = '1px'} | local style = params.style or {['border-width'] = '1px'} | ||
if not style['border-color'] | |||
then style['border-color'] = maincolor | |||
end | |||
local previousval = localdata[params.previousparameter] | local previousval = localdata[params.previousparameter] | ||
local nextval = localdata[params.nextparameter] | local nextval = localdata[params.nextparameter] | ||
local navbox = | local navbox = | ||
mw.html.create('p') | mw.html.create('p') | ||
:addClass(class) | |||
:css(style) | :css(style) | ||
:tag('span') | :tag('span') | ||
| Ligne 86 : | Ligne 95 : | ||
captiontext = captiontext(localdata) | captiontext = captiontext(localdata) | ||
end | end | ||
local style = params.style or {padding ='5px 0'} | |||
local style = params.style or '' | local class = "images" | ||
local class = | |||
if #images == 2 then | if #images == 2 then | ||
class = image2 | class = "image2" | ||
end | end | ||
-- Partie image | -- Partie image | ||
local imagesString = '' | local imagesString = '' | ||
for i,image in pairs(images) do | for i,image in pairs(images) do | ||
imagesString = imagesString .. '[[Fichier:' .. image .. '| | imagesString = imagesString .. '[[Fichier:' .. image .. '|frameless' | ||
if #images == 1 then | if #images == 1 then | ||
imagesString = imagesString .. '|center' | imagesString = imagesString .. '|center' | ||
| Ligne 108 : | Ligne 117 : | ||
end | end | ||
local image = mw.html.create('div') | |||
:css(style) | |||
:addClass("images") | |||
:wikitext(imagesString) | |||
-- Partie légende | -- Partie légende | ||
| Ligne 118 : | Ligne 130 : | ||
:addClass("legend") | :addClass("legend") | ||
:done() | :done() | ||
end | end | ||
return | return image, caption | ||
end | end | ||
| Ligne 127 : | Ligne 138 : | ||
local class = params.class or '' | local class = params.class or '' | ||
local style = params.style or {} | local style = params.style or {} | ||
if not style['background-color'] then | |||
style['background-color'] = maincolor | |||
end | |||
local text = localdata[params.value] or getWikidataValue(params) or params.defaulttext | local text = localdata[params.value] or getWikidataValue(params) or params.defaulttext | ||
| Ligne 196 : | Ligne 210 : | ||
local title | local title | ||
if params.title then | if params.title then | ||
local text = params.title.value or error('no value provided for this title') | local text | ||
local style = params.title.style or {['text-align'] = 'center', ['background-color'] | if type(params.title) == 'string' then -- raccourci moche mais pratique : n'utiliser qu'une chaîne pour le titre | ||
text = params.title | |||
else | |||
text = params.title.value or error('no value provided for this title') | |||
params = {} | |||
end | |||
local style = params.title.style or {['text-align'] = 'center', color = '000000'} | |||
if not style['background-color'] then | |||
style['background-color'] = maincolor | |||
end | |||
local colspan = params.title.colspan or '2' | local colspan = params.title.colspan or '2' | ||
title = mw.html.create('th') | title = mw.html.create('th') | ||
| Ligne 271 : | Ligne 294 : | ||
if not params then params = {} end | if not params then params = {} end | ||
local class = params.class or ' | local class = 'navbar noprint ' .. (params.class or '') | ||
local style = params.style or {border = | local style = params.style or {} | ||
if not style['border-top-style'] then | |||
style['border-top-style'] = 'solid' | |||
end | |||
if not style['border-width'] then | |||
style['border-width'] = '1px' | |||
end | |||
if not style['border-color'] then | |||
style['border-color'] = maincolor | |||
end | |||
if not style['font-size'] then | |||
style['font-size'] = '80%' | |||
end | |||
local backlinktext = '[' .. tostring( mw.uri.fullUrl( pagename, '&action=edit§ion=0' ) ) .. ' ' .. i18n['edit infobox'] .. ']' | local backlinktext = '[' .. tostring( mw.uri.fullUrl( pagename, '&action=edit§ion=0' ) ) .. ' ' .. i18n['edit infobox'] .. ']' | ||
local backlink = mw.html.create(' | local backlink = mw.html.create('span') | ||
:wikitext(backlinktext) | :wikitext(backlinktext) | ||
:addClass('plainlinks') | :addClass('plainlinks') | ||
| Ligne 284 : | Ligne 318 : | ||
itemlinktext = '[[d:' .. item.id .. '|' .. i18n['edit item'] .. ']]' | itemlinktext = '[[d:' .. item.id .. '|' .. i18n['edit item'] .. ']]' | ||
end | end | ||
local itemlink = mw.html.create(' | local itemlink = mw.html.create('span') | ||
:wikitext(itemlinktext) | :wikitext(itemlinktext) | ||
:done() | :done() | ||
local doclinktext = '[[Image:Gtk-dialog-info.svg|12px|link=' .. infoboxname .. '|' .. i18n['see doc'] .. ']]' | local doclinktext = '[[Image:Gtk-dialog-info.svg|12px|link=' .. infoboxname .. '|' .. i18n['see doc'] .. ']]' | ||
local doclink = mw.html.create(' | local doclink = mw.html.create('span') | ||
:css({['text-align'] = "right"}) | :css({['text-align'] = "right"}) | ||
:wikitext(doclinktext) | :wikitext(doclinktext) | ||
:done() | :done() | ||
local footer = mw.html.create(' | local footer = mw.html.create('p') | ||
: | :addClass(class) | ||
:css(style) | |||
:node(backlink) | |||
:node(itemlink) | |||
:node(doclink) | |||
return footer | return footer | ||
end | end | ||
| Ligne 310 : | Ligne 342 : | ||
local class = localinfoboxmodule.class or "infobox_v3" | local class = localinfoboxmodule.class or "infobox_v3" | ||
local parts = localinfoboxmodule.parts | local parts = localinfoboxmodule.parts | ||
maincolor = localinfoboxmodule.maincolor or "#E1E1E1" | |||
local footerparams = localinfoboxmodule.footer or {} | local footerparams = localinfoboxmodule.footer or {} | ||
infobox:css(style) | infobox:css(style) | ||
| Ligne 320 : | Ligne 353 : | ||
infobox:node(buildtitle(j)) | infobox:node(buildtitle(j)) | ||
elseif j.type == 'images' then | elseif j.type == 'images' then | ||
infobox:node( | local image, caption = buildimages(j) | ||
infobox:node(image):node(caption) | |||
elseif j.type == 'text' then | elseif j.type == 'text' then | ||
infobox:node(buildtext(j)) | infobox:node(buildtext(j)) | ||
Version du 22 septembre 2014 à 15:42
local p = {} local infobox = mw.html.create('div') local wikidata = require('Module:Wikidata') local maintenance = local infoboxname, infoboxmodule local localdata = {} local pagename = mw.title.getCurrentTitle().prefixedText local maincolor -- la couleur par défaut des titres, sous-titres etc.
local i18n = { ['see doc'] = 'Documentation du modèle', ['edit infobox'] = 'modifier l\'infobox', ['edit item'] = 'modifier Wikidata' }
local function getWikidataValue(params) -- helper function
if not item then
return nil
end
params.item = item
if params.wikidata then
if type(params.wikidata) == 'function' then
return params.wikidata()
else
return params.wikidata
end
elseif params.property then
local value = wikidata.formatStatements({property = params.property})
if value then
maintenance = maintenance ..
end
else
return nil
end
end
local function buildtitle(params) local text = text or localdata[params.value] or params.textdefaultvalue or mw.title.getCurrentTitle().text local class = params.class or "entete" local style = params.style or {} if not style['background-color'] then style['background-color'] = maincolor end local text = localdata[params.value] or params.textdefaultvalue or mw.title.getCurrentTitle().text local title = mw.html.create('p') :addClass(class) :css(style) :wikitext(text) :done() return title end
local function buildnavbox(params) local class = "overflow nav " .. (params.class or ) local style = params.style or {['border-width'] = '1px'} if not style['border-color'] then style['border-color'] = maincolor end local previousval = localdata[params.previousparameter] local nextval = localdata[params.nextparameter] local navbox = mw.html.create('p') :addClass(class) :css(style) :tag('span') :addClass('prev_bloc') :wikitext(previousval) :done() :tag('span') :addClass('next_bloc') :wikitext(nextval) :done() :done() return navbox end
local function buildimages(params)
local images = {}
for j, k in ipairs(params.imageparameters) do
table.insert(images, localdata[k])
end
if #images == 0 then
images = getWikidataValue(params)
end
if (not images) or #images == 0 then
images = params.defaultimage
end
if not images then
return nil
end
local captiontext = localdata[params.captionparameter] or params.defaultcaption
if type(captiontext) == 'function' then
captiontext = captiontext(localdata)
end
local style = params.style or {padding ='5px 0'}
local class = "images"
if #images == 2 then
class = "image2"
end
-- Partie image
local imagesString = for i,image in pairs(images) do imagesString = imagesString .. 'upright=' .. ( 1 / #images ) end imagesString = imagesString .. ''
end
local image = mw.html.create('div') :css(style) :addClass("images") :wikitext(imagesString)
-- Partie légende local caption = mw.html.create('p') if captiontext then caption :wikitext(captiontext) :css(params.style or {}) :addClass("legend") :done() end
return image, caption end
local function buildtext(params) local class = params.class or local style = params.style or {} if not style['background-color'] then style['background-color'] = maincolor end local text = localdata[params.value] or getWikidataValue(params) or params.defaulttext
if not text then return nil -- à ajouter : option cat de maintenance end local formattedtext = mw.html.create('p') :addClass(class) :css(style) :wikitext(text) :done() return formattedtext end
local function buildmixedrow(params) local class = params.class or local style = params.style or {} local value
-- retrieve value if type(params.value) == 'function' then local raw = params.value value = raw(localdata) elseif type(params.value) == 'string' then value = localdata[params.value] end
if not value then value = getWikidataValue(params) or params.defaultvalue end
local label = params.label if not value then return nil -- todo: maintenance end
-- formatting local formattedvalue = mw.html.create('div') :wikitext(value)
if (params.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) :allDone()
local formattedlabel = mw.html.create('th') :attr('scope', 'row') :wikitext(label) :done()
local row = mw.html.create('tr') :addClass(class) :css(style) :node(formattedlabel) :node(formattedvalue) :done()
return row end
local function buildtable(params) local tab = mw.html.create('table') local title if params.title then local text if type(params.title) == 'string' then -- raccourci moche mais pratique : n'utiliser qu'une chaîne pour le titre text = params.title else text = params.title.value or error('no value provided for this title') params = {} end local style = params.title.style or {['text-align'] = 'center', color = '000000'} if not style['background-color'] then style['background-color'] = maincolor end local colspan = params.title.colspan or '2' title = mw.html.create('th') :attr({colspan = 2}) :css(style) :wikitext(text) :done() end local rows = {} -- does not add the rows directly to tab: check if some rows are non empty beoforehand so that we do not add a title if there are no data to show for k, l in pairs(params.rows) do if type(l) == 'table' and l.type == 'mixed' then local row = buildmixedrow(l) if row then table.insert(rows, row) end end end if title and (#rows > 0) then tab:node(title) end if #rows > 0 then for i, j in pairs (rows) do tab:node(j) end end tab:allDone() return tab end
local function buildmap(params) -- A faire local image = 'France_location_map-Regions_and_departements.svg' local image2 = 'France_location_map-Regions_and_departements.svg'
local map1 = mw.html.create('div') :addClass("geobox") :tag('table') :addClass('DebutCarte') :attr({border="0", cellspacing="0", cellpadding="0"}) :css({margin = '0', border = 'none', padding = '0', ['text-align'] = 'center'}) :tag('tr') :tag('td') :tag('div') :css({position= 'relative', margin = "auto"}) :wikitext('(Voir situation sur carte : [[{{Géolocalisation/{{{1}}}') :done() :done() :done() :done() local map1 = mw.html.create('div') :addClass("geobox") :tag('table') :addClass('DebutCarte') :attr({border="0", cellspacing="0", cellpadding="0"}) :css({margin = '0', border = 'none', padding = '0', ['text-align'] = 'center'}) :tag('tr') :tag('td') :tag('div') :css({position= 'relative', margin = "auto"}) :wikitext('{{{alt}}}') :done() :done() :done() :done() local div = mw.html.create('div') :addClass("img_toogle") :node(map1):done() :node(map2):done() :allDone() return div end
local function buildfooter(params) if not params then params = {} end
local class = 'navbar noprint ' .. (params.class or ) local style = params.style or {} if not style['border-top-style'] then style['border-top-style'] = 'solid' end if not style['border-width'] then style['border-width'] = '1px' end if not style['border-color'] then style['border-color'] = maincolor end if not style['font-size'] then style['font-size'] = '80%' end local backlinktext = '[' .. tostring( mw.uri.fullUrl( pagename, '&action=edit§ion=0' ) ) .. ' ' .. i18n['edit infobox'] .. ']' local backlink = mw.html.create('span') :wikitext(backlinktext) :addClass('plainlinks') :done()
local itemlinktext = if item then itemlinktext = '' .. i18n['edit item'] .. '' end local itemlink = mw.html.create('span') :wikitext(itemlinktext) :done()
local doclinktext = '
'
local doclink = mw.html.create('span')
:css({['text-align'] = "right"})
:wikitext(doclinktext)
:done()
local footer = mw.html.create('p') :addClass(class) :css(style) :node(backlink) :node(itemlink) :node(doclink) return footer end
function p._build(localinfoboxmodule) -- analyse le module d'infobox local style = localinfoboxmodule.style or {} local class = localinfoboxmodule.class or "infobox_v3" local parts = localinfoboxmodule.parts maincolor = localinfoboxmodule.maincolor or "#E1E1E1" local footerparams = localinfoboxmodule.footer or {} infobox:css(style) infobox:addClass(class)
for i, j in pairs(parts) do if type(j) ~= 'table' then return 'error' elseif j.type == 'title' then infobox:node(buildtitle(j)) elseif j.type == 'images' then local image, caption = buildimages(j) infobox:node(image):node(caption) elseif j.type == 'text' then infobox:node(buildtext(j)) elseif j.type == 'map' then infobox:node(buildmap(j)) elseif j.type == 'navbox' then infobox:node(buildnavbox(j)) elseif j.type == 'table' then infobox:node(buildtable(j)) end end local div = mw.html.create('div') infobox:node(div):node(tab) infobox :node(buildfooter(footerparams)) :done() return tostring(infobox) .. maintenance end
function p.build(frame) -- set variables infoboxname = 'Module:Infobox/' .. frame.args.nom localinfoboxmodule = require(infoboxname)
-- get the localinfoboxmodule parameter values and clen them up for i, j in pairs(frame:getParent().args) do if j and mw.text.trim(j) ~= then localdata[i] = j end end -- fetch wikidata item and assign it to the item global variable if localdata.wikidata == 'non' then item = nil elseif (not localdata.wikidata) or (localdata.wikidata == ) then item = mw.wikibase.getEntityObject() else item = mw.wikibase.getEntityObject() -- todo: fetch arbitrary item and check for errors end -- build infobox return p._build(localinfoboxmodule) end
return p