Open main menu

Wiktionary β

This module generates content for several etymology templates:


local export = {}

local m_languages = require("Module:languages")


local function fetch_lang(lang, parameter)
	return m_languages.getByCode(lang) or m_languages.err(lang, parameter)
end


local function fetch_source(code, disallow_family)
	local source =
		m_languages.getByCode(code)
		or require("Module:etymology languages").getByCode(code)
		or not disallow_family and require("Module:families").getByCode(code)
	
	if source then
		return source
	else
		error("The language" .. (not disallow_family and ", family" or "") .. " or etymology language code \"" .. code .. "\" is not valid.")
	end
end


local function fetch_script(sc)
	if sc then
		return require("Module:scripts").getByCode(sc) or error("The script code \"" .. sc .. "\" is not valid.")
	else
		return nil
	end
end


function export.etyl(frame)
	local params = {
		[1] = {required = true, default = "und"},
		[2] = {},
		["sort"] = {},
	}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	
	local source = fetch_source(args[1])
	local lang = args[2]
	local sort_key = args["sort"]
	
	-- Empty language means English, but "-" means no language. Yes, confusing...
	if not lang then
		lang = "en"
	elseif lang == "-" then
		lang = nil
	end
	if lang then
		lang = fetch_lang(lang, 2)
	end
	if lang and (lang:getCode() == "la" or lang:getCode() == "nl") then
		require("Module:debug").track("etyl/" .. lang:getCode())
		require("Module:debug").track("etyl/" .. lang:getCode() .. "/" .. source:getCode())
	end
	
	return require("Module:etymology").format_etyl(lang, source, sort_key)
end


function export.cognate(frame)
	local params = {
		[1] = {required = true, default = "und"},
		[2] = {},
		[3] = {alias_of = "alt"},
		[4] = {alias_of = "t"},
		
		["alt"] = {},
		["g"] = {},
		["gloss"] = { alias_of = "t" },
		["id"] = {},
		["lit"] = {},
		["pos"] = {},
		["t"] = {},
		["tr"] = {},
		["sc"] = {},
		
		["sort"] = {},
	}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	
	local source = fetch_source(args[1])
	local sc = fetch_script(args["sc"])

	return require("Module:etymology").format_cognate(
		{
			lang = source,
			sc = sc,
			term = args[2],
			alt = args["alt"],
			id = args["id"],
			genders = { args["g"] },
			tr = args["tr"],
			gloss = args["t"],
			pos = args["pos"],
			lit = args["lit"]
		},
		args["sort"])
end


function export.noncognate(frame)
	return export.cognate(frame)
end


function export.derived(frame)
	local params = {
		[1] = {required = true, default = "und"},
		[2] = {required = true, default = "und"},
		[3] = {},
		[4] = {alias_of = "alt"},
		[5] = {alias_of = "t"},
		
		["alt"] = {},
		["g"] = {},
		["gloss"] = {alias_of = "t"},
		["id"] = {},
		["lit"] = {},
		["pos"] = {},
		["t"] = {},
		["tr"] = {},
		["sc"] = {},
		
		["sort"] = {},
	}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	
	local lang = fetch_lang(args[1], 1)
	local source = fetch_source(args[2])
	local sc = fetch_script(args["sc"])
	
	return require("Module:etymology").format_derived(
		lang, 
		{
			lang = source,
			sc = sc,
			term = args[3],
			alt = args["alt"],
			id = args["id"],
			genders = { args["g"] },
			tr = args["tr"],
			gloss = args["t"],
			pos = args["pos"],
			lit = args["lit"]
		},
		args["sort"])
end


function export.inherited(frame)
	local params = {
		[1] = {required = true, default = "und"},
		[2] = {required = true, default = "und"},
		[3] = {},
		[4] = {alias_of = "alt"},
		[5] = {alias_of = "t"},
		
		["alt"] = {},
		["g"] = {},
		["gloss"] = {alias_of = "t"},
		["id"] = {},
		["lit"] = {},
		["pos"] = {},
		["t"] = {},
		["tr"] = {},
		["sc"] = {},
		
		["sort"] = {},
	}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	
	local lang = fetch_lang(args[1], 1)
	local source = fetch_source(args[2], "no family")
	local sc = fetch_script(args["sc"])

	return require("Module:etymology").format_inherited(
		lang,
		{
			lang = source,
			sc = sc,
			term = args[3],
			alt = args["alt"],
			id = args["id"],
			genders = {args["g"]},
			tr = args["tr"],
			gloss = args["t"],
			pos = args["pos"],
			lit = args["lit"]
		},
		args["sort"])
