Module:Devmodule/sandbox

--

-- Invokes global modules from dev.wikia.com -- Syntax: -- --

local p = {}

-- Creates a new frame object. -- -- Removes the `#modulename`, `#mod`, and `#fname` parameters (which are added -- by Devmodule) from the frame.

local function makePseudoFrame(frame) if type(frame) ~= "table" then error("frame must be a table") end

local pseudoFrame = {}

for name in pairs(frame) do       pseudoFrame[name] = function (pseudoFrame, ...) local args = unpack(arg)

return frame[name](frame, args) end end

pseudoFrame.args = mw.clone(frame.args) pseudoFrame.args["#modulename"] = nil pseudoFrame.args["#mod"] = nil pseudoFrame.args["#fname"] = nil

function pseudoFrame:getParent if frame:getParent then return makePseudoFrame(frame:getParent) end end

return pseudoFrame end

-- Describe the following function here.

local function getInvokeDetails(frame) if not (frame and frame.args) then return end local args = require("Dev:Arguments").getArgs(frame) return args["#mod"] or args["#modulename"], args["#fname"] end

-- Takes the name of a module, removes any prefixes, and makes it -- case-insensitive.

local function normalize(name) local ustring = mw.ustring local uLower = ustring.lower local uSub = ustring.sub

-- When loading a module, these prefixes will work: local prefixes = { "Dev",                      -- added by Wikia "Module",                   -- canonical namespace name mw.site.namespaces[828].name -- namespace name in your language }

-- Reduce iterations when the canonical and local namespace names are equal. -- I.e.: avoid checking the same thing twice. -- @TODO check if this is true for any non-English languages if prefixes[2] == prefixes[3] then prefixes[3] = nil end

for _, prefix in ipairs(prefixes) do       prefix = uLower(prefix) .. ":"

local prefixLength = ustring.len(prefix) local lowerName = uLower(name)

-- if `name` starts with a prefix, remove it       if uSub(lowerName, 1, prefixLength) == prefix then name = uSub(name, prefixLength + 1)

-- only remove the first prefix, e.g.: -- "Dev:Module:Foo" => "Module:Foo" break end end

return mw.getContentLanguage:ucfirst(name) end

-- Describe the following function here.

local function loadModule(moduleName, functionName, frame) moduleName = normalize(moduleName)

local globalModule = require("Dev:" .. moduleName) local func = globalModule[functionName or "main"]

if func then return func(frame) end end

-- Describe the following function here.

function p.main(frame) local moduleName, functionName = getInvokeDetails(frame)

if type(moduleName) ~= "string" then error("Right syntax: \#invoke:devmodule|aa|bb|cc|#fname=main|#modulename=if") end local pseudoFrame = makePseudoFrame(frame) local status, results = pcall(loadModule, moduleName, functionName, pseudoFrame)

if not status and results:match("module.*(" .. moduleName .. ").*not found") then return "Module `" .. moduleName .. "` does not exist on dev.wikia." end

return results end

return p

-- -- (Add categories here.)