Modul:LuaWiki

Modul:LuaWiki

Modul:LuaWiki
Vorlagen-
programmierung
Diskussionen Lua Unterseiten
Modul Deutsch English

Esperanto Dolnoserbski Hornjoserbsce Modul: WP:Lua

Diese Seite enthält Code in der Programmiersprache Lua. Einbindungszahl Cirrus


--[=[ 2013-05-07
LuaWiki - Support Lua programming in Wiki environment
* .getArg()
* .initVariables()
* .getVariable()
* .isExisting()
* .setFrame()
* .transclude()
]=]



-- Module globals
local wikiVariables
local LuaWiki = {}



LuaWiki.error = function ( about )
    -- Enclose errorMsg with <span>
    -- Precondition:
    --     about  -- string
    local r = about
    if type( about ) == "string" then
        if #about == 0 then
            r = "Error in Lua"
        end
    else
        r = tostring( about )
    end
    return "<span class='error'>" .. error( r, 3 ) .. "</span>"
end -- LuaWiki.error()



LuaWiki.getArg = function ( arg, assign )
    -- Retrieve template argument
    -- Precondition:
    --     arg     -- string or number; argument identifier
    --     assign  -- any, optional; default value
    -- Uses:
    --     mw.getCurrentFrame()
    local r = mw.getCurrentFrame().args[ arg ]
    if type( r ) ~= "string" then
        if type( assign ) == nil then
            r = "{{{<" .. arg .. ">}}}"
        else
            r = assign
        end
    end
    return r
end -- LuaWiki.getArg()



LuaWiki.getVariable = function ( seek, numeric )
    -- Retrieve item from wikiVariables; populate if not yet present
    -- Precondition:
    --     seek     -- string; name of variable
    --     numeric  -- true: seek is numeric (else string)
    -- Uses:
    --     >< wikiVariables
    --     mw.getCurrentFrame()
    local g, i, n
    local r = false
    if type( wikiVariables ) == "table" then
        n = #wikiVariables
        for i = 1, n do
            g = wikiVariables[ i ]
            if g then
                if g[ 1 ] == seek then
                    r = g[ 2 ]
                    break;
                end
            end
        end -- for i
    else
        wikiVariables = { }
        n = 0
    end
    if not r then
        g = mw.getCurrentFrame():preprocess( "{{" .. seek .. "}}" )
        r = mw.ustring.match( g, "^(.*)$" )
        if numeric then
            r = tonumber( g )
        end
        table.insert( wikiVariables,  n + 1,  { seek, r } )
    end
    return r
end -- LuaWiki.getVariable()



LuaWiki.initVariables = function ( request )
    -- Initialize wikiVariables
    -- Precondition:
    --     request  -- table; every element either
    --                        * string; name of variable
    --                        * table; name string and true, if numeric
    -- Uses:
    --      < wikiVariables
    --     mw.getCurrentFrame()
    local g, i, n, s
    local src = "|"
    wikiVariables = { }
    for i = 1, #request do
         s = request[ i ]
         if type( s ) == "table" then
             s = s[ 1 ]
         end
         src = src .. s .. "={{" .. s .. "}}|"
    end -- for i
    src = mw.getCurrentFrame():preprocess( src )
    for i = 1, #request do
        s = request[ i ]
        n = ( type( s ) == "table" )
        if n then
            n = s[ 2 ]
        end
        if n then
            g = "-?%d+"
            s = s[ 1 ]
        else
            g = "[^|]*"
        end
        g = mw.ustring.match( src,  "|" .. s .. "=(" .. g .. ")|" )
        if n then
            g = tonumber( g )
        end
        table.insert( wikiVariables,  i,  { s, g } )
    end -- for i
end -- LuaWiki.initVariables()



LuaWiki.isExisting = function ( seek )
    -- Return true if page exists, else false
    -- Precondition:
    --     seek  -- string; full page name
    -- Uses:
    --     mw.getCurrentFrame()
    local g = mw.getCurrentFrame():callParserFunction( "#ifexist",
                                                       { seek,
                                                         "1",
                                                         "0" } )
    return ( g == "1" )
end -- LuaWiki.isExisting()



LuaWiki.transclude = function ( s, args )
    -- Save transclusion of a page, or error message
    -- Precondition:
    --     s     -- string; page name
    --     args  -- table or nil; arguments
    -- Uses:
    --     mw.getCurrentFrame()
    --     error()
    local r = { s, "1" }
    local frame = mw.getCurrentFrame()
    if frame:callParserFunction( "#ifexist", r ) == "1" then
        if args then
            r = frame:expandTemplate{ title = s, args = args }
        else
            r = frame:expandTemplate{ title = s }
        end
    else
        r = error( "No transclude page '" .. s .. "'" )
    end
    return r
end -- LuaWiki.transclude()



return LuaWiki

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.