Module:Devmodule

From The Satanic Wiki
Revision as of 22:56, 22 September 2018 by Mediawiki>DarthKitty
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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("Dev:Arguments").getArgs
local normalize = require("Dev:NormalizeModuleName")
local userError = require("Dev:User error")
local removeArgs = require("Dev: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.)