Belgekirina modulê[nîşan bide] [biguhêre] [rojane bike]
Ev belge ji Modul:etîmolojî/belge hatiye girtin.
local export = {}
local b = require("Modul:bingeh")

--[[	If language is an etymology language, iterates through parent languages
		until it finds a non-etymology language. ]]
function export.getNonEtymological(lang)
	while lang:getType() == "etymology language" do
		local parentCode = lang:getParentCode()
		local parent = require("Modul:ziman").getByCode(parentCode)
			or require("Module:etymology languages").getByCode(parentCode)
			or require("Module:families").getByCode(parentCode)
		
		lang = parent
--		mw.log(terminfo.lang:getCode() .. " " .. terminfo.lang:getType())
	end
	
	return lang
end


local function termError(terminfo)
	if terminfo.lang:getType() == "family" then
		if terminfo.term and terminfo.term ~= "-" then
			require("Module:debug").track("etymology/family/has-term")
		end
		
		terminfo.term = "-"
	end
	return terminfo
end


local function createLink(terminfo, templateName)
	local link = ""
	
	if terminfo.term == "-" then
		--[=[
		[[Special:WhatLinksHere/Template:tracking/cognate/no-term]]
		[[Special:WhatLinksHere/Template:tracking/derived/no-term]]
		[[Special:WhatLinksHere/Template:tracking/borrowed/no-term]]
		[[Special:WhatLinksHere/Template:tracking/calque/no-term]]
		]=]
		require("Module:debug").track(templateName .. "/no-term")
	elseif terminfo.lang:getCode() == "ar" or terminfo.lang:getCode() == "fa" or terminfo.lang:getCode() == "ckb" then --zimanên ku divê îtalîk neyên nivîsîn li vir e
		link = " " .. require("Module:links").full_link(terminfo, "translation", true)
	else
--		mw.log(terminfo.term)
		link = " " .. require("Module:links").full_link(terminfo, "term", true)
	end
	
	return link
end

function export.format_etyl(lang, source, sort_key, categories, force_cat)
	local info = {}
	
	
	-- [[Special:WhatLinksHere/Template:tracking/etymology/sortkey]]
	if sort_key then
		require("Module:debug").track("etymology/sortkey")
	end
	
	if not categories then
		categories = {}
	end
	
	if source:getCode() == "und" then
		info = {
			display = "nehatiye nivîsîn",
			cat_name = "zimanên din",
		}
	elseif source:getCode() == "navz" then
		info = {
			display = "[[w:Translingualism|navzimanî]]",
			cat_name = "navzimanî",
		}
	elseif source:getCode() == "mul-tax" then
		info = {
			display = "[[w:taxonomic name|taxonomic name]]",
			cat_name = "taxonomic names",
		}
	elseif source:getType() == "family" then
		info.display = "[[:Kategorî:" .. b.ucfirst(source:getCategoryName()) .. "|" .. b.lcfirst(source:getCategoryName()) .. "]]"

	else
		info.display = "[[:Kategorî:" .. b.ucfirst(source:getCategoryName()) .. "|" .. b.lcfirst(source:getCategoryName()) .. "]]"
		
		if source:getType() == "family" then
			info.cat_name = source:getCategoryName()
		else
			info.cat_name = source:getCanonicalName()
		end
	end
	
	-- Add the categories, but only if there is a current language
	
	if lang then
		local m_utilities = require("Module:utilities")
		
		if lang:getCode() == source:getCode() then
			table.insert(categories, "Peyvên " .. lang:getCanonicalName() .. " yên du car hatine deynkirin")
		end
		
		categories = m_utilities.format_categories(categories, lang, sort_key, nil, force_cat)
	else
		categories = ""
	end
	
	return "<span class=\"etyl\">" .. info.display .. categories .. "</span>"
end

-- Internal implementation of {{hevreh|...}} template
function export.format_cognate(terminfo, sort_key)
	return export.format_hevreh(nil, terminfo, sort_key, "cognate") -- berê ev digot format_derived
end


-- bi ya en.wikt naxebite loma min li vir du car format_derived çêkir û di format_derived de kategorî heye di ya din (format_hevreh) de tune. 
function export.format_hevreh(lang, terminfo, sort_key, templateName, force_cat)
	local source = terminfo.lang
	
	terminfo.lang = export.getNonEtymological(terminfo.lang)

	terminfo = termError(terminfo)
	
	
	local link = createLink(terminfo, templateName or "derived")

	return export.format_etyl(lang, source, sort_key, nil, force_cat) .. link
end

