Module:Liste éléments

From CryptoWiki

Documentation for this module may be created at Module:Liste éléments/doc

local p = {}

function p.main( frame )

    -- http://lua-users.org/wiki/StringTrim
    local function trim( s )
        return s:match( '^()%s*$' ) and '' or s:match( '^%s*(.*%S)' )
    end

    local args = frame:getParent().args

    local 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 = getParam( 'séparateur', getParam( 'sép', '·' ) )
    local espaces = tonumber( getParam( 'espaces', '1' ) )
    local glue

    if paramSep == ',' then
        glue = ', '
    elseif ( paramSep:len() > 10 and mw.text.unstripNoWiki( paramSep ) == ' ' ) or paramSep == ' ' then -- {{espace}}
        if espaces == 0 then
            glue = ''
        else
            glue = ( '\194\160' ):rep( espaces - 1 ) .. paramSep
        end
    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<span class="sep-liste">·</span>\194\160 '
    elseif paramSep == '2•' or paramSep == '•2' then
        glue = '\194\160\194\160•\194\160 '
    else
        if paramSep == '·' then
            paramSep = '<span class="sep-liste">·</span>'
        elseif paramSep == '-' then
            paramSep = '–' -- tiret demi-cadratin
        end
        if espaces == 0 then
            glue = paramSep
        else
            glue = ( '\194\160' ):rep( espaces ) .. paramSep .. ( '\194\160' ):rep( espaces - 1 ) .. ' '
        end
    end

    local secable = ( args[ 'sécable' ] == 'oui' )
    local items = {}

    for i, v in ipairs( args ) do
        local item = trim( v )
        if item ~= '' then
            if not secable then
                items[ #items + 1 ] = '<span class="nowrap">' .. item .. '</span>'
            else
                items[ #items + 1 ] = item
            end
        end
    end

    if #items > 0 then
        local templatestyles = frame:extensionTag( 'templatestyles', '', { src = 'Modèle:Liste éléments/styles.css' } )

        return templatestyles .. table.concat( items, glue )
    else
        return ''
    end
end

return p