Zum Inhalt springen

„Modul:LuaWiki“ – Versionsunterschied

aus Wikipedia, der freien Enzyklopädie
Inhalt gelöscht Inhalt hinzugefügt
Setup
(kein Unterschied)

Version vom 24. April 2013, 20:58 Uhr

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


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



-- Module globals
local wikiVariables
local templateFrame
local LuaWiki = {}



LuaWiki.getArg = function ( arg, assign )
    -- Retrieve template argument
    -- Precondition:
    --     arg     -- string or number; argument identifier
    --     assign  -- any, optional; default value
    -- Uses:
    --     >  templateFrame
    local r = templateFrame.args[ arg ]
    if type( r ) ~= "string" then
        if type( assign ) == nil then
            r = "{{{<" .. arg .. ">}}}"
        else
            r = assign
        end
    end
    return r
end -- 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:
    --     >  templateFrame
    --     >< wikiVariables
    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 ]
                  i = n
              end
           end
        end -- for i
    else
        wikiVariables = { }
        n = 0
    end
    if not r then
        g = templateFrame: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 -- 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:
    --     >  templateFrame
    --      < wikiVariables
    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 = templateFrame:preprocess( src )
    for i = 1, #request do
        s = request[ i ]
        n = ( type( s ) == "table" )
        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 -- initVariables()



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



LuaWiki.setFrame = function ( frame )
    -- Initialize frame for this utility
    -- Precondition:
    --     frame  -- templateFrame
    -- Uses:
    --      < templateFrame
    templateFrame = frame
end -- setFrame()



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



return LuaWiki