Module:etymology

Revision as of 16:38, 17 January 2022 by Sware (talk | contribs)


local export = {}

-- For testing
local force_cat = false

--[[	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("Module:languages").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
		terminfo.term = "-"
	end
	return terminfo
end


local function createLink(terminfo, templateName)
	local link = ""
	
	if terminfo.term ~= "-" then
--		mw.log(terminfo.term)
		link = " " .. require("Module:links").full_link(terminfo, "term_i", true)
	end
	
	return link
end


function export.format_etyl(lang, source, sort_key, categories, nocat)
	local info = {}
	
	if not categories then
		categories = {}
	end
	
	if source:getCode() == "und" then
		info = {
			display = "undetermined",
			cat_name = "other languages",
		}
	elseif source:getCode() == "mul" then
		info = {
			display = "[[w:Translingualism|translingual]]",
			cat_name = "Translingual",
		}
	elseif source:getCode() == "mul-tax" then
		info = {
			display = "[[w:taxonomic name|taxonomic name]]",
			cat_name = "taxonomic names",
		}
	else
		info.display = source:makeWikipediaLink()
		info.cat_name = source:getDisplayForm()
	end
	
	-- Add the categories, but only if there is a current language
	
	if lang and not nocat then
		local m_utilities = require("Module:utilities")
		
		if lang:getCode() == source:getCode() then
			table.insert(categories, lang:getCanonicalName() .. " twice-borrowed terms")
		else
			table.insert(categories, lang:getCanonicalName() .. " terms derived from " .. info.cat_name)
		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 {{cognate|...}} template
function export.format_cognate(terminfo, sort_key)
	return export.format_derived(nil, terminfo, sort_key, nil, "cognate")
end


-- Internal implementation of {{derived|...}} template
function export.format_derived(lang, terminfo, sort_key, nocat, templateName)
	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, nocat) .. link
end

-- Return true if `lang` has `otherlang` (which may be an etymology-only language) as an ancestor. This includes
-- cases such as Italian -> Old Italian, which is an etymology-only language whose "parent" is Italian but which is
-- marked as "ancestral to parent". `non_etym_otherlang` is the non-etymology-language equivalent of `otherlang`.
-- If `otherlang` is not an etymology-only language, the two will be the same.
local function hasAncestor(lang, otherlang, non_etym_otherlang)
	-- First check if `lang` has (the non-etymology-language parent of) `otherlang` as its ancestor. If `lang` is e.g.
	-- Italian and `otherlang` is Latin, this will be caught here. If `lang` is Italian and `otherlang` is Vulgar Latin
	-- (an etymology-only language whose parent is Latin), this will also be caught here, as we use the
	-- non-etymology-language parent, in this case Latin, when checking the ancestor hierarchy.
	if lang:hasAncestor(non_etym_otherlang) then
		return true
	end
	-- Second check for the Italian -> Old Italian case. This happens when (a) `otherlang` is etymology-only,
	-- (b) `otherlang` is marked as "ancestral to parent", (c) `otherlang`'s parent is the same as `lang`. This is not
	-- caught above because Italian is not ancestral to Italian.
	if otherlang:getType() == "etymology language" and otherlang:isAncestralToParent() then
		local otherlang_parent = export.getNonEtymological(otherlang)
		if otherlang_parent and otherlang_parent:getCode() == lang:getCode() then
			return true
		end
	end
	return false
end

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

	terminfo = termError(terminfo)

	local orig_terminfo_lang = terminfo.lang

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

	if not hasAncestor(lang, orig_terminfo_lang, terminfo.lang) and mw.title.getCurrentTitle().nsText ~= "Template" then
		-- All of the code inside this if-clause is for generating the error message.
		local function showLanguage(lang)
			local retval = ("%s (%s)"):format(lang:makeCategoryLink(), lang:getCode())
			if lang:getType() == "etymology language" then
				retval = retval .. (" (an etymology-only language whose regular parent is %s)"):format(
					showLanguage(export.getNonEtymological(lang)))
			end
			return retval
		end
		local postscript
		local ancestors = lang:getAncestors()
		local moduleLink = "[[Module:"
			.. require("Module:languages").getDataModuleName(lang:getCode())
			.. "]]"
		if not ancestors[1] then
			postscript = showLanguage(lang) .. " has no ancestors."
		else
			local ancestorList = table.concat(
				require("Module:fun").map(
					showLanguage,
					ancestors),
				" and ")
			postscript = ("The ancestor%s of %s %s %s."):format(
				ancestors[2] and "s" or "", lang:getCanonicalName(),
				ancestors[2] and "are" or "is", ancestorList)
		end
		error(("%s is not set as an ancestor of %s in %s. %s")
			:format(showLanguage(orig_terminfo_lang), showLanguage(lang), moduleLink, postscript))
	end
	
	local categories = {}
	
	local link = createLink(terminfo, "inherited")
	
	table.insert(categories, lang:getCanonicalName() .. " terms inherited from " .. source:getCanonicalName())
	
	return export.format_etyl(lang, source, sort_key, categories, nocat) .. link
end


-- Internal implementation of {{borrowed|...}} template
function export.format_borrowed(lang, terminfo, sort_key, nocap, notext, nocat, 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, lang:getCanonicalName() .. " twice-borrowed terms")
	else
		table.insert(categories, lang:getCanonicalName() .. " terms borrowed from " .. source:getDisplayForm())
	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 = "[[Appendix:Glossary#orthographic borrowing|" .. (nocap and "o" or "O") .. "rthographic 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).
		table.insert(categories, lang:getCanonicalName() .. " " .. borrowing_type .. " borrowings from " ..
			source:getDisplayForm())
	end
	
	local link = createLink(terminfo, "borrowed")
	
	return text .. export.format_etyl(lang, source, sort_key, categories, nocat) .. link
end


local function specialized_borrowing(lang, terminfo, sort_key, nocat, pre_text, template_name, category)
	local result = pre_text
	
	local source = terminfo.lang
	
	terminfo.lang = export.getNonEtymological(terminfo.lang)

	terminfo = termError(terminfo)

	local categories = {}

	category = category:gsub("SOURCE", source:getDisplayForm())
	table.insert(categories, lang:getCanonicalName() .. " " .. category)
	
	local link = createLink(terminfo, template_name)
	
	result = result .. export.format_etyl(lang, source, sort_key, categories, nocat) .. link
	
	return result
end


-- Internal implementation of {{calque|...}} template
function export.calque(lang, terminfo, sort_key, nocap, notext, nocat)
	local pre_text = ""
	
	if not notext then
		pre_text = pre_text .. "[[Appendix:Glossary#calque|" .. (nocap and "c" or "C") .. "alque]] of "
	end

	return specialized_borrowing(lang, terminfo, sort_key, nocat, pre_text, "calque", "terms calqued from SOURCE")
end


-- Internal implementation of {{partial calque|...}} template
function export.partial_calque(lang, terminfo, sort_key, nocap, notext, nocat)
	local pre_text = ""
	
	if not notext then
		pre_text = pre_text .. "[[Appendix:Glossary#partial calque|" .. (nocap and "p" or "P") .. "artial calque]] of "
	end

	return specialized_borrowing(lang, terminfo, sort_key, nocat, pre_text, "partial_calque", "terms partially calqued from SOURCE")
end


-- Internal implementation of {{semantic loan|...}} template
function export.semantic_loan(lang, terminfo, sort_key, nocap, notext, nocat)

	local pre_text = ""
	
	if not notext then
		pre_text = pre_text .. "[[Appendix:Glossary#semantic loan|" .. (nocap and "s" or "S") .. "emantic loan]] from "
	end

	return specialized_borrowing(lang, terminfo, sort_key, nocat, pre_text, "semantic_loan", "semantic loans from SOURCE")
end

-- Internal implementation of {{phono-semantic matching|...}} template
function export.phono_semantic_matching(lang, terminfo, sort_key, nocap, notext, nocat)

	local pre_text = ""
	
	if not notext then
		-- FIXME, create entry in [[Appendix:Glossary]]
		pre_text = pre_text .. "[[w:Phono-semantic matching|" .. (nocap and "p" or "P") .. "hono-semantic matching]] of "
	end

	return specialized_borrowing(lang, terminfo, sort_key, nocat, pre_text, "phono_semantic_matching", "phono-semantic matchings from SOURCE")
end

return export