Module:Multiparamètres

Depi Wikipedya, ansiklopedi lib

Fonctions fournies par ce module :

Modèl:Projet Scribunto


Catégorie:Module en langage Lua


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

local p = {}

function p.brut( frame )
    return p._main( frame, function ( item ) return item end )
end

function p.lien( frame )
    return p._main( frame, function ( item ) return '[[' .. ( ( item:sub( 1, 1 ) == ':' ) and item or ':' .. item ) .. ']]' end )
end

function p.categorie( frame )
    return p._main( frame, function ( item ) return '[[:Kategori:' .. item .. '|' .. item .. ']]' end )
end

function p.utilisateur( frame )
    return p._main( frame, function ( item ) return '[[Itilizatè:' .. item .. '|' .. item .. ']]' end )
end

function p._main( frame, formatter )
    local args = frame:getParent().args

    local gras = ( args.gras and args.gras ~= '' )
    local items = {}

    for i, v in ipairs( args ) do
        local item = trim( v )
        if item ~= '' then
            item = formatter( item )
            if gras then
                item = "'''" .. item .. "'''"
            end
            items[ #items + 1 ] = item
        end
    end

    local dernierSeparateur = args.et and ( ' ' .. args.et .. ' ' )

    -- le paramètre "et" peut être défini à vide, exemple : [[Modèl:Module biblio/responsabilité principale]]
    if args.et == '' then
        dernierSeparateur = ' '
    end

    return mw.text.listToText( items, ', ', dernierSeparateur )
end

return p