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

وحدة:category tree/affix cat

من ويكاموس، القاموس الحر
local m_links = require("Module:links")
local m_compound = require("Module:compound")

local export = {}

-- 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 == "affixtype" then
			self._info.affixtype = val
		elseif key == "alt" then
			self._info.alt = val
			self._alt = val
		elseif 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 == "pos" then
			self._info.pos = 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.")
		elseif key == "sort" then
			self._info.sort = val
		elseif key == "term" then
			self._info.term = val
			self._term = val
		elseif key == "tr" then
			self._info.tr = val
			self._tr = val
		else
			error("The parameter \"" .. key .. "\" was not recognized.")
		end
	end
	
	if not self._lang then
		error("No language code was specified.")
	end
	
	if not self._term then
		error("No term was specified.")
	end
	
	-- Convert term/alt into affixes if needed
	if self._info.affixtype == "prefix" then
		self._desc = "beginning with the prefix"
	elseif self._info.affixtype == "suffix" then
		self._desc = "ending with the suffix"
	elseif self._info.affixtype == "circumfix" then
		self._desc = "bookended with the circumfix"
	elseif self._info.affixtype == "infix" then
		self._desc = "spliced with the infix"
	elseif self._info.affixtype == "interfix" then
		self._desc = "joined with the interfix"
	else
		error("Invalid affixtype specified.")
	end
	
	self._term = m_compound.make_affix(self._term, self._lang, self._sc, self._info.affixtype)
	self._alt = m_compound.make_affix(self._alt, self._lang, self._sc, self._info.affixtype)
	self._tr = m_compound.make_affix(self._tr, self._lang, require("Module:scripts").getByCode("Latn"), self._info.affixtype)
	
	-- Make pos plural
	self._pos = self._info.pos or "word"
	
	if self._pos:find("[sx]$") then
		self._pos = self._pos .. "es"
	else
		self._pos = self._pos .. "s"
	end
	
	return self
end

export.new = Category.new


function Category:getBreadcrumbName()
	local link = m_links.full_link(nil, self._alt or self._term, self._lang, self._sc, "term", nil, {tr = "-"}, false)
	return self._pos .. " " .. self._info.affixtype .. "ed with " .. link
end


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


function Category:canBeEmpty()
	return false
end


function Category:getCategoryName()
	return self._lang:getCanonicalName() .. " " .. self._pos .. " " .. self._info.affixtype .. "ed with " .. self._lang:makeEntryName(self._term)
end


function Category:getDescription()
	local description =
		self._lang:getCanonicalName() .. " " .. self._pos .. " " .. self._desc .. " " ..
		m_links.full_link(self._term, self._alt, self._lang, self._sc, "term", nil, {tr = self._tr}, false) ..
		"."
	
	local displaytitle =
		"Category:" .. self._lang:getCanonicalName() .. " " .. self._pos .. " " .. self._info.affixtype .. "ed with " ..
		m_links.full_link(nil, self._alt or self._term, self._lang, self._sc, "term", nil, {tr = "-"}, false)
	
	return mw.getCurrentFrame():callParserFunction("DISPLAYTITLE", displaytitle) .. description
end


function Category:getParents()
	return {{name = "Category:" .. self._lang:getCanonicalName() .. " words by " .. self._info.affixtype, sort = self._lang:makeSortKey(self._info.sort or self._term)}}
end


function Category:getChildren()
	return nil
end


function Category:getUmbrella()
	return nil
end


return export