Module:etymology: Difference between revisions

From Linguifex
Jump to navigation Jump to search
No edit summary
No edit summary
 
(11 intermediate revisions by the same user not shown)
Line 4: Line 4:
local force_cat = false
local force_cat = false


--[[ If language is an etymology language, iterates through parent languages
local function term_error(terminfo)
until it finds a non-etymology language. ]]
if terminfo.lang:hasType("family") then
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 = "-"
terminfo.term = "-"
end
end
Line 29: Line 12:




local function createLink(terminfo, templateName)
local function create_link(terminfo, template_name, to_wik)
local link = ""
local link = ""
if terminfo.term ~= "-" then
if terminfo.term ~= "-" then
facescript = require("Module:script utilities").is_Latin_script(data.sc) and "term_i" or nil
-- mw.log(terminfo.term)
link = " " .. require("Module:links").full_link(terminfo, facescript, true)
link = require("Module:links").full_link(terminfo, "term_i", true, false, to_wik)
if (link ~= "") then link = " " .. link end
end
end
Line 41: Line 25:




function export.format_etyl(lang, source, sort_key, categories, nocat)
function export.process_and_create_link(terminfo, template_name, to_wik)
local info = {}
terminfo = term_error(terminfo)
return create_link(terminfo, template_name or "derived", to_wik)
if not categories then
end
categories = {}
end
function export.get_display_and_cat_name(source, raw)
local display, cat_name
if source:getCode() == "und" then
if source:getCode() == "und" then
info = {
display = "undetermined"
display = "undetermined",
cat_name = "other languages"
cat_name = "other languages",
}
elseif source:getCode() == "mul" then
elseif source:getCode() == "mul" then
info = {
display = "[[w:Translingualism|translingual]]"
display = "[[w:Translingualism|translingual]]",
cat_name = "Translingual"
cat_name = "Translingual",
}
elseif source:getCode() == "mul-tax" then
elseif source:getCode() == "mul-tax" then
info = {
display = "[[w:taxonomic name|taxonomic name]]"
display = "[[w:taxonomic name|taxonomic name]]",
cat_name = "taxonomic names"
cat_name = "taxonomic names",
}
else
else
info.display = source:makeWikipediaLink()
display = raw and source:getCanonicalName() or source:makeWikipediaLink()
cat_name = source:getDisplayForm()
if source:getType() == "family" then
end
info.cat_name = source:getCategoryName()
 
return display, cat_name
end
 
 
function export.insert_source_cat_get_display(data)
local categories, lang, source = data.categories, data.lang, data.source
local display, cat_name = export.get_display_and_cat_name(source, data.raw)
 
if lang and not data.nocat then
-- Add the category, but only if there is a current language
if not categories then
categories = {}
end
 
local langname = lang:getFullName()
-- If `lang` is an etym-only language, we need to check both it and its parent full language against `source`.
-- Otherwise if e.g. `lang` is Medieval Latin and `source` is Latin, we'll end up wrongly constructing a
-- category 'Latin terms derived from Latin'.
if lang:getCode() == source:getCode() or lang:getFullCode() == source:getCode() then
table.insert(categories, langname .. " terms borrowed back into " .. langname)
else
else
info.cat_name = source:getCanonicalName()
table.insert(categories, langname .. " " .. (data.borrowing_type or "terms derived") .. " from " ..
cat_name)
end
end
end
end
 
-- Add the categories, but only if there is a current language
return display, categories
end
if lang and not nocat then
 
local m_utilities = require("Module:utilities")
 
function export.format_source(data)
if lang:getCode() == source:getCode() then
local lang, sort_key = data.lang, data.sort_key
table.insert(categories, lang:getCanonicalName() .. " twice-borrowed terms")
 
else
local display, categories = export.insert_source_cat_get_display(data)
table.insert(categories, lang:getCanonicalName() .. " terms derived from " .. info.cat_name)
if lang and not data.nocat then
end
-- Format categories, but only if there is a current language; {{cog}} currently gets no categories
categories = require("Module:utilities").format_categories(categories, lang, sort_key, nil,
categories = m_utilities.format_categories(categories, lang, sort_key, nil, force_cat)
data.force_cat or force_cat)
else
else
categories = ""
categories = ""
end
end
return "<span class=\"etyl\">" .. info.display .. categories .. "</span>"
return "<span class=\"etyl\">" .. display .. categories .. "</span>"
end
end




