Module:Category tree/poscatboiler

local export = {}

local labels = require("Module:category tree/poscatboiler/data")

-- Category object

local Category = {} Category.__index = Category

function Category.new(info, auto) local self = setmetatable({}, Category) assert(type(info) == "table", "The \"info\" parameter must be a table.") self._info = {} for key, val in pairs(info) do		if key == "code" then self._info.code = val self._lang = require("Module:languages").getByCode(val) or error("The language code \"" .. val .. "\" is not valid.") elseif key == "label" then self._info.label = val elseif key == "sc" then self._info.sc = val self._sc = require("Module:scripts").getByCode(val) or error("The script code \"" .. val .. "\" is not valid.") else error("The parameter \"" .. key .. "\" was not recognized.") end end -- Was no label given? if not self._info.label then if auto then -- Try to extract the label from the page name. local pagename = mw.title.getCurrentTitle.text local prefix = self._lang:getCanonicalName .. " "			if mw.ustring.sub(pagename, 1, mw.ustring.len(prefix)) ~= prefix then return nil end self._info.label = mw.ustring.sub(pagename, mw.ustring.len(prefix) + 1) else error("No label was specified.") end end -- Check if the label exists self._data = labels[self._info.label] if not self._data then return nil end -- Umbrella categories cannot have a script if self._sc and not self._lang then error("Umbrella categories cannot have a script specified.") end return self end

export.new = Category.new

function Category:getBreadcrumbName local ret = self._info.label if self._sc then ret = ret .. " in " .. self._sc:getCategoryName end return ret end

function Category:getDataModule return self._data["edit"] end

function Category:canBeEmpty return self._data["can_be_empty"] end

function Category:isHidden return self._data["hidden"] and self._info.code end

function Category:getCategoryName if self._lang then local ret = self._lang:getCanonicalName .. " " .. self._info.label if self._sc then ret = ret .. " in " .. self._sc:getCategoryName end return mw.getContentLanguage:ucfirst(ret) else return mw.getContentLanguage:ucfirst(self._info.label .. " by language") end end

function Category:getDescription if self._lang then if self._sc then return self:getCategoryName .. "."		else local ret = self._data["description"] if ret then ret = ret:gsub("", mw.title.getCurrentTitle.text) ret = ret:gsub("", self._lang:getCanonicalName) ret = ret:gsub("", self._lang:getCategoryName) end return ret end else return "Categories with " .. self._info.label .. " in various specific languages." end end

function Category:getParents if self._lang then if self._sc then local pinfo = mw.clone(self._info) pinfo.sc = nil local parent = Category.new(pinfo) return else local parents = self._data["parents"] if not parents or #parents == 0 then return nil end local ret = {} for _, parent in ipairs(parents) do				local parent = mw.clone(parent) if type(parent) == "table" then parent.sort = parent.sort:gsub("", self._lang:getCanonicalName) parent.sort = parent.sort:gsub("", self._lang:getCategoryName) else parent = {name = parent, sort = self._info.label} end if parent.name:find("^Category:") then parent.name = parent.name:gsub("", self._lang:getCanonicalName) parent.name = parent.name:gsub("", self._lang:getCategoryName) else local pinfo = mw.clone(self._info) pinfo.label = parent.name parent.name = Category.new(pinfo) end table.insert(ret, parent) end return ret end else if self._data["fundamental"] then return else return nil end end end

function Category:getChildren local children = self._data["children"] if not self._lang or not children or #children == 0 then return nil end local ret = {} for _, child in ipairs(children) do		child = mw.clone(child) if type(child) ~= "table" then child = {name = child, sort = child} end local cinfo = mw.clone(self._info) cinfo.label = child.name child.name = Category.new(cinfo) table.insert(ret, child) end return ret end

function Category:getUmbrella if not self._lang or self._sc then return nil end local uinfo = mw.clone(self._info) uinfo.code = nil return Category.new(uinfo) end

return export