انتقل إلى المحتوى

وحدة:category tree/scriptcatboiler

من ويكاموس، القاموس الحر
local export = {}

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


-- Category object

local Category = {}
Category.__index = Category


function Category.new(info)
	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._sc = require("Module:scripts").getByCode(val) or error("The script code \"" .. val .. "\" is not valid.")
		elseif key == "label" then
			self._info.label = val
			self._data = labels[self._info.label]
		else
			error("The parameter \"" .. key .. "\" was not recognized.")
		end
	end
	
	-- Check if the label exists
	if not self._info.label then
		error("No label was specified.")
	elseif not self._data then
		return nil
	end
	
	if self._info.label == "ROOT" and not self._sc then
		return nil
	end
	
	return self
end

export.new = Category.new


function Category:getBreadcrumbName()
	if self._info.label == "ROOT" then
		return self._sc:getCategoryName()
	else
		return self._info.label
	end
end


function Category:getDataModule()
	return "Module:category tree/scriptcatboiler/data"
end


function Category:canBeEmpty()
	return self._info.label == "ROOT"
end


function Category:getCategoryName()
	if self._sc then
		local ret = nil
		
		if self._info.label ~= "ROOT" then
			ret = " " .. self._info.label
		end
		
		return mw.getContentLanguage():ucfirst(self._sc:getCategoryName() .. (ret or ""))
	else
		return mw.getContentLanguage():ucfirst(self._info.label .. " by script")
	end
end


function Category:getDescription()
	if self._sc then
		local ret = self._data["description"]
			
		if ret then
			if ret:find("{{{code}}}") then ret = ret:gsub("{{{code}}}", self._sc:getCode()) end
			if ret:find("{{{scname}}}") then ret = ret:gsub("{{{scname}}}", self._sc:getCanonicalName()) end
			if ret:find("{{{sccat}}}") then ret = ret:gsub("{{{sccat}}}", self._sc:getCategoryName()) end
		end
		
		return ret
	else
		return "Categories with " .. self._info.label .. " of various specific scripts."
	end
end


function Category:getParents()
	if self._sc then
		local parents = self._data["parents"]
		
		if not parents or #parents == 0 then
			return nil
		end
		
		local ret = {}
		
		for key, parent in ipairs(parents) do
			local parent = mw.clone(parent)
			
			if type(parent) == "table" then
				parent.sort = parent.sort:gsub("{{{scname}}}", self._sc:getCanonicalName())
				parent.sort = parent.sort:gsub("{{{sccat}}}", self._sc:getCategoryName())
			else
				parent = {name = parent, sort = self._info.label}
			end
			
			if parent.name:find("^Category:") then
				parent.name = parent.name:gsub("{{{scname}}}", self._sc:getCanonicalName())
				parent.name = parent.name:gsub("{{{sccat}}}", self._sc: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
	else
		if self._data["fundamental"] then
			return {{name = "Category:" .. self._data["fundamental"], sort = self._data["sortparentumbrella"] or self._info.label}}
		else
			return nil
		end
	end
end


function Category:getChildren()
	local children = self._data["children"]
	
	if not self._sc 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._sc then
		return nil
	end
	
	local uinfo = mw.clone(self._info)
	uinfo.code = nil
	return Category.new(uinfo)
end


return export