close
Sayahna Sayahna
Search

Difference between revisions of "Module:Br separated entries"


m (Changed protection level of Module:Br separated entries: High-risk Lua module: allow template editors ([Edit=Allow only template editors and admins] (indefinite) [Move=Allow only template editors and admins] (indefinite)))
 
m (1 revision)
 
(No difference)

Latest revision as of 06:25, 8 March 2014

Documentation for this module may be created at Module:Br separated entries/doc

local p = {}

local function _main( args )
    local sep = '<br />'
    local lastsep
    local t = {}
    for i, v in ipairs( args ) do
        table.insert( t, v )
        if mw.ustring.match( v, '%S' ) then
            table.insert( t, sep )
            lastsep = #t
        end
    end
    if lastsep then
        table.remove( t, lastsep )
    end
    return table.concat( t )
end

function p.main( frame )
    local origArgs
    if frame == mw.getCurrentFrame() then
        -- We're being called via #invoke. If the invoking template passed any arguments,
        -- use them. Otherwise, use the arguments that were passed into the template.
        origArgs = frame:getParent().args
        for k, v in pairs( frame.args ) do
            origArgs = frame.args
            break
        end
    else        
        -- We're being called from another module or from the debug console, so assume
        -- the arguments are passed in directly.
        origArgs = frame
    end
    
    -- Use integer args only, and allow for explicit positional arguments
    -- that are specified out of order, e.g. {{br separated entries|3=entry3}}.
    -- After processing, the args can be accessed accurately from ipairs.
    local args = {}
    for k, v in pairs( origArgs ) do
        if type( k ) == 'number' and k >= 1 and math.floor( k ) == k then
            table.insert( args, k )
        end
    end
    table.sort( args )
    for i,v in ipairs( args ) do
        args[ i ] = origArgs[ v ]
    end
    
    return _main( args )
end

return p