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

De Lagny-sur-Marne Wiki
Aller à la navigation Aller à la recherche
Aucun résumé des modifications
0x010D (discussion | contributions)
Annulation des modifications 95377631 de 109.151.161.64 (d): casse Module:Infobox Builder
Ligne 1 : Ligne 1 :
--
local Infobox = {}
-- This module implements {{Infobox}}
 
--
function Infobox:new( args )
    --Object initialisation
local p = {}
    local object = {
        text = "",
local HtmlBuilder = require('Module:HtmlBuilder')
        isPart = false,
        entity = nil --Lazy loading of the related Wikidata item
local args = {}
     }
local origArgs
     setmetatable(object, {
local root
        __index = Infobox,
        __tostring = function( self ) return self:tostring() end
function union(t1, t2)
    })
    -- Returns the union of the values of two tables, as a sequence.
 
     local vals = {}
    if args.isPart then
     for k, v in pairs(t1) do
         object.isPart = true
         vals[v] = true
    end
 
    --Open main div
    if object.isPart then
        return object --On retourne si on construit seulement un module
     end
     end
     for k, v in pairs(t2) do
 
         vals[v] = true
    local str = '<div class="infobox_v3 '
     if args.class then
         str = str .. args.class
     end
     end
     local ret = {}
     str = str ..'"'
     for k, v in pairs(vals) do
     if args.width then
         table.insert(ret, k)
         str = str .. ' style="width: ' .. args.width .. 'em"'
     end
     end
     return ret
    object.text = str .. '>'
     return object
end
end
 
local function getArgNums(prefix)
function Infobox:addText( args )
     -- Returns a table containing the numbers of the arguments that exist
     self.text = self.text .. args.text
    -- for the specified prefix. For example, if the prefix was 'data', and
end
    -- 'data1', 'data2', and 'data5' exist, it would return {1, 2, 5}.
 
    local nums = {}
function Infobox:getEntity()
    for k, v in pairs(args) do
    if self.entity == nil then
        local num = tostring(k):match('^' .. prefix .. '([1-9]%d*)$')
        self.entity = mw.wikibase.getEntity()
        if num then table.insert(nums, tonumber(num)) end
     end
     end
     table.sort(nums)
     return self.entity
    return nums
end
end
 
local function addRow(rowArgs)
function Infobox:addTitle( args )
     -- Adds a row to the infobox, with either a header cell
     local str = '<p class="entete'
    -- or a label/data cell combination.
     if args.icon then
     if rowArgs.header then
         str = str .. ' icon ' .. args.icon
         root
    end
            .tag('tr')
    if args.class then
                .tag('th')
        str = str .. ' ' .. args.class
                    .attr('colspan', 2)
    end
                    .addClass(rowArgs.class)
    str = str .. '"'
                    .css('text-align', 'center')
     if args.background then
                    .cssText(args.headerstyle)
         local style = {}
                    .wikitext(rowArgs.header)
        style['background-color'] = args.background
     elseif rowArgs.data then
         if args['border-color'] then
         local row = root.tag('tr')
             style['border-color'] = args['border-color']
        row.addClass(rowArgs.rowclass)
        else
         if rowArgs.label then
            style['border-color'] = args.background
             row
                .tag('th')
                    .attr('scope', 'row')
                    .css('text-align', 'left')
                    .cssText(args.labelstyle)
                    .wikitext(rowArgs.label)
                    .done()
         end
         end
         if args.color then
        local dataCell = row.tag('td')
             style['color'] = args.color
         if not rowArgs.label then  
             dataCell
                .attr('colspan', 2)
                .css('text-align', 'center')
         end
         end
         dataCell
         str = str .. ' style="' .. formatStyle( style ) .. '"'
            .addClass(rowArgs.class)
    end
            .cssText(rowArgs.datastyle)
    str = str .. '>'
            .newline()
    if args.text and args.text ~= '' then
            .wikitext(rowArgs.data)
        str = str .. args.text
    else
        str = str .. mw.title.getCurrentTitle().text
     end
     end
    self.text = self.text .. str .. '</p>'
end
end
 
local function renderTitle()
function Infobox:openTable( args )
     if not args.title then return end
     local str = '<table'
     str = str .. '><caption'
     root
     if args.class then
        .tag('caption')
         str = str .. ' class="' .. args.class .. '"'
            .addClass(args.titleclass)
    end
            .cssText(args.titlestyle)
 
            .wikitext(args.title)
    --Style of the caption