end


function export.borrowed(frame)
	local params = {
		[1] = {required = true, default = "und"},
		[2] = {required = true, default = "und"},
		[3] = {},
		[4] = {alias_of = "alt"},
		[5] = {alias_of = "t"},
		
		["alt"] = {},
		["g"] = {},
		["gloss"] = {alias_of = "t"},
		["id"] = {},
		["lit"] = {},
		["pos"] = {},
		["t"] = {},
		["tr"] = {},
		["sc"] = {},
		
		["sort"] = {},
		["withtext"] = {type = "boolean"},
	}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	
	local lang = fetch_lang(args[1], 1)
	local source = fetch_source(args[2])
	local sc = fetch_script(args["sc"])
	
	return require("Module:etymology").format_borrowed(
		lang,
		{lang = source, sc = sc, term = args[3], alt = args["alt"], id = args["id"], genders = {args["g"]}, tr = args["tr"], gloss = args["t"], pos = args["pos"], lit = args["lit"]},
		args["sort"], not args["withtext"], false)
end


function export.learned_borrowed(frame)
	local params = {
		[1] = {required = true, default = "und"},
		[2] = {required = true, default = "und"},
		[3] = {},
		[4] = {alias_of = "alt"},
		[5] = {alias_of = "t"},
		
		["alt"] = {},
		["g"] = {},
		["gloss"] = {alias_of = "t"},
		["id"] = {},
		["lit"] = {},
		["pos"] = {},
		["t"] = {},
		["tr"] = {},
		["sc"] = {},
		
		["notext"] = {type = "boolean"},
		["sort"] = {},
	}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	
	local lang = fetch_lang(args[1], 1)
	local source = fetch_source(args[2])
	local sc = fetch_script(args["sc"])
	
	return require("Module:etymology").format_borrowed(
		lang,
		{lang = source, sc = sc, term = args[3], alt = args["alt"], id = args["id"], genders = {args["g"]}, tr = args["tr"], gloss = args["t"], pos = args["pos"], lit = args["lit"]},
		args["sort"], args["notext"], true)
end


function export.calque(frame)
	local params = {
		[1] = {required = true, default = "und"},
		[2] = {required = true, default = "und"},
		[3] = {},
		[4] = {alias_of = "alt"},
		[5] = {alias_of = "t"},
		
		["alt"] = {},
		["g"] = {},
		["gloss"] = {alias_of = "t"},
		["id"] = {},
		["lit"] = {},
		["pos"] = {},
		["t"] = {},
		["tr"] = {},
		
		["sort"] = {},
		["nocap"] = {type = "boolean"},
		["notext"] = {type = "boolean"},
	}
	
	local args = frame:getParent().args
	
	-- More informative error message.
	if args["etyl lang"] or args["etyl term"] or args["etyl t"] or args["etyl tr"] then
		error("{{[[Template:calque|calque]]}} no longer supports parameters beginning with etyl. " ..
			"The parameters supported are similar to those used by " ..
			"{{[[Template:der|der]]}}, {{[[Template:inh|inh]]}}, " ..
			"{{[[Template:bor|bor]]}}. See [[Template:calque/documentation]] for more.")
	end
	
	args = require("Module:parameters").process(args, params)
	
	local lang = fetch_lang(args[1], 1)
	local source = fetch_source(args[2])
	
	return require("Module:etymology").calque(
		lang,
		{
			lang = source,
			term = args[3],
			alt = args["alt"],
			tr = args["tr"],
			gloss = args["t"],
			lit = args["lit"],
		},
		args["sort"],
		args["nocap"],
		args["notext"])
end

