Modul:Vorlage:Str

aus Wikipedia, der freien Enzyklopädie
Zur Navigation springen Zur Suche springen

Die Dokumentation für dieses Modul kann unter Modul:Vorlage:Str/Doku erstellt werden

--[=[ 2013-10-19
{{Template:Str *********}}
]=]



local Templates = { }



function Templates.crop( args )
    -- Used to remove the right-most {2} characters of a string {1}.
    -- Precondition:
    --     1 = A string
    --     2 = A length; 1 if omitted
    -- Postcondition:
    --     Throws error if {2} < 0
    --     Returns string; remaining starting characters, if any.
    local r
    if args[ 1 ] then
        local n = tonumber( args[ 2 ] )
        r = mw.ustring.len( args[ 1 ] )
        if not n then
            if args[ 2 ] then
                n = r
            else
                n = 1
            end
        elseif n < 0 then
            error( "&#123;{Str&nbsp;crop&#124;&#124;"
                   .. args[ 2 ] .. "&lt;0}}",  0 )
        end
        if n >= r then
            r = ""
        else
            if n == 0 then
                r = args[ 1 ]
            elseif n == r then
                r = ""
            else
                r = mw.ustring.sub( args[ 1 ],  1,  r - n )
            end
        end
    else
        r = ""
    end
    return r
end -- .crop()



function Templates.find( args )
    -- Position of first appearance of {2} in {1}.
    -- Precondition:
    --     1 = A base_string
    --     2 = A sub_string
    -- Postcondition:
    --     Returns string; -1 if sub_string not found
    --                     Character position is 1 based
    --                     (not 0 based as usual in calculations).
    local r
    if args[ 1 ] and args[ 2 ] then
        local k
        r, k = mw.ustring.find( args[ 1 ], args[ 2 ], 1, true )
        if not r then
            r = -1
        end
    elseif args[ 1 ] then
        r = 1
    else
        r = -1
    end
    return r
end -- .find()



function Templates.ge_len( args )
    -- Conditional answer depending on string {1} length wrt {2}.
    -- Template:Str ≥ len
    -- Precondition:
    --     1 = A string
    --     2 = A length
    --     3 = Data to return/render when "longer than or equally long".
    --     4 = Data to return/render when "shorter than".
    -- Postcondition:
    --     Throws error if {1} undefined or invalid.
    --     Returns string; either {3} or {4}.
    local r
    if args[ 1 ] then
        local n = tonumber( args[ 2 ] )
        r = mw.ustring.len( args[ 1 ] )
        if not n then
            r = args[ 3 ] or ""
        elseif r < n then
            r = args[ 4 ] or ""
        else
            r = args[ 3 ] or ""
        end
    else
        error( "&#123;{Str&nbsp;&#8805;&nbsp;len}}", 0 )
    end
    return r
end -- .ge_len()



function Templates.index( args )
    -- Returns the {2}-th character of trimmed text {1}.
    -- Precondition:
    --     1 = A string
    --     2 = A position, counted from 1 in string
    -- Postcondition:
    --     Throws error if {2} < 0 or {2} out of {1}
    local r = tonumber( args[ 2 ] )
    if r then
        if r <= 0 then
            error( "&#123;{Str&nbsp;index&#124;&#124;"
                   .. args[ 2 ] .. "&lt;=0}}",  0 )
        elseif args[ 1 ] then
            local n = mw.ustring.len( args[ 1 ] )
            if n < r then
                error( "&#123;{Str&nbsp;index&#124;&#124;"
                       .. args[ 2 ] .. "&gt;&gt;}}",  0 )
            else
                r = mw.ustring.sub( args[ 1 ],  r,  r )
            end
        else
            error( "&#123;{Str&nbsp;index}}", 0 )
        end
    else
        if args[ 2 ] then
            r = args[ 2 ] .. "}}"
        else
            r = "}}"
        end
        error( "&#123;{Str&nbsp;index&#124;&#124;" .. r, 0 )
    end
    return r
end -- .index()



function Templates.left( args )
    -- Gives the {2}-length substring of characters
    -- from the start of the trimmed string {1}.
    -- Precondition:
    --     1 = A string
    --     2 = A length; if omitted, 1 is used
    -- Postcondition:
    --     Return the left {2} characters of {1}.
    --     If {2} is invalid, empty or zero, an empty string is returned.
    local n, r
    if not args[ 2 ] then
        n = 1
    elseif args[ 2 ] == "" then
        n = 0
    else
        n = tonumber( args[ 2 ] )
        if not n then
            n = 0
        end
    end
    if n > 0  and  args[ 1 ] then
        r = mw.ustring.sub( args[ 1 ], 1, n )
    else
        r = ""
    end
    return r
