« Module:Liste éléments » : différence entre les versions

De Lagny-sur-Marne Wiki
Aller à la navigation Aller à la recherche
0x010D (discussion | contributions)
optim accès global dans loop, mef code
0x010D (discussion | contributions)
helper getParam() pour obtenir un paramètre s'il existe et est non vide, sinon retourne nil ou fallback optionnel ; noter la dépendance à variable locale "args"
Ligne 5 : Ligne 5 :
     local args = frame:getParent().args
     local args = frame:getParent().args
     local trimFunc = mw.text.trim -- cache accès global
     local trimFunc = mw.text.trim -- cache accès global
    function getParam(name, default)
        if args[name] ~= nil and args[name] ~= '' then
            return args[name]
        else
            return default -- nil si non spécifié
        end
    end


     local paramSep, espaces, glue
     local paramSep, espaces, glue
     local values = {}
     local values = {}


     if args['séparateur'] ~= nil and args['séparateur'] ~= '' then
     paramSep = getParam('séparateur', getParam('sép', '·'))
        paramSep = args['séparateur']
     espaces = tonumber(getParam('espaces', '1'))
    elseif args['sép'] ~= nil and args['sép'] ~= '' then
        paramSep = args['sép']
    else
        paramSep = '·'
     end
 
    if args['espaces'] ~= nil and args['espaces'] ~= '' then
        espaces = tonumber(args['espaces'])
    else
        espaces = 1
    end


     if paramSep == ',' then
     if paramSep == ',' then

Version du 10 mai 2016 à 08:27

local z = {}

function z.main(frame)

   local args = frame:getParent().args
   local trimFunc = mw.text.trim -- cache accès global
   function getParam(name, default)
       if args[name] ~= nil and args[name] ~=  then
           return args[name]
       else
           return default -- nil si non spécifié
       end
   end
   local paramSep, espaces, glue
   local values = {}
   paramSep = getParam('séparateur', getParam('sép', '·'))
   espaces = tonumber(getParam('espaces', '1'))
   if paramSep == ',' then
       glue = ', '
   elseif mw.text.unstripNoWiki(paramSep) == ' ' or paramSep == ' ' then --  ,  
       glue = paramSep
   elseif paramSep == '2·' or paramSep == '·2' then
       -- '\194\160' est une espace insécable (code UTF-8 sur deux octets)
       glue = '\194\160\194\160·\194\160 '
   elseif paramSep == '2•' or paramSep == '•2' then
       glue = '\194\160\194\160•\194\160 '
   else
       if paramSep == '·' then
           paramSep = '·'
       elseif paramSep == '-' or paramSep == '−' then
           paramSep = '–' -- tiret demi-cadratin
       elseif paramSep == '--' then
           paramSep = '—' -- tiret cadratin
       end
       if espaces == 0 then
           glue = paramSep
       else
           glue = string.rep('\194\160', espaces) .. paramSep .. string.rep('\194\160', espaces-1) .. ' '
       end
   end
   local secable = (args['sécable'] == 'oui')
   for i,v in ipairs(args) do
       local value = trimFunc(v)
       if value ~=  then
           if not secable then
               value = ''..value..''
           end
           values[#values+1] = value
       end
   end
   return table.concat(values, glue)

end

return z