Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Module:CategoryTranscluder

From Teriock
Revision as of 11:16, 14 January 2025 by Gpe (talk | contribs) (Created page with "local p = {} -- Function to retrieve and transclude pages in a given category function p.transcludeCategory(frame) -- Get the category name from the arguments local args = frame:getParent().args local categoryName = args[1] if not categoryName or categoryName == "" then return "Error: No category specified." end -- Initialize a buffer to hold the transcluded content local contentBuffer = {} -- Iterate through pages in the categ...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Documentation for this module may be created at Module:CategoryTranscluder/doc

local p = {}

-- Function to retrieve and transclude pages in a given category
function p.transcludeCategory(frame)
    -- Get the category name from the arguments
    local args = frame:getParent().args
    local categoryName = args[1]

    if not categoryName or categoryName == "" then
        return "Error: No category specified."
    end

    -- Initialize a buffer to hold the transcluded content
    local contentBuffer = {}

    -- Iterate through pages in the category
    local category = mw.site.getCategory(categoryName)
    if not category then
        return "Error: Category not found."
    end

    local count = 0
    for _, page in ipairs(category:members()) do
        count = count + 1

        -- Only transclude articles, not files or subcategories
        if page.ns == 0 then
            table.insert(contentBuffer, string.format("<includeonly>{{:%s}}</includeonly>", page.text))
        end

        -- Optional: Stop after a set limit to prevent excessive processing
        if count > 100 then
            table.insert(contentBuffer, "<!-- Output truncated to the first 100 items. -->")
            break
        end
    end

    -- Combine the transcluded content and return it
    return table.concat(contentBuffer, "\n")
end

return p