« Module:Infobox » : différence entre les versions
Aller à la navigation
Aller à la recherche
Aucun résumé des modifications |
Annulation des modifications 95377631 de 109.151.161.64 (d): casse Module:Infobox Builder |
||
Ligne 1 : | Ligne 1 : | ||
local Infobox = {} | |||
function Infobox:new( args ) | |||
--Object initialisation | |||
local object = { | |||
text = "", | |||
isPart = false, | |||
entity = nil --Lazy loading of the related Wikidata item | |||
local | } | ||
setmetatable(object, { | |||
__index = Infobox, | |||
__tostring = function( self ) return self:tostring() end | |||
}) | |||
if args.isPart then | |||
object.isPart = true | |||
end | |||
--Open main div | |||
if object.isPart then | |||
return object --On retourne si on construit seulement un module | |||
end | end | ||
local str = '<div class="infobox_v3 ' | |||
if args.class then | |||
str = str .. args.class | |||
end | end | ||
str = str ..'"' | |||
if args.width then | |||
str = str .. ' style="width: ' .. args.width .. 'em"' | |||
end | end | ||
return | object.text = str .. '>' | ||
return object | |||
end | end | ||
function Infobox:addText( args ) | |||
self.text = self.text .. args.text | |||
end | |||
function Infobox:getEntity() | |||
if self.entity == nil then | |||
self.entity = mw.wikibase.getEntity() | |||
end | end | ||
return self.entity | |||
end | end | ||
function Infobox:addTitle( args ) | |||
local str = '<p class="entete' | |||
if args.icon then | |||
if | str = str .. ' icon ' .. args.icon | ||
end | |||
if args.class then | |||
str = str .. ' ' .. args.class | |||
end | |||
str = str .. '"' | |||
if args.background then | |||
local style = {} | |||
style['background-color'] = args.background | |||
if args['border-color'] then | |||
local | style['border-color'] = args['border-color'] | ||
else | |||
if | style['border-color'] = args.background | ||
end | end | ||
if args.color then | |||
style['color'] = args.color | |||
if | |||
end | end | ||
str = str .. ' style="' .. formatStyle( style ) .. '"' | |||
end | |||
str = str .. '>' | |||
if args.text and args.text ~= '' then | |||
str = str .. args.text | |||
else | |||
str = str .. mw.title.getCurrentTitle().text | |||
end | end | ||
self.text = self.text .. str .. '</p>' | |||
end | end | ||
function Infobox:openTable( args ) | |||
local str = '<table' | |||
str = str .. '><caption' | |||
if args.class then | |||
str = str .. ' class="' .. args.class .. '"' | |||
end | |||
--Style of the caption | |||
local style = {} | |||
if args.border then | |||
style['border-color'] = args.border | |||
if | end | ||
if args.background then | |||
style['background'] = args.background | |||
. | if args.color then | ||
style['color'] = args.color | |||
end | |||
local | |||
if | |||
end | end | ||
if | if next( style ) then | ||
str = str .. ' style="' .. formatStyle( style ) .. '"' | |||
end | end | ||
str = str .. '>' | |||
if args.text and args.text ~= '' then | |||
str = str .. args.text | |||
else | |||
str = str .. 'Données clés' | |||
end | end | ||
self.text = self.text .. str .. '</caption>' | |||
end | |||
function Infobox:closeTable( args ) | |||
self.text = self.text .. '</table>' | |||
end | end | ||
function Infobox:addMixedRow( args ) | |||
if args. | |||
args. | --Get value from Wikidata if needed | ||
if args.property and ( not args.value or args.value == '' ) then | |||
local entity = self:getEntity() | |||
if entity then | |||
local Wikidata = require 'Module:Wikidata' | |||
args.value = Wikidata.formatStatementsFromLua( { | |||
entity = entity, | |||
property = args.property, | |||
rank = args.rank | |||
} ) | |||
end | |||
end | end | ||
if args. | |||
if not args.value or args.value == '' then | |||
return | |||
end | end | ||
if not args.label then | |||
self.text = self.text .. '<tr><td class="error">Le paramètre label n\'est pas renseigné.</td></tr>' | |||
return | |||
end | end | ||
local str = '<tr><th scope="row"' | |||
if args.class then | |||
str = str .. ' class="' .. args.class .. '"' | |||
local | |||
end | end | ||
local style = {} | |||
local | if args.width then | ||
if | style['width'] = args.width .. 'em' | ||
end | end | ||
if next( style ) then | |||
str = str .. ' style="' .. formatStyle( style ) .. '"' | |||
end | end | ||
self.text = self.text .. str .. '>' .. args.label .. '</th><td>' .. args.value .. '</td></tr>' | |||
end | end | ||
function Infobox:addImages( args ) | |||
-- | --Get images | ||
local images = {} | |||
if args. | if not args.images then | ||
return | |||
end | end | ||
for i,conf in pairs( args.images ) do | |||
if conf.name and conf.name ~= '' then | |||
table.insert( images, conf ) | |||
end | |||
end | end | ||
if not next( images ) then | |||
return --Pas d'images | |||
if | |||
end | end | ||
local imagesCount = table.maxn( images ) | |||
local str = '' | |||
if imagesCount == 2 then | |||
str = '<div class="image2"' | |||
if args.background then | |||
str = str .. ' style="background: ' .. args.background .. ';"' | |||
end | |||
str = str .. '>' | |||
end | end | ||
for i,image in pairs( images ) do | |||
for i, | str = str .. '[[Fichier:' .. image.name .. '|thumb' | ||
if | if imagesCount == 1 then | ||
str = str .. '|center' | |||
end | |||
if image.upright then | |||
str = str .. '|upright=' .. image.upright | |||
else | |||
str = str .. '|upright=' .. ( 1 / imagesCount ) | |||
end | end | ||
if image.alt then | |||
str = str .. '|alt=' .. image.alt | |||
else | |||
str = str .. '|alt=Description de ' | |||
if args.legend then | |||
str = str .. 'cette image, également commentée ci-après' | |||
else | |||
str = str .. 'l\'image ' .. image.name | |||
end | end | ||
end | end | ||
str = str .. ']]' | |||
end | end | ||
if imagesCount == 2 then | |||
str = str .. '</div>' | |||
end | |||
if args.legend then | |||
str = str .. '<p class="legend">' .. args.legend .. '</p>' | |||
end | end | ||
self.text = self.text .. str | |||
end | end | ||
function | function Infobox:tostring() | ||
if self.isPart then | |||
return self.text | |||
if | |||
else | else | ||
return self.text .. '</div>' | |||
end | |||
end | |||
--Create a style property value from an array CSS property = CSS value | |||
function formatStyle( args ) | |||
local elems = {} | |||
for key, val in pairs( args ) do | |||
table.insert( elems, key .. ':' .. val ) | |||
end | end | ||
return table.concat( elems, '; ' ) | |||
end | |||
local p = {} | |||
function p.new( args ) | |||
return Infobox:new( args ) | |||
end | |||
function p.test() | |||
local a = Infobox:new( {} ) | |||
a:addTitle( {} ) | |||
a:addImages( { | |||
images = { | |||
{ | |||
name = 'Pellicule.jpg' | |||
}, | |||
{ | |||
name = 'Pellicule.jpg' | |||
} | |||
}, | |||
legend = 'legend' | |||
} ) | |||
a:openTable( {} ) | |||
a:addMixedRow( { | |||
label = 'Test', | |||
value = 'test' | |||
} ) | |||
a:closeTable( {} ) | |||
return tostring(a) | |||
} | |||
return | |||
end | end | ||
return p | return p |
Version du 29 juillet 2013 à 20:41
local Infobox = {}
function Infobox:new( args )
--Object initialisation local object = { text = "", isPart = false, entity = nil --Lazy loading of the related Wikidata item } setmetatable(object, { __index = Infobox, __tostring = function( self ) return self:tostring() end })
if args.isPart then object.isPart = true end
--Open main div if object.isPart then return object --On retourne si on construit seulement un module end
local str = '
'
return object
end
function Infobox:addText( args )
self.text = self.text .. args.text
end
function Infobox:getEntity()
if self.entity == nil then self.entity = mw.wikibase.getEntity() end return self.entity
end
function Infobox:addTitle( args )
local str = '' if args.text and args.text ~= then str = str .. args.text else str = str .. mw.title.getCurrentTitle().text end self.text = self.text .. str .. '
'end
function Infobox:openTable( args )
local str = '<table' str = str .. '><caption' if args.class then str = str .. ' class="' .. args.class .. '"' end
--Style of the caption local style = {} if args.border then style['border-color'] = args.border end if args.background then style['background'] = args.background if args.color then style['color'] = args.color end end if next( style ) then str = str .. ' style="' .. formatStyle( style ) .. '"' end
str = str .. '>' if args.text and args.text ~= then str = str .. args.text else str = str .. 'Données clés' endself.text = self.text .. str .. '' end function Infobox:closeTable( args ) self.text = self.text .. ''
end
function Infobox:addMixedRow( args )
--Get value from Wikidata if needed if args.property and ( not args.value or args.value == ) then local entity = self:getEntity() if entity then local Wikidata = require 'Module:Wikidata' args.value = Wikidata.formatStatementsFromLua( { entity = entity, property = args.property, rank = args.rank } ) end end
if not args.value or args.value == then return end if not args.label thenself.text = self.text .. 'Le paramètre label n\'est pas renseigné.'
return endlocal str = '' .. args.label .. '' .. args.value .. ''
end
function Infobox:addImages( args )
--Get images local images = {} if not args.images then return end for i,conf in pairs( args.images ) do if conf.name and conf.name ~= then table.insert( images, conf ) end end
if not next( images ) then return --Pas d'images end local imagesCount = table.maxn( images )
local str = if imagesCount == 2 thenstr = '
'
'
end
for i,image in pairs( images ) do
str = str .. 'alt=Description de ' if args.legend then str = str .. 'cette image, également commentée ci-après' else str = str .. 'l\'image ' .. image.name end end str = str .. ''
end
if imagesCount == 2 thenstr = str .. '
end
if args.legend thenstr = str .. '
' .. args.legend .. '
'end self.text = self.text .. str
end
function Infobox:tostring()
if self.isPart then return self.text elsereturn self.text .. '
'
end
end
--Create a style property value from an array CSS property = CSS value function formatStyle( args )
local elems = {} for key, val in pairs( args ) do table.insert( elems, key .. ':' .. val ) end return table.concat( elems, '; ' )
end
local p = {} function p.new( args )
return Infobox:new( args )
end function p.test()
local a = Infobox:new( {} ) a:addTitle( {} ) a:addImages( { images = { { name = 'Pellicule.jpg' }, { name = 'Pellicule.jpg' } }, legend = 'legend' } ) a:openTable( {} ) a:addMixedRow( { label = 'Test', value = 'test' } ) a:closeTable( {} ) return tostring(a)
end return p