This module implements {{poscatboiler}}. For information on how to use the template or modify its categories, see its documentation. For the complete hierarchy, see Module:category tree/poscatboiler/hierarchy.

Subpages

baguhon

Plantilya:subpages


local export = {}

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

-- Category object

local Category = {}
Category.__index = Category

function Category.new_main(frame)
	local self = setmetatable({}, Category)
	
	local params = {
		[1] = {},
		[2] = {required = true},
		["sc"] = {},
	}
	
	args = require("Module:parameters").process(frame:getParent().args, params)
	self._info = {code = args[1], label = args[2], sc = args["sc"]}
	
	self:initCommon()
	
	if not self._data then
		return nil
	end
	
	return self
end

function Category.new(info)
	for key, val in pairs(info) do
		if not (key == "code" or key == "label" or key == "sc" or key == "no_catfix") then
			error("The parameter \"" .. key .. "\" was not recognized.")
		end
	end
	
	local self = setmetatable({}, Category)
	self._info = info
	
	if not self._info.label then
		error("No label was specified.")
	end
	
	self:initCommon()
	
	if not self._data then
		error("The label \"" .. self._info.label .. "\" does not exist.")
	end
	
	return self
end

export.new = Category.new
export.new_main = Category.new_main


function Category:initCommon()
	if self._info.code then
		self._lang = require("Module:languages").getByCode(self._info.code) or require("Module:languages").err(self._info.code, 1)
		if self._info.label:find('^redlink') then
			self._info.no_catfix = true
		end
	else
		self._lang = nil
	end
	
	if self._info.sc then
		self._sc = require("Module:scripts").getByCode(self._info.sc) or error("The script code \"" .. self._info.sc .. "\" is not valid.")
	else
		self._sc = nil
	end
	
	-- Check if the label exists
	self._data = labels[self._info.label]
	
	if not self._data then
		-- Check regex
		for pattern, data in pairs(labels) do
			if data.regex then
				if mw.ustring.match(self._info.label, pattern) then
					self._data = data
				end
			end
		end
	end
	
	-- Umbrella categories cannot have a script
	if self._sc and not self._lang then
		error("Umbrella categories cannot have a script specified.")
	end
end


function Category:getBreadcrumbName()
	local ret = self._info.label
	
	if ( ret == "modules" or ret == "templates" ) and not self._lang then
		ret = "by language"
	end
	
	if self._sc then
		ret = ret .. " in " .. self._sc:getCategoryName()
	end
	
	return ret
end


function Category:getInfo()
	return self._info
end


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


function Category:canBeEmpty()
	if not self._lang then
		return true
	else
		return self._data["can_be_empty"]
	end
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(isChild)
	-- Allows different text in the list of a category's children
	local isChild = isChild == "child"
	
	if self._lang then
		if self._sc then
			return self:getCategoryName() .. "."
		else
			local ret = self._data["description"]
			
			if not isChild and self._data.additional then
				ret = ret .. " " .. self._data.additional
			end
			
			if ret then
				if self._data.regex and type(ret) == "function" then
					ret = ret(self._info.label)
				end
				ret = ret:gsub("{{PAGENAME}}", mw.title.getCurrentTitle().text)
				ret = ret:gsub("{{{langname}}}", self._lang:getCanonicalName())
				ret = ret:gsub("{{{langcat}}}", 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 {{name = parent, sort = self._sc:getCanonicalName()}}
		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 = {name = parent}
				end
				
				if not parent.sort then
					parent.sort = self._info.label
				end
				
				if self._data.regex then
					if type(parent.name) == "function" then
						parent.name = parent.name(self._info.label)
					end
					
					if type(parent.sort) == "function" then
						parent.sort = parent.sort(self._info.label)
					end
				end
				
				parent.sort = parent.sort:gsub("{{{langname}}}", self._lang:getCanonicalName())
				parent.sort = parent.sort:gsub("{{{langcat}}}", self._lang:getCategoryName())
				
				if parent.name and parent.name:find("^Category:") then
					parent.name = parent.name:gsub("{{{langname}}}", self._lang:getCanonicalName())
					parent.name = parent.name:gsub("{{{langcat}}}", self._lang:getCategoryName())
				else
					local pinfo = mw.clone(self._info)
					pinfo.label = parent.name
					pinfo.no_catfix = nil
					
					if parent.template then
						parent.name = require("Module:category tree/" .. parent.template).new(pinfo)
					else
						parent.name = Category.new(pinfo)
					end
				end
				
				table.insert(ret, parent)
			end
			
			return ret
		end
	else
		if self._data["fundamental"] then
			local fundamental = self._data["fundamental"]
			local sortparentumbrella = self._data["sortparentumbrella"]
			
			if self._data.regex then
				if type(fundamental) == "function" then
					fundamental = fundamental(self._info.label)
				end
				if type(sortparentumbrella) == "function" then
					sortparentumbrella = sortparentumbrella(self._info.label)
				end
			end
			
			return {{name = "Category:" .. fundamental, sort = sortparentumbrella or self._info.label}}
		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}
		end
		
		if not child.sort then
			child.sort = child.name
		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


function Category:getAppendix()
	if self._info.label and self._lang then
		local appendixName = "Appendix:" .. self._lang:getCanonicalName() .. " " .. self._info.label
		local appendix = mw.title.new(appendixName).exists
		if appendix then
			return appendixName
		else
			return nil
		end
	else
		return nil
	end
end


return export