Modul:category tree/topic cat

Dissen Vörlaag hef dokumentatsie neudig.
Maak de dokumentatsie veur dissen modul ovver t doel en gebroek op de dokumentatsieziede.

local export = {}

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

-- Category object

local Category = {}
Category.__index = Category

local valid_parameters = {
	code = true,
	label = true,
}

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("De spraokkode \"" .. val .. "\" is neet geldig.")
		elseif key == "label" then
		        self._info.label = val
			self._data = labels[self._info.label]
		else
			error("De parameter \"" .. key .. "\" was neet herkend.")
		end
		self._info[key] = val
	end
	
	assert(self._info.label and type(self._info.label) == "string", "De \"info\" tabel mot een \"label\" string weerde hebben.")

        if not self._info.label then
            error("Gin label op-egovven.")
        elseif not self._data then
            return nil
         end

        return self
end

export.new = Category.new


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


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

function Category:canBeEmpty()
	return false
end

function Category:getCategoryName()
	if self._info.code then
		local lang = require("Module:languages").getByCode(self._info.code)
		return lang:getCode() .. ":" .. mw.getContentLanguage():ucfirst(self._info.label)
	else
		return mw.getContentLanguage():ucfirst(self._info.label)
	end
end


function Category:getDescription()
	if self._info.code then
		local lang = require("Module:languages").getByCode(self._info.code)
		local ret = self._data["description"]

		-- TODO: Should probably find a better way to do this
		if ret == "default" then
			ret = "{{{langname}}} Begreppen, de mit {{{label_lc}}} to doon heff."
		elseif ret == "default with capital" then
			ret = "{{{langname}}} Begreppen, de mit {{{label_uc}}} to doon heff."
		elseif ret == "default with the" then
			ret = "{{{langname}}} Begreppen, de mit de {{{label_uc}}} to doon heff."
		elseif ret == "default with the lower" then
			ret = "{{{langname}}} terms related to the {{{label_lc}}}."
		elseif ret == "default with topic" then
			ret = "{{{langname}}} terms related to {{{label_lc}}} topics."
		end
		
		if ret then
			ret = ret:gsub("{{{langname}}}", lang:getCanonicalName())
			ret = ret:gsub("{{{langcat}}}", lang:getCategoryName())
			
			if ret:find("{{{label_uc}}}") then
				local label_entry_name = mw.getContentLanguage():ucfirst(self._info.label)
				local label_entry = mw.title.new(label_entry_name)
				
				if label_entry.exists then
					ret = ret:gsub("{{{label_uc}}}", "[[" .. label_entry_name .. "]]")
				else
					ret = ret:gsub("{{{label_uc}}}", label_entry_name)
				end
			end
			
			if ret:find("{{{label_lc}}}") then
				local label_entry_name = mw.getContentLanguage():lcfirst(self._info.label)
				local label_entry = mw.title.new(label_entry_name)
				
				if label_entry.exists then
					ret = ret:gsub("{{{label_lc}}}", "[[" .. label_entry_name .. "]]")
				else
					ret = ret:gsub("{{{label_lc}}}", label_entry_name)
				end
			end
		end
	
		return ret
	else
		local eninfo = mw.clone(self._info)
		eninfo.code = "en"
		local en = Category.new(eninfo)
		local ndsnlinfo = mw.clone(self._info)
		ndsnlinfo.code = "nds-nl"
		local nds_nl = Category.new(ndsnlinfo)	
		local ndsdeinfo = mw.clone(self._info)
		ndsdeinfo.code = "nds-de"
		local nds_de = Category.new(ndsdeinfo)	

		return
"Disse kategorie bevat allenig aandere kategorieën, gin weurdenbookdefinities. De oonderkategorieë gaot ovver t onderwarp: " .. self._info.label ..  ". T gif twee soorten:\n\n" ..
"* Onderkategorien met namen as \"aa:" .. self._info.label .. "\" (met ne spraokkode as veurvoogsel) bunt kategorien van weurde in bepaolde spraoken; Wellicht bu'j t meest interesseerd in [[:Kategorie:" .. nds_nl:getCategoryName() .. "]] en [[:Kategorie:" .. nds_de:getCategoryName() .. "]], för nedersaksiese en plattdüütsch begripp. \n" ..
"* Ünnerkategorien zo as dissen, maor dan zonder spraokkodeveurvoogsel bunt wietere verfieningen van disse kategorie."
	end
end


function Category:getBild()
	local Bild = self._data["Bild"]
	
	if Bild then
		return 
			"[[Image:" .. Bild .. "|left|100px]]"
    else
		return
    end
end


function Category:getParents()
	local lang = (self._info.code and require("Module:languages").getByCode(self._info.code) or nil)
	
	local parents = self._data["parents"]
	
	if not parents or #parents == 0 then
		return nil
	end
	
	local ret = {}
	
	for key, parent in ipairs(parents) do
		parent = mw.clone(parent)
		
		if type(parent) == "table" then
			if self._info.code then
				parent.sort = parent.sort:gsub("{{{langname}}}", lang:getCanonicalName())
				parent.sort = parent.sort:gsub("{{{langcat}}}", lang:getCategoryName())
			elseif parent.sort:find("{{{langname}}}") or parent.sort:find("{{{langcat}}}") then
				return nil
			end
		else
			parent = {name = parent, sort = self._info.label}
		end
		
		if not self._info.code then
			parent.sort = "*" .. parent.sort
		end
		
		if parent.name:find("^Kategorie:") then
			if self._info.code then
				parent.name = parent.name:gsub("{{{langname}}}", lang:getCanonicalName())
				parent.name = parent.name:gsub("{{{langcat}}}", lang:getCategoryName())
			elseif parent.name:find("{{{langname}}}") or parent.name:find("{{{langcat}}}") then
				return nil
			end
		else
			local pinfo = mw.clone(self._info)
			pinfo.label = parent.name
			parent.name = Category.new(pinfo)
		end
		
		table.insert(ret, parent)
	end
	
	if self._info.label ~= "List vun Themen" then
	      local pinfo = mw.clone(self._info)
	      pinfo.label = "List vun Themen"
	      table.insert(ret, {name = Category.new(pinfo), sort = (not self._info.code and "*" or "") .. self._info.label})
	end
	
	return ret
end


function Category:getChildren()
	return nil
end


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


return export