end
    local style = {}
     if args.border then
local function renderAboveRow()
         style['border-color'] = args.border
     if not args.above then return end
    end
    if args.background then
    root
        style['background'] = args.background
         .tag('tr')
        if args.color then
            .tag('th')
            style['color'] = args.color
                .attr('colspan', 2)
        end
                .addClass(args.aboveclass)
                .css('text-align', 'center')
                .css('font-size', '125%')
                .css('font-weight', 'bold')
                .cssText(args.abovestyle)
                .wikitext(args.above)
end
local function renderBelowRow()
     if not args.below then return end
    root
         .tag('tr')
            .tag('td')
                .attr('colspan', '2')
                .addClass(args.belowclass)
                .css('text-align', 'center')
                .cssText(args.belowstyle)
                .newline()
                .wikitext(args.below)
end
local function renderSubheaders()
    if args.subheader then
        args.subheader1 = args.subheader
     end
     end
     if args.subheaderrowclass then
     if next( style ) then
         args.subheaderrowclass1 = args.subheaderrowclass
         str = str .. ' style="' .. formatStyle( style ) .. '"'
     end
     end
     local subheadernums = getArgNums('subheader')
 
     for k, num in ipairs(subheadernums) do
     str = str .. '>'
        addRow({
     if args.text and args.text ~= '' then
            data = args['subheader' .. tostring(num)],
        str = str .. args.text
            datastyle = args.subheaderstyle or args['subheaderstyle' .. tostring(num)],
    else
            class = args.subheaderclass,
        str = str .. 'Données clés'
            rowclass = args['subheaderrowclass' .. tostring(num)]
        })
     end
     end
    self.text = self.text .. str .. '</caption>'
end
function Infobox:closeTable( args )
    self.text = self.text .. '</table>'
end
end
 
local function renderImages()
function Infobox:addMixedRow( args )
     if args.image then
 
         args.image1 = args.image
    --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.caption then
 
         args.caption1 = args.caption
     if not args.value or args.value == '' then
         return
     end
     end
     local imagenums = getArgNums('image')
     if not args.label then
    for k, num in ipairs(imagenums) do
         self.text = self.text .. '<tr><td class="error">Le paramètre label n\'est pas renseigné.</td></tr>'
        local caption = args['caption' .. tostring(num)]
         return
         local data = HtmlBuilder.create().wikitext(args['image' .. tostring(num)])
        if caption then
            data
                .tag('br', {selfClosing = true})
                    .done()
                .tag('div')
                    .cssText(args.captionstyle)
                    .wikitext(caption)
        end
        addRow({
            data = tostring(data),
            datastyle = args.imagestyle,
            class = args.imageclass,
            rowclass = args['imagerowclass' .. tostring(num)]
         })
     end
     end
end
     local str = '<tr><th scope="row"'
     if args.class then
local function renderRows()
         str = str .. ' class="' .. args.class .. '"'
    -- Gets the union of the header and data argument numbers,
    -- and renders them all in order using addRow.
     local rownums = union(getArgNums('header'), getArgNums('data'))
     table.sort(rownums)
    for k, num in ipairs(rownums) do
         addRow({
            header = args['header' .. tostring(num)],
            label = args['label' .. tostring(num)],
            data = args['data' .. tostring(num)],
            datastyle = args.datastyle,
            class = args['class' .. tostring(num)],
            rowclass = args['rowclass' .. tostring(num)]
        })
     end
     end
end
 
    local style = {}
local function renderNavBar()
     if args.width then
     if not args.name then return end
         style['width'] =  args.width .. 'em'
    root
         .tag('tr')
            .tag('td')
                .attr('colspan', '2')
                .css('text-align', 'right')
                .wikitext(mw.getCurrentFrame():expandTemplate({
                    title = 'navbar',
                    args = { args.name, mini = 1 }
                }))
end
   
local function renderItalicTitle()
    local italicTitle = args['italic title'] and mw.ustring.lower(args['italic title'])
    if italicTitle == '' or italicTitle == 'force' or italicTitle == 'yes' then
        root.wikitext(mw.getCurrentFrame():expandTemplate({title = 'italic title'}))
     end
     end
