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

Module:CatType: Difference between revisions

From Teriock
Content deleted Content added
No edit summary
No edit summary
Line 28: Line 28:
end
end


-- Extract categories from the parent category's content
-- Split the content into lines
local lines = mw.text.split(parentContent, "\n")
local categories = {}
local categories = {}

for category in parentContent:gmatch("%[%[Category:([^%]]+)%]%]") do
-- Iterate over each line to find category links
-- Replace "abilities" with the detected type in each category
for _, line in ipairs(lines) do
local updatedCategory = category:gsub("abilities", type)
table.insert(categories, "[[Category:" .. updatedCategory .. "]]")
if mw.ustring.find(line, "%[%[Category:") then
-- Extract the category name
local categoryName = mw.ustring.match(line, "%[%[Category:([^|%]]+)")
if categoryName then
-- Replace "abilities" with the detected type in the category name
local updatedCategory = categoryName:gsub("abilities", type)
table.insert(categories, "[[Category:" .. updatedCategory .. "]]")
end
end
end
end



Revision as of 13:19, 12 January 2025

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

local p = {}

function p.generateCategories(frame)
    local baseCategory = frame.args[1] or ""

    if baseCategory == "" then
        return "Error: No base category provided."
    end

    -- Determine the type based on the base category name
    local type = baseCategory:match("spells") or baseCategory:match("skills")
    if not type then
        return "Error: Unable to determine type ('spells' or 'skills') from category name."
    end

    -- Determine the parent category by replacing the detected type with "abilities"
    local parentCategory = baseCategory:gsub(type, "abilities")

    -- Attempt to retrieve the content of the parent category
    local parentPage = mw.title.new(parentCategory)
    if not parentPage then
        return "Error: Unable to access the parent category page object: " .. parentCategory
    end

    local parentContent = parentPage:getContent()
    if not parentContent then
        return "Error: Unable to retrieve the content of the parent category: " .. parentCategory
    end

    -- Split the content into lines
    local lines = mw.text.split(parentContent, "\n")
    local categories = {}

    -- Iterate over each line to find category links
    for _, line in ipairs(lines) do
        if mw.ustring.find(line, "%[%[Category:") then
            -- Extract the category name
            local categoryName = mw.ustring.match(line, "%[%[Category:([^|%]]+)")
            if categoryName then
                -- Replace "abilities" with the detected type in the category name
                local updatedCategory = categoryName:gsub("abilities", type)
                table.insert(categories, "[[Category:" .. updatedCategory .. "]]")
            end
        end
    end

    -- Return the updated categories as a newline-separated string
    return table.concat(categories, "\n")
end

return p