More actions
Content deleted Content added
Created page with "local p = {} function p.generateCategories(frame) local baseCategory = frame.args[1] or "" if baseCategory == "" then return "Error: No base category provided." end -- Determine type (either "spells" or "skills") 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 corresponding..." |
No edit summary |
||
(15 intermediate revisions by the same user not shown) | |||
Line 8: | Line 8: | ||
end |
end |
||
-- Determine type |
-- Determine the type based on the base category name |
||
local type = baseCategory:match("spells") or baseCategory:match("skills") |
local type = baseCategory:match("spells") or baseCategory:match("skills") |
||
if not type then |
if not type then |
||
-- If it's not spells or skills, check if it's an abilities page |
|||
if baseCategory:match("abilities$") then |
|||
-- Extract the last two words of the base category name |
|||
⚫ | |||
for word in baseCategory:gmatch("%w+") do |
|||
table.insert(words, word) |
|||
end |
|||
if #words >= 2 then |
|||
local lastTwoWords = words[#words - 1] .. " " .. words[#words] |
|||
local firstWordCapitalized = lastTwoWords:gsub("^(%w)", string.upper) |
|||
local relatedCategory = "Category:" .. firstWordCapitalized |
|||
-- Check if the related category exists |
|||
local relatedPage = mw.title.new(relatedCategory) |
|||
if relatedPage and relatedPage.exists then |
|||
return "[[" .. relatedCategory .. "]]" |
|||
end |
|||
end |
|||
return "Warning: Unable to determine related category or it doesn't exist." |
|||
else |
|||
return "Error: Unable to determine type ('spells', 'skills', or 'abilities') from category name." |
|||
end |
|||
end |
end |
||
-- Determine the |
-- Determine the parent category by replacing the detected type with "abilities" |
||
local |
local parentCategory = "Category:" .. baseCategory:gsub(type, "abilities") |
||
local abilitiesParents = mw.site.categories[abilitiesCategory] or {} |
|||
-- Attempt to retrieve and expand the content of the parent category |
|||
⚫ | |||
local parentPage = mw.title.new(parentCategory) |
|||
-- Add the immediate parent (abilities category) |
|||
if not parentPage then |
|||
table.insert(result, "[[Category:" .. abilitiesCategory .. "]]") |
|||
return "Error: Unable to access the parent category page object: " .. parentCategory |
|||
end |
|||
local rawParentContent = parentPage:getContent() |
|||
-- Add parent categories, replacing "abilities" with the detected type |
|||
if not rawParentContent then |
|||
for _, parent in ipairs(abilitiesParents) do |
|||
return "Error: Unable to retrieve the content of the parent category: " .. parentCategory |
|||
local replacement = parent:gsub("abilities", type) |
|||
table.insert(result, "[[Category:" .. replacement .. "]]") |
|||
end |
end |
||
-- local tweakedParentContent = rawParentContent:gsub("{{CatType}}", "{{CatType2}}") |
|||
-- Expand any templates in the parent category's content |
|||
local expandedParentContent = frame:preprocess(rawParentContent) |
|||
-- Replace every instance of "abilities]]" with the detected type (e.g., "spells]]" or "skills]]") |
|||
local updatedContent = expandedParentContent:gsub("Abilities%]%]", type:gsub("spells", "Spells"):gsub("skills", "Skills") .. "]]") |
|||
updatedContent = updatedContent:gsub("abilities%]%]", type .. "]]") |
|||
-- Return the updated content along with the parent category |
|||
return table.concat(result, "\n") |
|||
return "[[" .. parentCategory .. "]]\n" .. updatedContent |
|||
end |
end |
||
Latest revision as of 13:57, 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
-- If it's not spells or skills, check if it's an abilities page
if baseCategory:match("abilities$") then
-- Extract the last two words of the base category name
local words = {}
for word in baseCategory:gmatch("%w+") do
table.insert(words, word)
end
if #words >= 2 then
local lastTwoWords = words[#words - 1] .. " " .. words[#words]
local firstWordCapitalized = lastTwoWords:gsub("^(%w)", string.upper)
local relatedCategory = "Category:" .. firstWordCapitalized
-- Check if the related category exists
local relatedPage = mw.title.new(relatedCategory)
if relatedPage and relatedPage.exists then
return "[[" .. relatedCategory .. "]]"
end
end
return "Warning: Unable to determine related category or it doesn't exist."
else
return "Error: Unable to determine type ('spells', 'skills', or 'abilities') from category name."
end
end
-- Determine the parent category by replacing the detected type with "abilities"
local parentCategory = "Category:" .. baseCategory:gsub(type, "abilities")
-- Attempt to retrieve and expand 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 rawParentContent = parentPage:getContent()
if not rawParentContent then
return "Error: Unable to retrieve the content of the parent category: " .. parentCategory
end
-- local tweakedParentContent = rawParentContent:gsub("{{CatType}}", "{{CatType2}}")
-- Expand any templates in the parent category's content
local expandedParentContent = frame:preprocess(rawParentContent)
-- Replace every instance of "abilities]]" with the detected type (e.g., "spells]]" or "skills]]")
local updatedContent = expandedParentContent:gsub("Abilities%]%]", type:gsub("spells", "Spells"):gsub("skills", "Skills") .. "]]")
updatedContent = updatedContent:gsub("abilities%]%]", type .. "]]")
-- Return the updated content along with the parent category
return "[[" .. parentCategory .. "]]\n" .. updatedContent
end
return p