-- Internal implementation of {{cognate|...}} template
-- Internal implementation of {{cognate|...}} template
function export.format_cognate(terminfo, sort_key)
function export.format_cognate(data)
return export.format_derived(nil, terminfo, sort_key, nil, "cognate")
return export.format_derived {
terminfo = data.terminfo,
sort_key = data.sort_key,
template_name = "cognate",
}
end
end




-- Internal implementation of {{derived|...}} template
-- Internal implementation of {{derived|...}} template
function export.format_derived(lang, terminfo, sort_key, nocat, templateName)
function export.format_derived(data)
local source = terminfo.lang
local lang, terminfo, sort_key, nocat, template_name =
data.lang, data.terminfo, data.sort_key, data.nocat, data.template_name
terminfo.lang = export.getNonEtymological(terminfo.lang)
return export.format_source {
 
lang = lang,
terminfo = termError(terminfo)
source = terminfo.lang,
sort_key = sort_key,
terminfo.nocont = true
nocat = nocat,
borrowing_type = data.borrowing_type,
local link = createLink(terminfo, templateName or "derived")
force_cat = data.force_cat,
} .. export.process_and_create_link(terminfo, template_name)
return export.format_etyl(lang, source, sort_key, nil, nocat) .. link
end
end


 
do
-- Internal implementation of {{inherited|...}} template
-- Generate the non-ancestor error message.
function export.format_inherited(lang, terminfo, sort_key, nocat)
local function showLanguage(lang)
local source = terminfo.lang
local retval = ("%s (%s)"):format(lang:makeCategoryLink(), lang:getCode())
 
if lang:hasType("etymology-only") then
terminfo = termError(terminfo)
retval = retval .. (" (an etymology-only language whose regular parent is %s)"):format(
showLanguage(lang:getParent()))
end
return retval
end
terminfo.lang = export.getNonEtymological(terminfo.lang)
-- Check that `lang` has `otherlang` (which may be an etymology-only language) as an ancestor. Throw an error if not.
 
function export.check_ancestor(lang, otherlang)
if not lang:hasAncestor(terminfo.lang) and mw.title.getCurrentTitle().nsText ~= "Template" then
-- FIXME: I don't know if this function works correctly with etym-only languages in `lang`. I have fixed up
local function showLanguage(lang)
-- the module link code appropriately (June 2024) but the remaining logic is untouched.
return ("[[:Category:%s|%s]] (%s)")
if lang:hasAncestor(otherlang) or mw.title.getCurrentTitle().nsText == "Template" then
:format(lang:getCategoryName(), lang:getCanonicalName(), lang:getCode())
return
end
end
local postscript
local ancestors, postscript = lang:getAncestors()
local ancestors = lang:getAncestors()
local etymModuleLink = lang:hasType("etymology-only") and "[[Module:etymology languages/data]] or " or ""
local moduleLink = "[[Module:"
local moduleLink = "[[Module:"
.. require("Module:languages").getDataModuleName(lang:getCode())
.. require("Module:languages").getDataModuleName(lang:getFullCode())
.. "]]"
.. "]]"
if not ancestors[1] then
if not ancestors[1] then
Line 145: Line 152:
ancestors[2] and "are" or "is", ancestorList)
ancestors[2] and "are" or "is", ancestorList)
end
end
error(("%s is not set as an ancestor of %s in %s. %s")
error(("%s is not set as an ancestor of %s in %s%s. %s")
:format(showLanguage(terminfo.lang), showLanguage(lang), moduleLink, postscript))
:format(showLanguage(otherlang), showLanguage(lang), etymModuleLink, moduleLink, postscript))
end
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
end




-- Internal implementation of {{borrowed|...}} template
-- Internal implementation of {{inherited|...}} template
function export.format_borrowed(lang, terminfo, sort_key, nocap, notext, nocat, borrowing_type)
function export.format_inherited(data)
local lang, terminfo, sort_key, nocat = data.lang, data.terminfo, data.sort_key, data.nocat
local source = terminfo.lang
local source = terminfo.lang
terminfo.lang = export.getNonEtymological(terminfo.lang)
terminfo = termError(terminfo)
local text = ""
local categories = {}
local categories = {}
 
if not nocat then
if lang:getCode() == source:getCode() then
table.insert(categories, lang:getFullName() .. " terms inherited from " .. source:getCanonicalName())
table.insert(categories, lang:getCanonicalName() .. " twice-borrowed terms")
elseif source:getType() == "family" then
table.insert(categories, lang:getCanonicalName() .. " terms borrowed from " .. source:getCategoryName())
else
table.insert(categories, lang:getCanonicalName() .. " terms borrowed from " .. source:getCanonicalName())
end
end