end
    if next( style ) then
         str = str .. ' style="' .. formatStyle( style ) .. '"'
local function renderTrackingCategories()
    if args.decat ~= 'yes' then
         if #(getArgNums('data')) == 0 and mw.title.getCurrentTitle().namespace == 0 then
            root.wikitext('[[Category:Articles which use infobox templates with no data rows]]')
        end
        if args.child == 'yes' and args.title then
            root.wikitext('[[Category:Articles which use embedded infobox templates with the title parameter]]')
        end
     end
     end
    self.text = self.text .. str .. '>' .. args.label .. '</th><td>' .. args.value .. '</td></tr>'
end
end
 
local function _infobox()
function Infobox:addImages( args )
     -- Specify the overall layout of the infobox, with special settings
     --Get images
     -- if the infobox is used as a 'child' inside another infobox.
     local images = {}
     if args.child ~= 'yes' then
     if not args.images then
         root = HtmlBuilder.create('table')
         return
        root
            .addClass('infobox')
            .addClass(args.bodyclass)
            .attr('cellspacing', 3)
            .css('border-spacing', '3px')
            if args.subbox == 'yes' then
                root
                    .css('padding', '0')
                    .css('border', 'none')
                    .css('margin', '-3px')
                    .css('width', 'auto')
                    .css('min-width', '100%')
                    .css('font-size', '100%')
                    .css('clear', 'none')
                    .css('float', 'none')
                    .css('background-color', 'transparent')
            else
                root
                    .css('width', '22em')
            end
        root
            .cssText(args.bodystyle)
        renderTitle()
        renderAboveRow()
    else
        root = HtmlBuilder.create()
        root
            .wikitext(args.title)
     end
     end
     for i,conf in pairs( args.images ) do
     renderSubheaders()
        if conf.name and conf.name ~= '' then
    renderImages()
            table.insert( images, conf )
    renderRows()
         end
    renderBelowRow() 
    renderNavBar()
    renderItalicTitle()
    renderTrackingCategories()
    return tostring(root)
end
local function preprocessSingleArg(argName)
    -- If the argument exists and isn't blank, add it to the argument table.
    -- Blank arguments are treated as nil to match the behaviour of ParserFunctions.
    if origArgs[argName] and origArgs[argName] ~= '' then
         args[argName] = origArgs[argName]
     end
     end
end
 
     if not next( images ) then
local function preprocessArgs(prefixTable, step)
         return --Pas d'images
    -- Assign the parameters with the given prefixes to the args table, in order, in batches
    -- of the step size specified. This is to prevent references etc. from appearing in the
    -- wrong order. The prefixTable should be an array containing tables, each of which has
    -- two possible fields, a "prefix" string and a "depend" table. The function always parses
    -- parameters containing the "prefix" string, but only parses parameters in the "depend"
    -- table if the prefix parameter is present and non-blank.
     if type(prefixTable) ~= 'table' then
         error("Non-table value detected for the prefix table", 2)
     end
     end
     if type(step) ~= 'number' then
     local imagesCount = table.maxn( images )
         error("Invalid step value detected", 2)
 
    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
 
    -- Get arguments without a number suffix, and check for bad input.
     for i,image in pairs( images ) do
     for i,v in ipairs(prefixTable) do
        str = str .. '[[Fichier:' .. image.name .. '|thumb'
         if type(v) ~= 'table' or type(v.prefix) ~= "string" or (v.depend and type(v.depend) ~= 'table') then
         if imagesCount == 1 then
             error('Invalid input detected to preprocessArgs prefix table', 2)
            str = str .. '|center'
        end
        if image.upright then
            str = str .. '|upright=' .. image.upright
        else
             str = str .. '|upright=' .. ( 1 / imagesCount )
         end
         end
         preprocessSingleArg(v.prefix)
         if image.alt then
        -- Only parse the depend parameter if the prefix parameter is present and not blank.
            str = str .. '|alt=' .. image.alt
         if args[v.prefix] and v.depend then
         else
             for j, dependValue in ipairs(v.depend) do
            str = str .. '|alt=Description de '
                 if type(dependValue) ~= 'string' then
             if args.legend then
                    error('Invalid "depend" parameter value detected in preprocessArgs')
                 str = str .. 'cette image, également commentée ci-après'
                end
            else
                preprocessSingleArg(dependValue)
                str = str .. 'l\'image ' .. image.name
             end
             end
         end
         end
        str = str .. ']]'
     end
     end
 
     -- Get arguments with number suffixes.
     if imagesCount == 2 then
    local a = 1 -- Counter variable.
        str = str .. '</div>'
     local moreArgumentsExist = true
     end
     while moreArgumentsExist == true do
 
        moreArgumentsExist = false
     if args.legend then
         for i = a, a + step - 1 do
         str = str .. '<p class="legend">' .. args.legend .. '</p>'
            for j,v in ipairs(prefixTable) do
                local prefixArgName = v.prefix .. tostring(i)
                if origArgs[prefixArgName] then
                    moreArgumentsExist = true -- Do another loop if any arguments are found, even blank ones.
                    preprocessSingleArg(prefixArgName)
                end
                -- Process the depend table if the prefix argument is present and not blank, or
                -- we are processing "prefix1" and "prefix" is present and not blank, and
                -- if the depend table is present.
                if v.depend and (args[prefixArgName] or (i == 1 and args[v.prefix])) then
                    for j,dependValue in ipairs(v.depend) do
                        local dependArgName = dependValue .. tostring(i)
                        preprocessSingleArg(dependArgName)
                    end
                end
            end
        end
        a = a + step
     end
     end
   
    self.text = self.text .. str