-- Internal implementation of {{ji|...}} template
function export.format_derived(lang, terminfo, sort_key, templateName, force_cat)
	local source = terminfo.lang
	
	terminfo.lang = export.getNonEtymological(terminfo.lang)

	terminfo = termError(terminfo)
	
	local categories = {}
	
	local link = createLink(terminfo, templateName or "derived")
	
	if source:getType() == "family" then
		table.insert(categories, "Peyvên " .. lang:getCanonicalName() .. " ji " .. source:getCategoryName())
	else
		table.insert(categories, "Peyvên " .. lang:getCanonicalName() .. " ji " .. source:getCanonicalName())
	end
	return export.format_etyl(lang, source, sort_key, categories, force_cat) .. link
end


-- Internal implementation of {{mîrasmayî|...}} template
function export.format_inherited(lang, terminfo, sort_key)
	local source = terminfo.lang

	terminfo = termError(terminfo)
	
	terminfo.lang = export.getNonEtymological(terminfo.lang)

	if not lang:hasAncestor(terminfo.lang) and mw.title.getCurrentTitle().nsText ~= "Şablon" then
		local function showLanguage(lang)
			return ("[[:Kategorî:%s|%s]] (%s)")
				:format(lang:getCategoryName(), lang:getCanonicalName(), lang:getCode())
		end
		local postscript
		local ancestors = lang:getAncestors()
		local moduleLink = "[[Modul:"
			.. require("Modul:ziman").getDataModuleName(lang:getCode())
			.. "]]"
		if not ancestors[1] then
			postscript = "Zimanên pêşî yên " .. showLanguage(lang) .. " tune."
		else
			local ancestorList = table.concat(
				require("Module:fun").map(
					showLanguage,
					ancestors),
				" and ")
			postscript = ("zimanê%s jêder yê %s %s %s ye"):format(
				ancestors[2] and "n" or "", lang:getCanonicalName(),
				ancestors[2] and "" or "", ancestorList)
		end
		error(("%s zimanê pêşî yê %s nîne. Li gorî %s %s")
			:format(showLanguage(terminfo.lang), showLanguage(lang), moduleLink, postscript))
	end
	
	local categories = {}
	
	local link = createLink(terminfo, "inherited")
	
	if source:getType() == "family" then
		table.insert(categories, "Peyvên " .. lang:getCanonicalName() .. " yên mîrasmayî ji " .. source:getCategoryName())
	else
		table.insert(categories, "Peyvên " .. lang:getCanonicalName() .. " yên mîrasmayî ji " .. source:getCanonicalName())
	end
	return export.format_etyl(lang, source, sort_key, categories) .. link
end


-- Internal implementation of {{deynkirî|...}} template
function export.format_borrowed(lang, terminfo, sort_key, nocap, notext, borrowing_type)
	local source = terminfo.lang
	
	terminfo.lang = export.getNonEtymological(terminfo.lang)
	
	terminfo = termError(terminfo)
	
	local text = ""
	local categories = {}

	if lang:getCode() == source:getCode() then
		table.insert(categories, "Peyvên " .. lang:getCanonicalName() .. " yên du car hatine deynkirin")
	elseif source:getType() == "family" then
		table.insert(categories, "Peyvên " .. lang:getCanonicalName() .. " yên deynkirî ji " .. source:getCategoryName())
	else
		table.insert(categories, "Peyvên " .. lang:getCanonicalName() .. " yên deynkirî ji " .. source:getCanonicalName())
	end

	if not notext then
		if borrowing_type == "learned" then
			text = "[[learned borrowing|" .. (nocap and "l" or "L") .. "earned borrowing]] from "
		elseif borrowing_type == "semi-learned" then
			text = "[[semi-learned borrowing|" .. (nocap and "s" or "S") .. "emi-learned borrowing]] from "
		elseif borrowing_type == "orthographic" then
			text = "[[orthographic|" .. (nocap and "o" or "O") .. "rthographic]] [[Appendix:Glossary#borrowing|borrowing]] from "
		elseif borrowing_type == "unadapted" then
			text = "[[Appendix:Glossary#unadapted borrowing|" .. (nocap and "u" or "U") .. "nadapted borrowing]] from "
		else
			text = "[[Appendix:Glossary#loanword|Borrowing]] from "
		end
	end

	if borrowing_type ~= "plain" and lang:getCode() ~= source:getCode() then
		-- For non-plain borrowings, insert extra category, unless lang and source
		-- are the same (a twice-borrowed term).
		local source_name = source:getType() == "family" and source:getCategoryName() or source:getCanonicalName()
		table.insert(categories, lang:getCanonicalName() .. " " .. borrowing_type .. " borrowings from " .. source_name)
	end
	
	local link = createLink(terminfo, "borrowed")
	
	return text .. export.format_etyl(lang, source, sort_key, categories) .. link
end

return export