if not notext then
local link = export.process_and_create_link(terminfo, "inherited")
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, 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
export.check_ancestor(lang, source)
terminfo.lang = export.getNonEtymological(terminfo.lang)
 
terminfo = termError(terminfo)
 
local categories = {}


if source:getType() == "family" then
return export.format_source {
category = category:gsub("SOURCE", source:getCategoryName())
lang = lang,
else
source = source,
category = category:gsub("SOURCE", source:getCanonicalName())
sort_key = sort_key,
end
categories = categories,
table.insert(categories, lang:getCanonicalName() .. " " .. category)
nocat = nocat,
force_cat = data.force_cat,
local link = createLink(terminfo, template_name)
} .. link
result = result .. " " ..  export.format_etyl(lang, source, sort_key, categories, nocat) .. link
return result
end
end




-- Internal implementation of {{calque|...}} template
function export.insert_borrowed_cat(categories, lang, source)
function export.calque(lang, terminfo, sort_key, nocap, notext, nocat)
local category
local pre_text = ""
-- Do the same check as in insert_source_cat_get_display() (inverted).
if not (lang:getCode() == source:getCode() or lang:getFullCode() == source:getCode()) then
if not notext then
-- If both are the same, we want e.g. [[:Category:English terms borrowed back into English]] not
pre_text = pre_text .. "[[Appendix:Glossary#calque|" .. (nocap and "c" or "C") .. "alque]] of "
-- [[:Category:English terms borrowed from English]]; the former is inserted automatically by format_source().
category = " terms borrowed from " .. source:getDisplayForm()
end
end
 
if category then
return specialized_borrowing(lang, terminfo, sort_key, nocat, pre_text, "calque", "terms calqued from SOURCE")
table.insert(categories, lang:getFullName() .. category)
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
end
return specialized_borrowing(lang, terminfo, sort_key, nocat, pre_text, "partial_calque", "terms partially calqued from SOURCE")
end
end




-- Internal implementation of {{semantic loan|...}} template
-- Internal implementation of {{borrowed|...}} template.
function export.semantic_loan(lang, terminfo, sort_key, nocap, notext, nocat)
function export.format_borrowed(data)
local pre_text = ""
local lang, terminfo, sort_key, nocat = data.lang, data.terminfo, data.sort_key, data.nocat
local source = terminfo.lang
if not notext then
local categories = {}
pre_text = pre_text .. "[[Appendix:Glossary#semantic loan|" .. (nocap and "s" or "S") .. "emantic loan]] from "
if not nocat then
end
export.insert_borrowed_cat(categories, lang, source)
 
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
end


return specialized_borrowing(lang, terminfo, sort_key, nocat, pre_text, "phono_semantic_matching", "phono-semantic matchings from SOURCE")
return export.format_source {
lang = lang,
source = source,
sort_key = sort_key,
categories = categories,
nocat = nocat,
force_cat = data.force_cat,
} .. export.process_and_create_link(terminfo, "borrowed")
end
end


return export
return export

Latest revision as of 22:50, 15 September 2024



local export = {}

-- For testing
local force_cat = false

local function term_error(terminfo)
	if terminfo.lang:hasType("family") then
		terminfo.term = "-"
	end
	return terminfo
end


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


function export.process_and_create_link(terminfo, template_name, to_wik)
	terminfo = term_error(terminfo)
	return create_link(terminfo, template_name or "derived", to_wik)
end
	

function export.get_display_and_cat_name(source, raw)
	local display, cat_name
	if source:getCode() == "und" then
		display = "undetermined"
		cat_name = "other languages"
	elseif source:getCode() == "mul" then
		display = "[[w:Translingualism|translingual]]"
		cat_name = "Translingual"
	elseif source:getCode() == "mul-tax" then
		display = "[[w:taxonomic name|taxonomic name]]"
		cat_name = "taxonomic names"
	else
		display = raw and source:getCanonicalName() or source:makeWikipediaLink()
		cat_name = source:getDisplayForm()
	end

	return display, cat_name
end


function export.insert_source_cat_get_display(data)
	local categories, lang, source = data.categories, data.lang, data.source
	local display, cat_name = export.get_display_and_cat_name(source, data.raw)

	if lang and not data.nocat then
		-- Add the category, but only if there is a current language
		if not categories then
			categories = {}
		end

		local langname = lang:getFullName()
		-- If `lang` is an etym-only language, we need to check both it and its parent full language against `source`.
		-- Otherwise if e.g. `lang` is Medieval Latin and `source` is Latin, we'll end up wrongly constructing a
		-- category 'Latin terms derived from Latin'.
		if lang:getCode() == source:getCode() or lang:getFullCode() == source:getCode() then
			table.insert(categories, langname .. " terms borrowed back into " .. langname)
		else
			table.insert(categories, langname .. " " .. (data.borrowing_type or "terms derived") .. " from " ..
				cat_name)
		end
	end

	return display, categories
