Modul:Ceribandin/Balyozxane/kategorî/kes

Belgekirina modulê[biafirîne]
local export = {}

local label_data = require("Modul:Ceribandin/Balyozxane/kes/dane")

-- Category object

local Category = {}
Category.__index = Category

function Category.new_main(frame)
	local self = setmetatable({}, Category)
	
	local params = {
		[1] = {},
		[2] = {required = true},
	}
	
	args = require("Module:parameters2").process(frame:getParent().args, params)
	self._info = {label = args[1], sal = args[2]}
	
	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 == "sal" or key == "label") then
			error("Parametreya “" .. key .. "” nayê zanîn.")
		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("Modul:ziman").getByCode(self._info.code) or
			error("The language code “" .. self._info.code .. "” is not valid.")
	end
]=]--	
	-- Convert label to lowercase if possible
	local lowercase_label = mw.getContentLanguage():lcfirst(self._info.label)
	
	-- Check if the label exists
	local labels = label_data["LABELS"]

	if labels[lowercase_label] then
		self._info.label = lowercase_label
	end
	
	self._data = labels[self._info.label]
	
	-- Go through handlers
	if not self._data then
		for _, handler in ipairs(label_data["HANDLERS"]) do
			self._data = handler.handler(self._info.label)
			if self._data then
				self._data.edit = handler.module
				break
			end
		end
	end
end


function Category:getInfo()
	return self._info
end


function Category:getBreadcrumbName()
	return self._info.label
end


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


function Category:canBeEmpty()
	return true
end


function Category:isHidden()
	return false

--[=[	if self._info.label == "daxwazên skrîpta peyvê" or self._info.label == "daxwazên peyvan"  or self._info.label == "werger" then
		return true
	else 
		return false
	end
 ]=]--	
end


function Category:getCategoryName()
	if self._info.sal then
		return mw.getContentLanguage():ucfirst(self._info.label) .. self._info.sal
	else
		return mw.getContentLanguage():ucfirst(self._info.label)
	end
end


local function replace_special_descriptions(desc)
	-- TODO: Should probably find a better way to do this
	local descriptionFormats = {
		["default"]					= "Peyvên {{{langname}}} têkildarî {{{label_lc}}}.",
		["default with capital"]	= "Peyvên {{{langname}}} têkildarî {{{label_uc}}}.",
		["default with the"]		= "{{{langname}}} terms related to the {{{label_uc}}}.",
		["default with the lower"]	= "{{{langname}}} terms related to the {{{label_lc}}}.",
		["default with topic"]		= "{{{langname}}} terms related to {{{label_lc}}} topics.",
		["default-set"]				= "{{{langname}}} terms for various {{{label_lc}}}.",
	}
	
	return descriptionFormats[desc] or desc
end


local function expand_description(desc, cat)
	if cat._lang then
		desc =  desc:gsub("{{{langname}}}", cat._lang:getCanonicalName())
		desc =  desc:gsub("{{{langcode}}}", cat._lang:getCode())
		desc =  "<strong style=\"font-size:larger; font-weight:normal;\"><i>" .. desc:gsub("{{{langcat}}}", cat._lang:getCategoryName()) .. "</i></strong>"
	end
	
	if desc:find("{{{label_uc}}}") then
		local label_entry_name = mw.getContentLanguage():ucfirst(cat._info.label)
		local label_entry = mw.title.new(label_entry_name)
		
		if label_entry.exists then
			desc = desc:gsub("{{{label_uc}}}", "[[" .. label_entry_name .. "]]")
		else
			desc = desc:gsub("{{{label_uc}}}", label_entry_name)
		end
	end
	
	if desc:find("{{{label_lc}}}") then
		local label_entry_name = mw.getContentLanguage():lcfirst(cat._info.label)
		local label_entry = mw.title.new(label_entry_name)
		
		if label_entry.exists then
			desc = desc:gsub("{{{label_lc}}}", "[[" .. label_entry_name .. "]]")
		else
			desc = desc:gsub("{{{label_lc}}}", label_entry_name)
		end
	end
	
	return desc
end


