模块:Deep4 FrameChain

来自希服维基

可在模块:Deep4 FrameChain/doc创建此模块的帮助文档

local p = {}
function p.test(frame)
    local results = {}

    if type(frame) == 'nil' or frame == nil then
        -- Try to get frame from mw
        frame = mw.getCurrentFrame()
    end

    results[1] = "frame_type=" .. type(frame)

    if frame and type(frame) ~= 'nil' then
        results[2] = "getParent=" .. type(frame.getParent)

        if frame.getParent then
            local parent = frame:getParent()
            results[3] = "parent=" .. type(parent)

            if parent and type(parent) ~= 'nil' then
                local parent_methods = {}
                for _, m in ipairs({"getTitle", "preprocess", "extensionTag", "callParserFunction", "expandTemplate", "getArgument", "newChild"}) do
                    if parent[m] then
                        parent_methods[#parent_methods+1] = m .. "=" .. type(parent[m])
                    end
                end
                results[4] = "parent_methods: " .. table.concat(parent_methods, ", ")

                -- Try parent:getTitle
                if parent.getTitle then
                    results[5] = "title=" .. tostring(parent:getTitle())
                end

                -- Try calling parent:callParserFunction
                if parent.callParserFunction then
                    local ok, out = pcall(parent.callParserFunction, parent, "expr", "2+2")
                    results[6] = "callPF(expr)=" .. tostring(ok) .. ":" .. tostring(out)
                end

                -- Try parent:preprocess
                if parent.preprocess then
                    local ok, out = pcall(parent.preprocess, parent, "'''bold'''")
                    results[7] = "preprocess=" .. tostring(ok) .. ":" .. tostring(out):sub(1,80)
                end
            else
                results[3] = "no_parent"
            end
        end

        -- Grandparent
        if frame.getParent then
            local parent = frame:getParent()
            if parent and parent.getParent then
                local gp = parent:getParent()
                results[8] = "grandparent=" .. type(gp)
                if gp and gp.getTitle then
                    results[9] = "gp_title=" .. tostring(gp:getTitle())
                end
            end
        end
    end

    return table.concat(results, " | ")
end
return p