end -- .left()



function Templates.len( args )
    -- Returns length of string (excluding spaces at the start and end).
    -- Precondition:
    --     1 = A string
    local r
    if args[ 1 ] then
        r = tostring( mw.ustring.len( args[ 1 ] ) )
    else
        r = "0"
    end
    return r
end -- .len()



function Templates.repeating( args )
    -- Repeat string {1} as often as {2} indicates.
    -- Precondition:
    --     1 = A string; whitespace around will be kept
    --     2 = A factor; 1 if omitted or invalid
    -- Postcondition:
    --     Returns string.
    local r = args[ 1 ];
    if r then
        local n = tonumber( args[ 2 ] )
        if not n then
            n = 1
        end
        if n > 1 then
            r = r:rep( n )
        end
    else
        r = ""
    end
    return r
end -- .repeating()



function Templates.right( args )
    -- Gives the characters from {2} to the end of the string {1}.
    -- Precondition:
    --     1 = A string
    --     2 = An offset
    --         A negative offset is treated the same as zero,
    --         which simply returns the original string.
    local r
    if args[ 1 ] then
        local n = tonumber( args[ 2 ] )
        if not n then
            n = 0
        end
        if n > 0 then
            r = mw.ustring.sub( args[ 1 ],  n + 1 )
        else
            r = args[ 1 ]
        end
    else
        r = ""
    end
    return r
end -- .right()



function Templates.sub( args )
    -- Substring of string {1} starting at {2} and containing {3} chars.
    -- en:Template:Str sub old
    -- Precondition:
    --     1 = A string
    --     2 = An offset
    --         Base 0: the first character is numbered 0, and so on.
    --     3 = A length
    local r
    local e = false
    if args[ 1 ] then
        local n = tonumber( args[ 3 ] )
        if not n then
            if args[ 3 ] then
                e = "&#124;&#124;&#124;" .. args[ 3 ] .. "}}"
            else
                n = 0
            end
        end
        if not e then
            if n < 0 then
                e = "&#124;&#124;&#124;" .. args[ 3 ] .. "&lt;0}}"
            else
                local i = tonumber( args[ 2 ] )
                if not i  or  i < 0 then
                    if args[ 2 ] then
                        e = "&#124;&#124;" .. args[ 2 ] .. "}}"
                    else
                        e = "&#124;&#124;}}"
                    end
                elseif n == 0 then
                    r = ""
                else
                    r = mw.ustring.sub( args[ 1 ],  i + 1,  i + n )
                end
            end
        end
    else
       e = "}}"
    end
    if e then
        error( "&#123;{Str&nbsp;sub" .. e,  0 )
    end
    return r
end -- .sub()



local function Template( frame, action, leave )
    -- Run actual code from template transclusion
    -- Precondition:
    --     frame   -- object
    --     action  -- string with function name
    --     leave   -- true: keep whitespace around
    -- Postcondition:
    --     Return string; like the legacy templates
    --                    might be error message
    local k, v
    local got = frame:getParent().args
    if not leave then
        for k, v in pairs( got ) do
            got[ k ] = mw.text.trim( v )
        end -- for k, v
    end
    local lucky, r = pcall( Templates[ action ], got )
    if not lucky then
        r = "<span class=\"error\">" .. r .. "</span>"
    end
    return r
end -- Template()



-- Export
local p = { }

function p.TEST( action, args )
    -- Run main code from test environment
    -- Precondition:
    --     action  -- string with function name
    --     args    -- table; simulated environment
    -- Postcondition:
    --     Return string; like the legacy templates
    local k, v
    for k, v in pairs( args ) do
        args[ k ] = mw.text.trim( v )
    end -- for k, v
    local lucky, r = pcall( Templates[ action ], args )
    return r
end -- p.TEST()

p.crop = function ( frame )
    return Template( frame, "crop" )
end
p.find = function ( frame )
    return Template( frame, "find" )
end
p.index = function ( frame )
    return Template( frame, "index" )
end
p.left = function ( frame )
    return Template( frame, "left" )
end
p.len = function ( frame )
    return Template( frame, "len" )
end
p[ "≥ len" ] = function ( frame )
    return Template( frame, "ge_len" )
end
p.repeating = function ( frame )
    return Template( frame, "repeating", true )
end
p.right = function ( frame )
    return Template( frame, "right" )
end
p.sub = function ( frame )
    return Template( frame, "sub" )
end

return p