Module:etymology/templates: difference between revisions

From Wiktionary, the free dictionary
Jump to navigation Jump to search
Content deleted Content added
m fix variable name
remove Proto- prefix from reconstructed languages
Line 349: Line 349:
local languageName = lang:getCanonicalName()
local languageName = lang:getCanonicalName()
languageName = mw.ustring.gsub(languageName, "^Proto-", "")
local link = require("Module:links").full_link({lang = entryLang, sc = sc, term = term, alt = alt, id = args["id"], tr = args["tr"], genders = args["g"], gloss = args["gloss"], pos = args["pos"], lit = args["lit"]}, face, allowSelfLink)
local link = require("Module:links").full_link({lang = entryLang, sc = sc, term = term, alt = alt, id = args["id"], tr = args["tr"], genders = args["g"], gloss = args["gloss"], pos = args["pos"], lit = args["lit"]}, face, allowSelfLink)

Revision as of 00:34, 1 April 2017

This module generates content for several etymology templates. They include:

Also, the following specialized borrowing templates (not including {{bor}}/{{borrowed}} itself):

Miscellaneous etymology templates that take a single term parameter:

Miscellaneous etymology templates that take no term parameter:

Some templates that used to be here but have now been moved to submodules:


local export = {}

local m_languages = require("Module:languages")

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

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

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 learned = frame.args["learned"]; if not learned or learned == "" then learned = false else learned = true end
	
	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"] = {},
	}
	
	local compat = false
	
	if frame:getParent().args["lang"] then
		compat = true
	end
	
	if compat then
		require("Module:debug").track("borrowing/lang")
		params["lang"] = table.remove(params, 1)
	end
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	
	local lang = fetch_lang(args[compat and "lang" or 1], compat and "lang" or 1)
	local source = fetch_source(args[compat and 1 or 2])
	local sc = fetch_script(args["sc"])

	return require("Module:etymology").format_borrowed(
		lang,
		{lang = source, sc = sc, term = args[compat and 2 or 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"], learned)
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 compat_etyl = false
	
	if frame:getParent().args["etyl lang"] then
		compat_etyl = true
	end
	
	if compat_etyl then
		require("Module:debug").track("calque/etyl")
		
		params["etyl lang"] = {required = true, default = "und"}
		params["etyl term"] = {}
		params["etyl t"] = {}
		params["etyl tr"] = {}
		
		params[2] = {list = true, allow_holes = true}
		params[3] = nil
		params[4] = nil
		params[5] = nil
		
		params["alt"] = {list = true, allow_holes = true}
		params["g"] = nil
		params["gloss"] = {alias_of = "t", list = true, allow_holes = true}
		params["id"] = nil
		params["lit"] = nil
		params["pos"] = {list = true, allow_holes = true}
		params["t"] = {list = true, allow_holes = true}
		params["tr"] = {list = true, allow_holes = true}
	end
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	
	local lang = fetch_lang(args[1], 1)
	local source = fetch_source(args[compat_etyl and "etyl lang" or 2])

	local parts = {}
	
	if compat_etyl then
		local numParts = math.max(args[2].maxindex, args["alt"].maxindex, args["t"].maxindex, args["tr"].maxindex);
		for i = 1, numParts do
			table.insert(parts, {
				term = args[2][i],
				alt = args["alt"][i],
				tr = args["tr"][i],
				gloss = args["t"][i],
				pos = args["pos"][i],
			})
		end
		if numParts > 1 then 
			require("Module:debug").track("calque/parts")
		end
	end
	
	return require("Module:etymology").calque(
		lang,
		{lang = source, term = args[compat_etyl and "etyl term" or 3], alt = not compat_etyl and args[4] or nil, tr = args[compat_etyl and "etyl tr" or "tr"], gloss = args[compat_etyl and "etyl t" or "gloss" or 5]},
		parts, 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"] = { boolean = true },
		["calque"] = { boolean = true },
	}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	
	local lang = args[1]
	local term = args[2]
	local alt = args[3]
	local sc = args["sc"]
	
	if namespace == "Template" then
		if not sc and not 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 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 = require("Module:links").full_link({lang = entryLang, sc = sc, term = term, alt = alt, id = args["id"], tr = args["tr"], genders = args["g"], gloss = args["gloss"], pos = args["pos"], lit = args["lit"]}, face, allowSelfLink)
	
	local arrow = ""
	if args["bor"] or args["calque"] then
		arrow = "→ "
	end
	
	local calque = ""
	if args["calque"] then
		calque = " " .. qualifier("calque")
	end
	
	return table.concat{arrow, languageName, ": ", link, calque}
end

return export