Aller au contenu

« Module:Infobox » : différence entre les versions

De Wreck
Modèle:Infobox>Tpt
Nouvelle page : local Infobox = {} function Infobox:new( args ) --Object initialisation local object = { text = "", title = mw.title.getCurrentTitle() } setmetata...
 
Modèle:Infobox>Tpt
m + table and mixed rows
Ligne 48 : Ligne 48 :
     end
     end
     str = str .. '>'
     str = str .. '>'
     if args.text then
     if args.text and args.text ~= '' then
         str = str .. args.text
         str = str .. args.text
     else
     else
Ligne 54 : Ligne 54 :
     end
     end
     self.text = self.text .. str .. '</p>'
     self.text = self.text .. str .. '</p>'
end
function Infobox:openTable( args )
    local str = '<table'
    if args.type then
        str = str .. ' class="' .. args.type .. '"'
    end
    str = str .. '><caption'
    if 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 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'
    end
    self.text = self.text .. str .. '</caption>'
end
function Infobox:closeTable( args )
    self.text = self.text .. '</table>'
end
function Infobox:addMixedRow( args )
    if not args.value or args.value == '' then
        return
    end
    if not args.label then
        self.text = self.text .. '<tr><td class="error">Le paramètre label n\'est pas renseigné.</td></tr>'
    end
    local str = '<tr><th scope="row"'
    if args.class then
        str = str .. ' class="' .. args.class .. '"'
    end
    local style = {}
    if args.width then
        style['width'] =  args.width .. 'em'
    end
    if style ~= {} then
        str = str .. ' style="' .. formatStyle( style ) .. '"'
    end
    self.text = self.text .. str .. '>' .. args.label .. '</th><td>' .. args.value .. '</td></tr>'
end
end


Ligne 63 : Ligne 125 :
function formatStyle( args )
function formatStyle( args )
     local elems = {}
     local elems = {}
     for args = key, val do
     for key, val in pairs( args ) do
         elems.add( key .. ':' .. val )
         elems.add( key .. ':' .. val )
     end
     end
Ligne 76 : Ligne 138 :
     local a = Infobox:new( {} )
     local a = Infobox:new( {} )
     a:addTitle( {} )
     a:addTitle( {} )
     return a
    a:openTable( {} )
    a:addMixedRow( {
        label = 'Test',
        value = 'test'
    } )
    a:closeTable( {} )
     return tostring(a)
end
end
return p
return p

Version du 1 avril 2013 à 14:43

La documentation pour ce module peut être créée à Module:Infobox/doc

local Infobox = {}

function Infobox:new( args )
    --Object initialisation
    local object = {
        text = "",
        title = mw.title.getCurrentTitle()
    }
    setmetatable(object, {
        __index = Infobox,
        __tostring = function( self ) return self:tostring() end
    })

    --Open main div
    local str = '<div class="infobox_v3 '
    if args.class then
        str = str .. args.class
    end
    str = str ..'"'
    if args.width then
        str = str .. ' style="width: ' .. args.width .. 'em"'
    end
    object.text = str .. '>'
    return object
end

function Infobox:addTitle( args )
    local str = '<p class="entete '
    if args.icon then
        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
            style['border-color'] = args.border-color
        else
            style['border-color'] = args.background
        end
        if args.color then
            style['color'] = args.color
        end
        str = str .. 'style="' .. formatStyle( style ) .. '"'
    end
    str = str .. '>'
    if args.text and args.text ~= '' then
        str = str .. args.text
    else
        str = str .. self.title.text
    end
    self.text = self.text .. str .. '</p>'
end

function Infobox:openTable( args )
    local str = '<table'
    if args.type then 
        str = str .. ' class="' .. args.type .. '"'
    end
    str = str .. '><caption'
    if 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 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'
    end

    self.text = self.text .. str .. '</caption>'
end

function Infobox:closeTable( args )
    self.text = self.text .. '</table>'
end

function Infobox:addMixedRow( args )
    if not args.value or args.value == '' then
        return
    end
    if not args.label then
        self.text = self.text .. '<tr><td class="error">Le paramètre label n\'est pas renseigné.</td></tr>'
    end
    local str = '<tr><th scope="row"'
    if args.class then
        str = str .. ' class="' .. args.class .. '"'
    end

    local style = {}
    if args.width then
        style['width'] =  args.width .. 'em'
    end
    if style ~= {} then
        str = str .. ' style="' .. formatStyle( style ) .. '"'
    end

    self.text = self.text .. str .. '>' .. args.label .. '</th><td>' .. args.value .. '</td></tr>'
end

function Infobox:tostring()
    return self.text .. '</div>'
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
        elems.add( 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:openTable( {} )
    a:addMixedRow( {
        label = 'Test',
        value = 'test'
    } )
    a:closeTable( {} )
    return tostring(a)
end
return p