This module was made as a sandbox for the FrameTools module. This documentation is kept to prevent redlinks.
-- <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, unpack(arg))
end
parent[name] = function (parent, ...)
return pseudoFrame[name](frame, unpack(arg))
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()
-- Needs some testing to know why this causes bugs
-- checkSelf(self, "getParent")
return self.parent
end
function pseudoFrame:newChild(...)
checkSelf(self, "newChild")
local params = unpack(arg)
local tmpTable = p.makePseudoFrame(frame, self.args, params.args)
tmpTable.parent.title = self.title
tmpTable.title = params.title
return tmpTable
end
-- Functions useful for debugging.
function pseudoFrame:showargs()
checkSelf(self,"showargs")
local sChild = "Child parameters:\n"
local sParent = "Parent parameters:\n"
if (self.args) then
for i,v in pairs(self.args) do
sChild = sChild .."\n Key: "..i .. "- Value: " ..v
end
end
if (self.parent.args) then
for i,v in pairs(self.parent.args) do
sParent = sParent .."\n Key: "..i .. " - Value: " ..v
end
end
return sChild .."\n\n"..sParent
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 (sWikiText)
checkSelf(self,"preprocess")
return sWikiText
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.)