Module:Devmodule

From The Satanic Wiki
Jump to navigation Jump to search

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

-- <nowiki>
--------------------------------------------------------------------------------
-- Invokes global modules from dev.wikia.com.
--
-- Syntax:
-- {{Devmodule|#mod = <module name>|#fun = <function name>|<parameters>}}
--------------------------------------------------------------------------------
local p = {}

local getArgs = require("Module:Arguments").getArgs
local normalize = require("Module:NormalizeModuleName")
local userError = require("Module:User error")
local removeArgs = require("Module:FrameTools").removeArgs

--% Entry point to use module
--@ frame (table) A scribunto frame
--: (string) The return content of the module 
function p.main(frame)
    local args = getArgs(frame)
    local moduleName = args["#mod"] or args["#modulename"]
    local functionName = args["#fun"] or args["#fname"] or "main"
    local success, _, invokedModule

    -- try to `normalize()` the given module name
    -- if this fails, then the user did not pass the right parameters
    success, _, moduleName = pcall(normalize, moduleName)

    if not success then
        return userError("no `#mod` parameter given " ..
                         "(should be the name of a module on dev.wikia)")
    end

    -- try to `require()` the given module
    -- if this fails, then the user gave an incorrect module name
    success, invokedModule = pcall(require, "Dev:" .. moduleName)

    if not success then
        return userError("could not find a module called `" .. moduleName ..
                         "` on dev.wikia")
    end

    -- make sure the module has a function called `functionName`
    -- if this fails, then the user gave an incorrect function name
    local func = invokedModule[functionName]

    if not func then
        return userError("that module does not have a function called `" ..
                         functionName .. "`")
    end

    -- create a frame-like object, without the Devmodule-exclusive arguments
    local pseudoFrame = removeArgs(frame, "#mod", "#modulename", "#fun",
                                   "#fname")

    -- call the function
    -- any errors from the function itself will be passed to the user
    return func(pseudoFrame)
end

return p

-- </nowiki>
-- (Add categories here.)