function Category:getDescription()
	if self._lang then
		local desc = self._data["description"]

		desc = replace_special_descriptions(desc)
		if desc then
			return expand_description(desc, self)
		end
	else
		if not self._lang and self._info.label == "lîsteya ferhengan" then
			return "Lîsteya ferhengan li gorî ziman û mijaran."
		elseif not self._lang and self._info.label == "rêziman" then
			return "Lîsteya hemû kategoriyên rêzimanê li gorî beşa rêzimanî û zimanan."
		elseif not self._lang and self._info.label == "deng" then
			return "Lîsteya kategoriyên zimanên bi dosyaya dengî."
			end

		local eninfo = mw.clone(self._info)
		eninfo.code = "ku"
		local ku = Category.new(eninfo)

		local desc = self._data["description"]
		desc = replace_special_descriptions(desc)
		if desc then
			desc = desc:gsub("^{{{langname}}} ", "")
			desc = desc:gsub("^{{{langcode}}} ", "")
			desc = desc:gsub("^{{{langcat}}} ", "")
			desc = desc:gsub("%.$", "")
			desc = expand_description(desc, self)
		else
			desc = self._info.label
		end

		return
			"<i>" .. mw.getContentLanguage():ucfirst(self._info.label) .. " </i>mijara sereke ya vê kategoriyê ye. \n\n" ..
			"Du cure binkategorî di vê kategoriyê de hene:\n\n" ..
			"* Kategoriyên bi vî rengî: “" .. mw.getContentLanguage():ucfirst(self._info.label) .. " bi almanî” (yên bi navê zimanekî xilas dibin) li ser têgihên taybet yên bi vî zimanî ne. " ..
			"Ji bo kategoriya kurdî binêre [[:Kategorî:" .. ku:getCategoryName() .. "]].\n" ..
			"* Binkategoriyên bêyî navê zimanekî ji bo binkategoriyên vê kategoriyê ne lê ji bo mijarên taybet in."
	end
end


function Category:getParents()
	local parents = self._data["parents"]
	
	if not self._lang and ( self._info.label == "lîsteya ferhengan" or self._info.label == "rêziman" or self._info.label == "deng" ) then
		return {{ name = "Kategorî:!Serkategorî", sort = self._info.label:gsub("all ", "") }}
	end
	
	if not parents or #parents == 0 then
		return nil
	end
	
	local ret = {}
	local is_set = false
	
	if self._info.label == "rêziman" then
		is_set = true
	end
	
	for key, parent in ipairs(parents) do
		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._lang then
			parent.sort = parent.sort:gsub("{{{langname}}}", self._lang:getCanonicalName())
			parent.sort = parent.sort:gsub("{{{langcat}}}", self._lang:getCategoryName())
		elseif parent.sort:find("{{{langname}}}") or parent.sort:find("{{{langcat}}}") or parent.template == "zimankatbike" then
			return nil
		end
		
		if not self._lang then
			parent.sort = " " .. parent.sort
		end
		
		if parent.name and parent.name:find("^Kategorî:") then
			if self._lang then
				parent.name = parent.name:gsub("{{{langname}}}", self._lang:getCanonicalName())
				parent.name = parent.name:gsub("{{{langcat}}}", self._lang:getCategoryName())
			elseif parent.name:find("{{{langname}}}") or parent.name:find("{{{langcat}}}") or parent.template == "zimankatbike" then
				return nil
			end
		else
			if parent.name == "lîsteya ferhengan" then
				is_set = true
			end
			
			local pinfo = mw.clone(self._info)
			pinfo.label = parent.name
			
			if parent.template then
				parent.name = require("Modul:dara kategoriyan/" .. parent.template).new(pinfo)
			else
				parent.name = Category.new(pinfo)
			end
		end
		
		table.insert(ret, parent)
	end
	
--[[	if not is_set and self._info.label ~= "lîsteya ferhengên" and self._info.label ~= "list of sets" then
		local pinfo = mw.clone(self._info)
		pinfo.label = "lîsteya ferhengên"
		table.insert(ret, {name = Category.new(pinfo), sort = (not self._lang and " " or "") .. self._info.label})
	end --]]
	
	return ret
end


function Category:getChildren()
	return nil
end


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


return export