end
end
 
function p.infobox(frame)
function Infobox:tostring()
    -- If called via #invoke, use the args passed into the invoking template.
     if self.isPart then
    -- Otherwise, for testing purposes, assume args are being passed directly in.
         return self.text
     if frame == mw.getCurrentFrame() then
         origArgs = frame:getParent().args
     else
     else
         origArgs = frame
         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, '; ' )
     -- Parse the data parameters in the same order that the old {{infobox}} did, so that
end
    -- references etc. will display in the expected places. Parameters that depend on
 
    -- another parameter are only processed if that parameter is present, to avoid
local p = {}
    -- phantom references appearing in article reference lists.
function p.new( args )
    preprocessSingleArg('child')
     return Infobox:new( args )
    preprocessSingleArg('bodyclass')
end
    preprocessSingleArg('subbox')
function p.test()
    preprocessSingleArg('bodystyle')
     local a = Infobox:new( {} )
    preprocessSingleArg('title')
     a:addTitle( {} )
     preprocessSingleArg('titleclass')
     a:addImages( {
    preprocessSingleArg('titlestyle')
         images = {
    preprocessSingleArg('above')
            {
     preprocessSingleArg('aboveclass')
                name = 'Pellicule.jpg'
     preprocessSingleArg('abovestyle')
            },
     preprocessArgs({
            {
         {prefix = 'subheader', depend = {'subheaderstyle', 'subheaderrowclass'}}
                name = 'Pellicule.jpg'
    }, 10)
            }
    preprocessSingleArg('subheaderstyle')
         },
    preprocessSingleArg('subheaderclass')
         legend = 'legend'
    preprocessArgs({
     } )
        {prefix = 'image', depend = {'caption', 'imagerowclass'}}
     a:openTable( {} )
    }, 10)
     a:addMixedRow( {
    preprocessSingleArg('captionstyle')
        label = 'Test',
    preprocessSingleArg('imagestyle')
        value = 'test'
    preprocessSingleArg('imageclass')
     } )
    preprocessArgs({
     a:closeTable( {} )
        {prefix = 'header'},
     return tostring(a)
         {prefix = 'data', depend = {'label', 'rowclass'}},
         {prefix = 'class'}
     }, 50)
     preprocessSingleArg('headerstyle')
     preprocessSingleArg('labelstyle')
    preprocessSingleArg('datastyle')
    preprocessSingleArg('below')
     preprocessSingleArg('belowclass')
     preprocessSingleArg('belowstyle')
    preprocessSingleArg('name')
    args['italic title'] = origArgs['italic title'] -- different behaviour if blank or absent
    preprocessSingleArg('decat')
     return _infobox()
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'
   end
self.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 then
self.text = self.text .. 'Le paramètre label n\'est pas renseigné.'
       return
   end
local 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 then
str = ''
   end
   if args.legend then
str = str .. '

' .. args.legend .. '

'
   end
   
   self.text = self.text .. str

end

function Infobox:tostring()

   if self.isPart then
       return self.text
   else
return 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