Module:Etymology: Difference between revisions

From The Languages of David J. Peterson
Jump to navigation Jump to search
No edit summary
Tag: Reverted
(Undo revision 536382 by Juelos (talk))
Tag: Undo
 
Line 29: Line 29:
else
else
-- mw.log(terminfo.term)
-- mw.log(terminfo.term)
link = require("Module:links").full_link(terminfo, "term")
link = require("Module:links").full_link(terminfo, "term", true)
if (link ~= "") then link = " " .. link end
if (link ~= "") then link = " " .. link end
end
end
Line 63: Line 63:




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


if lang and not data.nocat then
if lang and not nocat then
-- Add the category, but only if there is a current language
-- Add the category, but only if there is a current language
if not categories then
if not categories then
categories = {}
categories = {}
end
end
 
local langname = lang:getFullName()
if lang:getCode() == source:getCode() then
-- If `lang` is an etym-only language, we need to check both it and its parent full language against `source`.
table.insert(categories, lang:getCanonicalName() .. " twice-borrowed terms")
-- 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
table.insert(categories, langname .. " " .. (data.borrowing_type or "terms derived") .. " from " ..
table.insert(categories, lang:getCanonicalName() .. " terms derived from " .. cat_name)
cat_name)
end
end
end
end
Line 89: Line 83:




function export.format_source(data)
-- FIXME: rename to format_source()
local lang, sort_key = data.lang, data.sort_key
function export.format_etyl(lang, source, sort_key, categories, nocat)
-- [[Special:WhatLinksHere/Wiktionary:Tracking/etymology/sortkey]]
-- [[Special:WhatLinksHere/Template:tracking/etymology/sortkey]]
if sort_key then
if sort_key then
require("Module:debug/track")("etymology/sortkey")
require("Module:debug/track")("etymology/sortkey")
end
end


local display, categories = export.insert_source_cat_get_display(data)
local display
if lang and not data.nocat then
display, categories = export.insert_source_cat_get_display(categories, lang, source, false, nocat)
if lang and not nocat then
-- Format categories, but only if there is a current language; {{cog}} currently gets no categories
-- 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 = require("Module:utilities").format_categories(categories, lang, sort_key, nil, force_cat)
data.force_cat or force_cat)
else
else
categories = ""
categories = ""
Line 110: Line 104:


