Modul:category tree/PIE root cat

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

local export = {}

-- Category object

local Category = {}
Category.__index = Category


function Category.new_main(frame)
	local pagename = mw.title.getCurrentTitle().text
	
	local langName, root = mw.ustring.match(pagename, "^(.-) ?[Tt]erms derived from the PIE root (.+)$")
	
	local lang, id
	
	if langName then
		if langName ~= "" then
			lang = require("Module:languages").getByCanonicalName(langName) or
				error('The language name "' .. langName .. '" in the page title is not valid.')
		end
		
		if mw.ustring.find(root, " %(") then
			root, id = mw.ustring.match(root, "^(.+) %((.*)%)$")
		end
		
		if id == "" then
			id = nil
		end
	end
	
	return Category.new{
		catLang = lang,
		label = root,
		id = id
	}
end


function Category.new(info)
	local validKeys = {
		catLang = true,
		code = true,
		label = true,
		id = true,
	}
	for key, val in pairs(info) do
		if not validKeys[key] then
			error("The parameter \"" .. key .. "\" was not recognized.")
		end
	end
	
	local self = setmetatable({}, Category)
	self._info = info
	self._info.lang = require("Module:languages").getByCode("ine-pro")
	
	if self._info.code or self._info.catLang then
		self._lang = self._info.catLang or require("Module:languages").getByCode(self._info.code) or error("The language code \"" .. self._info.code .. "\" is not valid.")
		if not self._info.code then
			self._info.code = self._lang:getCode()
		end
	end
	
	return self
end

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


function Category:getInfo()
	return self._info
end


function Category:getBreadcrumbName()
	if self._info.label then
		local root = require("Module:script utilities").tag_text(
			self._info.label,
			self._info.lang,
			nil,
			"term"
		)
		
		if self._info.id then
			root = root .. " (" .. self._info.id .. ")"
		end
		
		return root
	else
		return "Terms derived from Proto-Indo-European roots"
	end
end


function Category:getDataModule()
	return "Module:category tree/PIE root cat"
end


function Category:canBeEmpty()
	if self._lang then
		return false
	else
		return true
	end
end


function Category:isHidden()
	return false
end


function Category:getCategoryName()
	if self._info.label then
		local shared = " derived from the PIE root " ..
			self._info.label
	
		if self._info.id then
			shared = shared .. " (" .. self._info.id .. ")"
		end
		
		if self._lang then
			return self._lang:getCanonicalName() .. " terms" .. shared
		else
			return "Terms" .. shared
		end
	else
		return "Terms derived from Proto-Indo-European roots"
	end
end


function Category:getDescription()
	if self._info.label then
		local shared = " that originate ultimately from the [[w:Proto-Indo-European root|Proto-Indo-European root]] " ..
			 require("Module:links").full_link(
				{
					term = self._info.label,
					lang = self._info.lang,
					id = self._info.id
				},
				"term"
			) .. "."
		
		mw.getCurrentFrame():callParserFunction(
			"DISPLAYTITLE",
			require("Module:string").plain_gsub(
				"Category:" .. self:getCategoryName(),
				self._info.label,
				require("Module:script utilities").tag_text(
					self._info.label,
					self._info.lang,
					nil,
					"term"
				)
			)
		)
		
		if self._lang then
			return self._lang:getCanonicalName() .. " terms" .. shared
		else
			return "Terms" .. shared
		end
	else
		return "Categories for terms derived from particular [[w:Proto-Indo-European root|Proto-Indo-European roots]]."
	end
end


function Category:getParents()
	local parents = {}
	if self._lang then -- langname terms derived from PIE root
		table.insert(
			parents,
			{
				name = require("Module:category tree/poscatboiler")
					.new {
						code = self._info.code,
						label = "terms by PIE root",
					},
				-- PIE sortkey
				sort = self._info.lang:makeSortKey(self._info.label)
			}
		)
		table.insert(
			parents,
			{
				name = require("Module:category tree/PIE root cat")
					.new {
						code = nil,
						label = self._info.label,
						id = self._info.id
					},
				sort = self._lang:getCanonicalName()
			}
		)
	else -- terms derived from PIE root
		if self._info.label then
			table.insert(
				parents,
				{
					name = Category.new {
							code = nil,
							label = nil,
							id = nil
						},
					-- PIE sortkey
					sort = self._info.lang:makeSortKey(self._info.label)
				}
			)
		else
			table.insert(
				parents,
				{
					name = require("Module:category tree/poscatboiler")
						.new {
							code = "ine-pro",
							label = "roots",
						},
					sort = " "
				}
			)
			table.insert(
				parents,
				{
					name = require("Module:category tree/derived cat")
						.new {
							label = "ine-pro",
						},
					sort = " roots"
				}
			)
		end
	end
		
	if self._info.id then
		table.insert(
			parents,
			{
				name = Category.new {
						catLang = self._lang,
						label = self._info.label,
						id = nil
					},
				sort = " " .. self._info.id
			}
		)
	end
	
	return parents
end


function Category:getChildren()
	return nil
end


function Category:getUmbrella()
	return nil
end


return export