Modul:category tree/phrasebook cat

A modult a Modul:category tree/phrasebook cat/doc lapon tudod dokumentálni

local export = {}

local labels = require("Module:category tree/phrasebook cat/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._lang = require("Module:languages").getByCode(val) or error("The language 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 ~= "phrasebook" and not self._lang then
		error("Phrasebook subcategories do not have umbrella categories.")
	end
	
	return self
end

export.new = Category.new


function Category:getInfo()
	return self._info
end


function Category:getBreadcrumbName()
	if self._lang then
		return self._info.label:gsub(".+/([^/]+)$", "%1")
	else
		return "phrasebooks"
	end
end


function Category:getDataModule()
	return "Module:category tree/phrasebook cat/data"
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
		return mw.getContentLanguage():ucfirst(self._lang:getCanonicalName() .. " " .. self._info.label)
	else
		return "Phrasebooks by language"
	end
end


function Category:getDescription()
	if self._lang then
		local ret = self._data["description"]
		
		if ret then
			ret = ret:gsub("{{{langname}}}", self._lang:getCanonicalName())
			ret = ret:gsub("{{{langcat}}}", self._lang:getCategoryName())
		end
		
		return ret
	else
		return "Categories with phrasebooks in various specific languages."
	end
end


function Category:getParents()
	if self._lang 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 = {name = parent}
			end
			
			if not parent.sort then
				parent.sort = self._info.label:gsub(".+/([^/]+)$", "%1")
			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
				
				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
	else
		return nil
	end
end


function Category:getChildren()
	return nil
end


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


return export