end


function export.format_source(data)
	local lang, sort_key = data.lang, data.sort_key

	local display, categories = export.insert_source_cat_get_display(data)
	if lang and not data.nocat then
		-- Format categories, but only if there is a current language; {{cog}} currently gets no categories
		categories = require("Module:utilities").format_categories(categories, lang, sort_key, nil,
			data.force_cat or force_cat)
	else
		categories = ""
	end
	
	return "<span class=\"etyl\">" .. display .. categories .. "</span>"
end


-- Internal implementation of {{cognate|...}} template
function export.format_cognate(data)
	return export.format_derived {
		terminfo = data.terminfo,
		sort_key = data.sort_key,
		template_name = "cognate",
	}
end


-- Internal implementation of {{derived|...}} template
function export.format_derived(data)
	local lang, terminfo, sort_key, nocat, template_name =
		data.lang, data.terminfo, data.sort_key, data.nocat, data.template_name
	return export.format_source {
		lang = lang,
		source = terminfo.lang,
		sort_key = sort_key,
		nocat = nocat,
		borrowing_type = data.borrowing_type,
		force_cat = data.force_cat,
	} .. export.process_and_create_link(terminfo, template_name)
end

do
	-- Generate the non-ancestor error message.
	local function showLanguage(lang)
		local retval = ("%s (%s)"):format(lang:makeCategoryLink(), lang:getCode())
		if lang:hasType("etymology-only") then
			retval = retval .. (" (an etymology-only language whose regular parent is %s)"):format(
				showLanguage(lang:getParent()))
		end
		return retval
	end
	
	-- Check that `lang` has `otherlang` (which may be an etymology-only language) as an ancestor. Throw an error if not.
	function export.check_ancestor(lang, otherlang)
		-- FIXME: I don't know if this function works correctly with etym-only languages in `lang`. I have fixed up
		-- the module link code appropriately (June 2024) but the remaining logic is untouched.
		if lang:hasAncestor(otherlang) or mw.title.getCurrentTitle().nsText == "Template" then
			return
		end
		local ancestors, postscript = lang:getAncestors()
		local etymModuleLink = lang:hasType("etymology-only") and "[[Module:etymology languages/data]] or " or ""
		local moduleLink = "[[Module:"
			.. require("Module:languages").getDataModuleName(lang:getFullCode())
			.. "]]"
		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. %s")
			:format(showLanguage(otherlang), showLanguage(lang), etymModuleLink, moduleLink, postscript))
	end
end


-- Internal implementation of {{inherited|...}} template
function export.format_inherited(data)
	local lang, terminfo, sort_key, nocat = data.lang, data.terminfo, data.sort_key, data.nocat
	local source = terminfo.lang
	
	local categories = {}
	if not nocat then
		table.insert(categories, lang:getFullName() .. " terms inherited from " .. source:getCanonicalName())
	end

	local link = export.process_and_create_link(terminfo, "inherited")
	
	export.check_ancestor(lang, source)

	return export.format_source {
		lang = lang,
		source = source,
		sort_key = sort_key,
		categories = categories,
		nocat = nocat,
		force_cat = data.force_cat,
	} .. link
end


function export.insert_borrowed_cat(categories, lang, source)
	local category
	-- Do the same check as in insert_source_cat_get_display() (inverted).
	if not (lang:getCode() == source:getCode() or lang:getFullCode() == source:getCode()) then
		-- If both are the same, we want e.g. [[:Category:English terms borrowed back into English]] not
		-- [[:Category:English terms borrowed from English]]; the former is inserted automatically by format_source().
		category = " terms borrowed from " .. source:getDisplayForm()
	end
	if category then
		table.insert(categories, lang:getFullName() .. category)
	end
end


-- Internal implementation of {{borrowed|...}} template.
function export.format_borrowed(data)
	local lang, terminfo, sort_key, nocat = data.lang, data.terminfo, data.sort_key, data.nocat
	local source = terminfo.lang
	
	local categories = {}
	if not nocat then
		export.insert_borrowed_cat(categories, lang, source)
	end

	return export.format_source {
		lang = lang,
		source = source,
		sort_key = sort_key,
		categories = categories,
		nocat = nocat,
		force_cat = data.force_cat,
	} .. export.process_and_create_link(terminfo, "borrowed")
end

return export