-- Internal implementation of {{cognate|...}} template
-- Internal implementation of {{cognate|...}} template
function export.format_cognate(data)
function export.format_cognate(terminfo, sort_key)
return export.format_derived {
return export.format_derived(nil, terminfo, sort_key, nil, "cognate")
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(data)
function export.format_derived(lang, terminfo, sort_key, nocat, template_name)
local lang, terminfo, sort_key, nocat, template_name =
local source = terminfo.lang
data.lang, data.terminfo, data.sort_key, data.nocat, data.template_name
return export.format_etyl(lang, source, sort_key, nil, nocat) .. export.process_and_create_link(terminfo, 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
end


do
-- Check that `lang` has `otherlang` (which may be an etymology-only language) as an ancestor. Throw an error if not.
-- Generate the non-ancestor error message.
local function check_ancestor(lang, otherlang)
local function showLanguage(lang)
if not lang:hasAncestor(otherlang) and mw.title.getCurrentTitle().nsText ~= "Template" then
local retval = ("%s (%s)"):format(lang:makeCategoryLink(), lang:getCode())
-- Generate the non-ancestor error message.
if lang:hasType("etymology-only") then
local function showLanguage(lang)
retval = retval .. (" (an etymology-only language whose regular parent is %s)"):format(
local retval = ("%s (%s)"):format(lang:makeCategoryLink(), lang:getCode())
showLanguage(lang:getParent()))
if lang:hasType("etymology-only") then
end
retval = retval .. (" (an etymology-only language whose regular parent is %s)"):format(
return retval
showLanguage(lang:getParent()))
end
end
return retval
-- 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
end
local ancestors, postscript = lang:getAncestors()
local postscript
local etymModuleLink = lang:hasType("etymology-only") and "[[Module:etymology languages/data]] or " or ""
local ancestors = lang:getAncestors()
local moduleLink = "[[Module:"
local moduleLink = "[[Module:"
.. require("Module:languages").getDataModuleName(lang:getFullCode())
.. require("Module:languages").getDataModuleName(lang:getCode())
.. "]]"
.. "]]"
if not ancestors[1] then
if not ancestors[1] then
Line 168: Line 144:
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. %s")
error(("%s is not set as an ancestor of %s in %s. %s")
:format(showLanguage(otherlang), showLanguage(lang), etymModuleLink, moduleLink, postscript))
:format(showLanguage(otherlang), showLanguage(lang), moduleLink, postscript))
end
end
end
end
Line 175: Line 151:


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


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


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


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




-- Internal implementation of {{borrowed|...}} template.
-- Internal implementation of {{borrowed|...}} template.
function export.format_borrowed(data)
function export.format_borrowed(lang, terminfo, sort_key, nocat)
local lang, terminfo, sort_key, nocat = data.lang, data.terminfo, data.sort_key, data.nocat
local source = terminfo.lang
local source = terminfo.lang
Line 223: Line 187:
end
end


return export.format_source {
return export.format_etyl(lang, source, sort_key, categories, nocat) ..
lang = lang,
export.process_and_create_link(terminfo, "borrowed")
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 23:40, 5 September 2024

If in templates, invoke Module:etymology/templates (or one of its submodules) instead.


local export = {}

-- For testing
local force_cat = false

local function term_error(terminfo)
	if terminfo.lang:hasType("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 create_link(terminfo, template_name)
	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")(template_name .. "/no-term")
	else
--		mw.log(terminfo.term)
		link = require("Module:links").full_link(terminfo, "term", true)
		if (link ~= "") then link = " " .. link end
	end
	
	return link
end


function export.process_and_create_link(terminfo, template_name)
	terminfo = term_error(terminfo)
	return create_link(terminfo, template_name or "derived")
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 = "[[Wikipedia:Translingualism|translingual]]"
		cat_name = "Translingual"
	elseif source:getCode() == "mul-tax" then
		display = "[[Wikipedia: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(categories, lang, source, raw, nocat)
	local display, cat_name = export.get_display_and_cat_name(source, raw)

	if lang and not nocat then
		-- Add the category, but only if there is a current language
		if not categories then
			categories = {}
		end
	
		if lang:getCode() == source:getCode() then
			table.insert(categories, lang:getCanonicalName() .. " twice-borrowed terms")
		else
			table.insert(categories, lang:getCanonicalName() .. " terms derived from " .. cat_name)
		end
	end

	return display, categories
end


-- FIXME: rename to format_source()
function export.format_etyl(lang, source, sort_key, categories, nocat)
	-- [[Special:WhatLinksHere/Template:tracking/etymology/sortkey]]
	if sort_key then
		require("Module:debug/track")("etymology/sortkey")
	end

	local display
	display, categories = export.insert_source_cat_get_display(categories, lang, source, false, nocat)
	if lang and not 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, force_cat)
	else
		categories = ""
	end
	
	return "<span class=\"etyl\">" .. 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, template_name)
	local source = terminfo.lang
	return export.format_etyl(lang, source, sort_key, nil, nocat) .. export.process_and_create_link(terminfo, template_name)
end

-- Check that `lang` has `otherlang` (which may be an etymology-only language) as an ancestor. Throw an error if not.
local function check_ancestor(lang, otherlang)
	if not lang:hasAncestor(otherlang) and mw.title.getCurrentTitle().nsText ~= "Template" then
		-- 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
		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(otherlang), showLanguage(lang), moduleLink, postscript))
	end
end


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

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

	return export.format_etyl(lang, source, sort_key, categories, nocat) .. link
end


function export.insert_borrowed_cat(categories, lang, source)
	local category
	if lang:getCode() == source:getCode() then
		category = " twice-borrowed terms"
	else
		category = " terms borrowed from " .. source:getDisplayForm()
	end
	table.insert(categories, lang:getCanonicalName() .. category)
end


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

	return export.format_etyl(lang, source, sort_key, categories, nocat) ..
		export.process_and_create_link(terminfo, "borrowed")
end

return export