function export.semantic_loan(frame)
	
	local params = {
		[1] = {required = true, default = "und"},
		[2] = {required = true, default = "und"},
		[3] = {},
		[4] = {alias_of = "alt"},
		[5] = {alias_of = "t"},
		
		["alt"] = {},
		["g"] = {},
		["gloss"] = {alias_of = "t"},
		["id"] = {},
		["lit"] = {},
		["pos"] = {},
		["t"] = {},
		["tr"] = {},
		["sc"] = {},
		
		["nocap"] = {type = "boolean"},
		["notext"] = {type = "boolean"},
		["sort"] = {},
		["ger"] = {type = "boolean"},
	}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	
	local lang = fetch_lang(args[1], 1)
	local source = fetch_source(args[2])
	local sc = fetch_script(args["sc"])

	return require("Module:etymology").format_semantic_loan(
		lang,
		{lang = source, sc = sc, term = args[3], alt = args["alt"], id = args["id"], genders = {args["g"]}, tr = args["tr"], gloss = args["t"], pos = args["pos"], lit = args["lit"]},
		args["sort"], args["nocap"], args["notext"])
end

local function qualifier(content)
	if content then
		return table.concat{
			'<span class="ib-brac qualifier-brac">(</span>',
			'<span class="ib-content qualifier-content">',
			content,
			'</span>',
			'<span class="ib-brac qualifier-brac">)</span>'
		}
	end
end


function export.descendant(frame)
	local namespace = mw.title.getCurrentTitle().nsText
	
	local params = {
		[1] = { required = true },
		[2] = {},
		[3] = {},
		[4] = { alias_of = "gloss" },
		["g"] = { list = true },
		["gloss"] = {},
		["id"] = {},
		["lit"] = {},
		["pos"] = {},
		["t"] = { alias_of = "gloss" },
		["tr"] = {},
		["sc"] = {},
		["bor"] = { type = "boolean" },
		["der"] = { type = "boolean" },
		["clq"] = { type = "boolean" },
		["calq"] = { alias_of = "clq" },
		["calque"] = { alias_of = "clq" },
		["alts"] = { type = "boolean" },
	}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	
	local lang = args[1]
	local term = args[2]
	local alt = args[3]
	local tr = args["tr"]
	local sc = args["sc"]
	
	if namespace == "Template" then
		if not ( sc or lang ) then
			sc = "Latn"
		end
		if not lang then
			lang = "en"
		end
		if not term then
			term = "word"
		end
	end
	
	lang = m_languages.getByCode(lang)
		or require("Module:etymology languages").getByCode(lang)
		or m_languages.err(lang, 1)
		
	local entryLang = require("Module:etymology").getNonEtymological(lang)
	
	if lang:getCode() ~= entryLang:getCode() then
		-- [[Special:WhatLinksHere/Template:tracking/descendant/etymological]]
		require("Module:debug").track("descendant/etymological")
		require("Module:debug").track("descendant/etymological/" .. lang:getCode())
	end
	
	if sc then
		sc = require("Module:scripts").getByCode(sc) or error("The script code \"" .. sc .. "\" is not valid.")
	end
	
	local languageName = lang:getCanonicalName()
	languageName = mw.ustring.gsub(languageName, "^Proto%-", "")
	
	local link = ""
	
	if term ~= "-" then
		link = require("Module:links").full_link(
			{
				lang = entryLang,
				sc = sc,
				term = term,
				alt = alt,
				id = args["id"],
				tr = tr,
				genders = args["g"],
				gloss = args["gloss"],
				pos = args["pos"],
				lit = args["lit"],
			},
			nil,
			true
		)
	end
	
	local function add_tooltip(text, tooltip)
		return '<span title="' .. tooltip .. '">' .. text .. '</span>'
	end
	
	local arrow, alts, calque
	
	if args["bor"] then
		arrow = add_tooltip("→ ", "borrowed")
	elseif args["clq"] then
		arrow = add_tooltip("→ ", "calque")
	elseif args["der"] then
		arrow = add_tooltip("⇒ ", "derived by addition of morphemes")
	else
		arrow = ""
	end
	
	if args["alts"] then
		-- [[Special:WhatLinksHere/Template:tracking/desc/alts]]
		require("Module:debug").track("desc/alts")
		alts = require("Module:descendants tree").getAlternativeForms(entryLang, term)
	else
		alts = ""
	end
	
	if args["clq"] then
		calque = " " .. qualifier("calque")
	else
		calque = ""
	end
	
	return table.concat{arrow, languageName, ": ", link, alts, calque}
end

return export