Module:LuaCall: Difference between revisions

From Test Wiki
Jump to navigation Jump to search
Content deleted Content added
Sometime I should figure out better answers to this annoyance of suppressed \ns
Time to declare total war on the debug formatting. Forgot why I didn't do this (PITA), but in this case I want a debug someone else can read
Line 3: Line 3:
function p.main(frame)
function p.main(frame)
local parent=frame.getParent(frame) or {}
local parent=frame.getParent(frame) or {}
local debuglog="Parent: \n\n"
local debuglog="Parent: </nowiki><br /><nowiki>"
for k,v in pairs(parent.args or {}) do
for k,v in pairs(parent.args or {}) do
_G[k]=v -- transfer every parameter directly to the global variable table
_G[k]=v -- transfer every parameter directly to the global variable table
debuglog=debuglog..k.."="..v.."\n\n"
debuglog=debuglog..k.."="..v.."</nowiki><br /><nowiki>"
end
end
debuglog=debuglog.."Frame: \n\n"
debuglog=debuglog.."Frame: </nowiki><br /><nowiki>"
for k,v in pairs(frame.args or {}) do
for k,v in pairs(frame.args or {}) do
debuglog=debuglog..k.."="..v.."\n\n"
debuglog=debuglog..k.."="..v.."</nowiki><br /><nowiki>"
_G[k]=v -- transfer every parameter directly to the global variable table
_G[k]=v -- transfer every parameter directly to the global variable table
end
end
Line 18: Line 18:
if _G[1] then
if _G[1] then
local reserved_function,reserved_contents=mw.ustring.match(_G[1],"^(%s*%a[^%s%(]*)%(([^%)]*)%)%s*$")
local reserved_function,reserved_contents=mw.ustring.match(_G[1],"^(%s*%a[^%s%(]*)%(([^%)]*)%)%s*$")
debuglog=debuglog.."Function is:" .. reserved_function .. "\n\nContents are:"..reserved_contents.."\n\n"
debuglog=debuglog.."Function is:" .. reserved_function .. "</nowiki><br /><nowiki>Contents are:"..reserved_contents.."</nowiki><br /><nowiki>"
local reserved_value={}
local reserved_value={}
local reserved_counter=0
local reserved_counter=0
Line 25: Line 25:
reserved_counter=reserved_counter+1
reserved_counter=reserved_counter+1
reserved_value[reserved_counter]=mw.ustring.match(reserved_contents,"([^%,]+)")
reserved_value[reserved_counter]=mw.ustring.match(reserved_contents,"([^%,]+)")
debuglog=debuglog.."["..reserved_counter.."] = "..tostring(reserved_value[reserved_counter]) .. "\n\n"
debuglog=debuglog.."["..reserved_counter.."] = "..tostring(reserved_value[reserved_counter]) .. "</nowiki><br /><nowiki>"
reserved_contents=mw.ustring.match(reserved_contents,"[^%,]+,(.*)$")
reserved_contents=mw.ustring.match(reserved_contents,"[^%,]+,(.*)$")
until not reserved_contents
until not reserved_contents

Revision as of 15:51, 17 April 2013

Documentation for this module may be created at Module:LuaCall/doc

local p={}

function p.main(frame)
    local parent=frame.getParent(frame) or {}
    local debuglog="Parent: </nowiki><br /><nowiki>"
    for k,v in pairs(parent.args or {}) do
        _G[k]=v -- transfer every parameter directly to the global variable table
        debuglog=debuglog..k.."="..v.."</nowiki><br /><nowiki>"
    end
    debuglog=debuglog.."Frame: </nowiki><br /><nowiki>"
    for k,v in pairs(frame.args or {}) do
        debuglog=debuglog..k.."="..v.."</nowiki><br /><nowiki>"        
        _G[k]=v -- transfer every parameter directly to the global variable table
    end
     --- Alas Scribunto does NOT implement coroutines, according to
     --- http://www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual#string.format
     --- this will not stop us from trying to implement one single lousy function call
    if _G[1] then
        local reserved_function,reserved_contents=mw.ustring.match(_G[1],"^(%s*%a[^%s%(]*)%(([^%)]*)%)%s*$")
        debuglog=debuglog.."Function is:" .. reserved_function .. "</nowiki><br /><nowiki>Contents are:"..reserved_contents.."</nowiki><br /><nowiki>"
        local reserved_value={}
        local reserved_counter=0
        if reserved_contents then
            repeat
                reserved_counter=reserved_counter+1
                reserved_value[reserved_counter]=mw.ustring.match(reserved_contents,"([^%,]+)")
                debuglog=debuglog.."["..reserved_counter.."] = "..tostring(reserved_value[reserved_counter]) .. "</nowiki><br /><nowiki>"
                reserved_contents=mw.ustring.match(reserved_contents,"[^%,]+,(.*)$")
            until not reserved_contents
        end
    end
    if not reserved_function then return frame.preprocess(frame,"<nowiki>"..debuglog.."</nowiki>") end
    return _G[reserved_function](unpack(reserved_contents or {}))
end
return p