Module:User:Isomorphyc/etymology-draft

From Wiktionary, the free dictionary
Jump to navigation Jump to search

This is a private module sandbox of Isomorphyc, for their own experimentation. Items in this module may be added and removed at Isomorphyc's discretion; do not rely on this module's stability.


local export = {}

function export.format_etyl(lang, source, sort_key)
	local info
	
	if source:getCode() == "und" then
		info = {
			display = "undetermined",
			cat_name = "other languages"}
	elseif source:getType() == "etymology language" then
		info = {
			display = "[[w:" .. source:getWikipediaArticle() .. "|" .. source:getCanonicalName() .. "]]",
			cat_name = source:getCanonicalName()}
	elseif source:getType() == "family" then
		info = {
			display = "[[w:" .. source:getCategoryName() .. "|" .. source:getCanonicalName() .. "]]",
			cat_name = source:getCategoryName()}
	else
		info = {
			display = "[[w:" .. source:getWikipediaArticle() .. "|" .. source:getCanonicalName() .. "]]",
			cat_name = source:getCanonicalName()}
	end
	
	-- Add the categories, but only if there is a current language
	local categories = ""
	
	if lang then
		local m_utilities = require("Module:utilities")
		
		categories = {}
		
		if lang:getCode() == source:getCode() or (source:getType() == "etymology language" and lang:getCode() == source:getParentCode()) then
			categories = m_utilities.format_categories({lang:getCanonicalName() .. " twice-borrowed terms"}, lang, sort_key)
		else
			categories = m_utilities.format_categories({lang:getCanonicalName() .. " terms derived from " .. info.cat_name}, lang, sort_key)
		end
	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)
end

function remove_links(text)
	if type(text) == "table" then text = text.args[1] end; if not text then text = "" end
	text = text:gsub("%[%[Category:[^|%]]-|?[^|%]]-%]%]", "")
	text = text:gsub("%[%[[^|%]]-|", "")
	text = text:gsub("%[%[", "")
	text = text:gsub("%]%]", "")
	return text
end


function preprocess_pr(terminfo)
	terminfo.pr = terminfo.lang:transliterate1(remove_links(terminfo.term), terminfo.sc, false)
end

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

	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.")
		else
			terminfo.term = "-"
		end
	end
	
	if terminfo.lang:getType() == "etymology language" then
		local parent = require("Module:User:Isomorphyc/languages-draft").getByCode(terminfo.lang:getParentCode()) or
		require("Module:families").getByCode(terminfo.lang:getParentCode())

		if not (parent:getType() == "regular" or parent:getType() == "reconstructed" or parent: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.")
			else
				terminfo.term = "-"
			end
		end

		terminfo.lang = parent
	end
	
	local link = ""
	
	if terminfo.term ~= "-" then
		link = " " .. require("Module:User:Isomorphyc/links-draft").full_link(terminfo, "term", true)
	end
	
	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)
	preprocess_pr(terminfo)
	local source = terminfo.lang

	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.")
		else
			terminfo.term = "-"
		end
	end
	
	if terminfo.lang:getType() == "etymology language" then
		local parent = require("Module:User:Isomorphyc/languages-draft").getByCode(terminfo.lang:getParentCode()) or
		require("Module:families").getByCode(terminfo.lang:getParentCode())

		if not (parent:getType() == "regular" or parent:getType() == "reconstructed" or parent: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.")
			else
				terminfo.term = "-"
			end
		end
		
		terminfo.lang = parent
	end

	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 link = ""
	
	if terminfo.term ~= "-" then
		link = " " .. require("Module:User:Isomorphyc/links-draft").full_link(terminfo, "term", true)
	end
	
	local categories = require("Module:utilities").format_categories({lang:getCanonicalName() .. " terms derived from " .. source:getCanonicalName()}, lang, sort_key)
	categories = categories .. require("Module:utilities").format_categories({lang:getCanonicalName() .. " terms inherited from " .. source:getCanonicalName()}, lang, sort_key)
	
	return export.format_etyl(nil, source, sort_key) .. link .. categories
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
	
	preprocess_pr(terminfo)
	local source = terminfo.lang

	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.")
		else
			terminfo.term = "-"
		end
	end
	
	if terminfo.lang:getType() == "etymology language" then
		local parent = require("Module:User:Isomorphyc/languages-draft").getByCode(terminfo.lang:getParentCode()) or
		require("Module:families").getByCode(terminfo.lang:getParentCode())

		if not (parent:getType() == "regular" or parent:getType() == "reconstructed" or parent: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.")
			else
				terminfo.term = "-"
			end
		end
		
		terminfo.lang = parent
	end

	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 derived from " .. source:getCategoryName())
		table.insert(categories, lang:getCanonicalName() .. " terms borrowed from " .. source:getCategoryName())
	else
		table.insert(categories, lang:getCanonicalName() .. " terms derived from " .. source:getCanonicalName())
		table.insert(categories, lang:getCanonicalName() .. " terms borrowed from " .. source:getCanonicalName())
	end
	
	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
	
	local link = ""
	
	if terminfo.term ~= "-" then
		link = " " .. require("Module:User:Isomorphyc/links-draft").full_link(terminfo, "term", true)
	end
	
	categories = require("Module:utilities").format_categories(categories, lang, sort_key)
	
	return (notext and "" or text) .. export.format_etyl(nil, source, sort_key) .. link .. categories
end

-- Internal implementation of {{calque|...}} template
function export.calque(lang, terminfo, parts, sort_key, nocap, notext)
	preprocess_pr(terminfo)
	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 categories = require("Module:utilities").format_categories({lang:getCanonicalName() .. " calques"}, lang, sort_key)
	
	result = result .. " " .. export.format_borrowed(lang, terminfo, sort_key, false, true, false) .. categories
	
	return result
end

return export