FANDOM


-- <nowiki>
--------------------------------------------------------------------------------
-- This module contains helper functions for dealing with frame objects.
--------------------------------------------------------------------------------
local p = {}
local libraryUtil = require("libraryUtil")
 
--------------------------------------------------------------------------------
-- Frame methods are protected by a `checkSelf` function, which makes them more
-- difficult to copy.
--------------------------------------------------------------------------------
function p.copy(frame)
    local copy = mw.clone(frame)
 
    -- Point methods on `copy` to their `frame` counterparts
    for methodName, method in pairs(frame) do
        if type(method) == "function" and methodName ~= "getParent" then
            copy[methodName] = function (copy, ...)
                return method(frame, ...)
            end
        end
    end
 
    -- This method needs special treatment
    function copy:getParent()
        local parent = frame:getParent()
 
        if parent then
            return p.copy(parent)
        end
    end
 
    return copy
end
 
--------------------------------------------------------------------------------
-- Creates a pseudo frame with some useful functions available in
-- [[mw:Extension:Scribunto]], e.g. `newChild`.
--------------------------------------------------------------------------------
function p.makePseudoFrame(frame, parentArgs, childArgs)
    local pseudoFrame = {parent = {}}
    local checkSelf = libraryUtil.makeCheckSelfFunction("pseudoFrame", "obj",
                                                        pseudoFrame,
                                                        "PseudoFrame object")
    local parent = pseudoFrame.parent
    local pArgs = {}
    local args = {}
 
    if frame then
        if type(frame) ~= "table" then
            error("frame must be a table")
        end
 
        args = frame.args
 
        for name in pairs(frame) do
            pseudoFrame[name] = function (pseudoFrame, ...)
                return frame[name](frame, ...)
            end
 
            parent[name] = function (parent, ...)
                return pseudoFrame[name](frame, ...)
            end
        end
 
        if frame:getParent() then
            pArgs = frame:getParent().args
        end
    end
 
    pseudoFrame.parent.args = mw.clone(parentArgs or pArgs)
    pseudoFrame.args = mw.clone(childArgs or args)
    pseudoFrame.title = tostring(mw.title.getCurrentTitle())
    pseudoFrame.parent.title = ""
 
    function pseudoFrame:getTitle()
        checkSelf(self, "getTitle")
 
        return self.title
    end
 
    function pseudoFrame.parent:getTitle()
        return self.parent.title
    end
 
    function pseudoFrame:getParent()
        return self.parent
    end
 
    -- Creates a new Frame object that is a child of the current frame, optional
    -- arguments and title.
    -- Syntax: frame:newChild{title = title, args = table}
    function pseudoFrame:newChild(attributes)
        checkSelf(self, "newChild")
 
        local tmpTable = p.makePseudoFrame(frame, self.args, attributes.args)
 
        tmpTable.parent.title = self.title
        tmpTable.title = attributes.title
 
        return tmpTable
    end
 
    function pseudoFrame:showargs()
        checkSelf(self, "showargs")
 
        local childParams = "Child parameters:\nKey \t Value"
        local parentParams = "Parent parameters:\nKey \t Value"
 
        if self.args then
            for k, v in pairs(self.args) do
                childParams = childParams .. "\n" .. k .. "\t" .. v
            end
        end
 
        if self.parent.args then
            for k, v in pairs(self.parent.args) do
                parentParams = parentParams .. "\n" .. k .. "\t" .. v
            end
        end
 
        return childParams .. "\n\n" .. parentParams
    end
 
    function pseudoFrame:setArgs(childArgs, parentArgs)
        checkSelf(self, "setArgs")
 
        if childArgs then
            self.args = childArgs or {}
        end
 
        if parentArgs then
           self.parent.args = parentArgs
        end
    end
 
    if not pseudoFrame.preprocess then
        function pseudoFrame:preprocess(wikiText)
            checkSelf(self, "preprocess")
 
            return wikiText
        end
    end
 
    return pseudoFrame
end
 
--------------------------------------------------------------------------------
-- Returns a frame-like object with the given arguments removed.
--------------------------------------------------------------------------------
function p.removeArgs(frame, ...)
    local pseudoFrame = p.copy(frame)
    local args = pseudoFrame.args
    local metatable = getmetatable(args)
 
    -- disable arg caching
    metatable.__index = nil
    metatable.__pairs = nil
 
    -- remove args
    for _, arg in ipairs{...} do
        args[arg] = nil
    end
 
    return pseudoFrame
end
 
return p
 
-- </nowiki>
-- (Add categories here.)

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.