Module:etymology: difference between revisions

From Wiktionary, the free dictionary
Jump to navigation Jump to search
Content deleted Content added
mNo edit summary
No edit summary
Line 28: Line 28:
local link = ""
local link = ""
if terminfo.term or terminfo.alt or terminfo.tr then
if terminfo.term == "-" then
--[=[
-- Track template instances without a term.
[[Special:WhatLinksHere/Template:tracking/cognate/no-term]]
if terminfo.term == "-" then
[[Special:WhatLinksHere/Template:tracking/derived/no-term]]
--[=[
[[Special:WhatLinksHere/Template:tracking/cognate/no-term]]
[[Special:WhatLinksHere/Template:tracking/borrowed/no-term]]
[[Special:WhatLinksHere/Template:tracking/derived/no-term]]
[[Special:WhatLinksHere/Template:tracking/calque/no-term]]
]=]
[[Special:WhatLinksHere/Template:tracking/borrowed/no-term]]
require("Module:debug").track(templateName .. "/no-term")
[[Special:WhatLinksHere/Template:tracking/calque/no-term]]
else
]=]
require("Module:debug").track(templateName .. "/no-term")
link = " " .. require("Module:links").full_link(terminfo, "term", true)
else
link = " " .. require("Module:links").full_link(terminfo, "term", true)
end
end
end

Revision as of 17:35, 13 March 2017

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


local export = {}

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

local function termError(terminfo)
	if not (terminfo.lang:getType() == "regular" or terminfo.lang:getType() == "reconstructed" or terminfo.lang:getType() == "etymology language") then
		if terminfo.term then
			error("A term was provided but the given code \"" .. terminfo.lang:getCode() .. "\" is not a language, and therefore cannot have terms or dictionary entries.")
		end
	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")
	else
		link = " " .. require("Module:links").full_link(terminfo, "term", true)
	end
	
	return link
end

function export.format_etyl(lang, source, sort_key, categories)
	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 = "undetermined",
			cat_name = "other languages",
		}
	else
		info.display = source:makeWikipediaLink()
		
		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() or (source:getType() == "etymology language" and lang:getCode() == source:getParentCode()) 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)
	else
		categories = ""
	end
	
	return "<span class=\"etyl\">" .. info.display .. categories .. "</span>"
end

-- Internal implementation of {{cognate|...}} and {{cog|...}} templates
function export.format_cognate(terminfo, sort_key)
	return export.format_derived(nil, terminfo, sort_key, "cognate")
end

-- Internal implementation of {{derived|...}} and {{der|...}} templates
function export.format_derived(lang, terminfo, sort_key, templateName)
	local source = terminfo.lang

	terminfo = termError(terminfo)
	
	terminfo = getNonEtymological(terminfo)
	
	local link = createLink(terminfo, templateName or "derived")
	
	return export.format_etyl(lang, source, sort_key) .. link
end

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

	terminfo = termError(terminfo)
	
	terminfo = getNonEtymological(terminfo)

	if not lang:hasAncestor(terminfo.lang) and mw.title.getCurrentTitle().nsText ~= "Template" then
		error(terminfo.lang:getCanonicalName() .. " is not an ancestor of " .. lang:getCanonicalName() .. ".")
	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) .. link
end

-- Internal implementation of {{borrowed|...}} and {{bor|...}} templates
function export.format_borrowed(lang, terminfo, sort_key, nocap, notext, learned)
	if nocap then
		require("Module:debug").track("bor/nocap")
	end
	
	local source = terminfo.lang

	terminfo = termError(terminfo)
	
	terminfo = getNonEtymological(terminfo)

	local text = ""
	local categories = {}

	if lang:getCode() == terminfo.lang:getCode() then
		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
	
	if not notext then
		if learned then
			text = "[[learned borrowing|" .. (nocap and "l" or "L") .. "earned borrowing]] from "
			table.insert(categories, lang:getCanonicalName() .. " learnedly borrowed terms")
		else
			text = "[[Appendix:Glossary#loanword|" .. (nocap and "b" or "B") .. "orrowing]] from "
		end
	end
	
	local link = createLink(terminfo, "borrowed")
	
	return text .. export.format_etyl(lang, source, sort_key, categories) .. link
end

-- Internal implementation of {{calque|...}} template
function export.calque(lang, terminfo, parts, sort_key, nocap, notext)
	local result = ""
	
	if parts and #parts ~= 0 then
		result = result .. require("Module:compound").show_compound(lang, nil, parts)
		result = result .. ", [[Appendix:Glossary#calque|calque]] of"
		result = result .. "[[Category:calque with terms]] "
	elseif not notext then
		result = result .. "[[Appendix:Glossary#calque|" .. (nocap and "c" or "C") .. "alque]] of "
	end
	
	local source = terminfo.lang

	terminfo = termError(terminfo)
	
	terminfo = getNonEtymological(terminfo)

	local categories = {}

	if lang:getCode() == terminfo.lang:getCode() then
		table.insert(categories, lang:getCanonicalName() .. " twice-borrowed terms")
	elseif source:getType() == "family" then
		table.insert(categories, lang:getCanonicalName() .. " terms calqued from " .. source:getCategoryName())
	else
		table.insert(categories, lang:getCanonicalName() .. " terms calqued from " .. source:getCanonicalName())
	end
	
	local link = createLink(terminfo, "calque")
	
	result = result .. " " ..  export.format_etyl(lang, source, sort_key, categories) .. link
	
	return result
end

return export