Module:Etymology/templates: Difference between revisions

From The Languages of David J. Peterson
Jump to navigation Jump to search
No edit summary
Tag: Reverted
(Undo revision 536380 by Juelos (talk))
Tag: Undo
 
Line 1: Line 1:
local export = {}
local export = {}


local require_when_needed = require("Module:require when needed")
local m_languages = require("Module:languages")


local concat = table.concat
local format_categories = require_when_needed("Module:utilities", "format_categories")
local insert = table.insert
local process_params = require_when_needed("Module:parameters", "process")
local trim = mw.text.trim
local lower = mw.ustring.lower


local etymology_module = "Module:etymology"
local function fetch_lang(lang, parameter)
local etymology_specialized_module = "Module:etymology/specialized"
return m_languages.getByCode(lang) or m_languages.err(lang, parameter)
local m_internal = require("Module:etymology/templates/internal")
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


-- For testing
local force_cat = false


function export.etyl(frame)
function export.etyl(frame)
local params = {
local params = {
[1] = {required = true, type = "language", default = "und"},
[1] = {required = true, default = "und"},
[2] = {type = "language", default = "en"},
[2] = {},
["sort"] = {},
["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...
-- 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 args = frame:getParent().args
local args = frame:getParent().args
if args[2] and trim(args[2]) == "-" then
params[2] = nil
if args.gloss then
args = process_params({
require("Module:debug").track("cognate/gloss param")
[1] = args[1],
end
["sort"] = args["sort"]
}, params)
local params = {
else
[1] = {required = true, default = "und"},
args = process_params(args, params)
[2] = {},
[3] = {alias_of = "alt"},
[4] = {alias_of = "t"},
["alt"] = {},
["g"] = {list = true},
["id"] = {},
["lit"] = {},
["pos"] = {},
["t"] = {},
["gloss"] = {alias_of = "t"},
["tr"] = {},
["ts"] = {},
["sc"] = {},
["sort"] = {},
}
args = require("Module:parameters").process(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"],
ts = args["ts"],
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"] = {list = true},
["gloss"] = {alias_of = "t"},
["id"] = {},
["lit"] = {},
["pos"] = {},
["t"] = {},
["tr"] = {},
["ts"] = {},
["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"],
ts = args["ts"],
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"] = {list = true},
["gloss"] = {alias_of = "t"},
["id"] = {},
["lit"] = {},
["pos"] = {},
["t"] = {},
["tr"] = {},
["ts"] = {},
["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"],
ts = args["ts"],
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"] = {list = true},
["gloss"] = {alias_of = "t"},
["id"] = {},
["lit"] = {},
["pos"] = {},
["t"] = {},
["tr"] = {},
["ts"] = {},
["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_borrowed(
lang,
{
lang = source,
sc = sc,
term = args[3],
alt = args["alt"],
id = args["id"],
genders = args["g"],
tr = args["tr"],
ts = args["ts"],
gloss = args["t"],
pos = args["pos"],
lit = args["lit"],
},
args["sort"], false, true, "plain")
end
 
 
function export.learned_borrowed(frame)
local args = frame:getParent().args
if args.gloss then
require("Module:debug").track("learned_borrowed/gloss param")
end
local params = {
[1] = {required = true, default = "und"},
[2] = {required = true, default = "und"},
[3] = {},
[4] = {alias_of = "alt"},
[5] = {alias_of = "t"},
["alt"] = {},
["g"] = {list = true},
["gloss"] = {alias_of = "t"},
["id"] = {},
["lit"] = {},
["pos"] = {},
["t"] = {},
["tr"] = {},
["ts"] = {},
["sc"] = {},
["notext"] = {type = "boolean"},
["nocap"] = {type = "boolean"},
["sort"] = {},
}
args = require("Module:parameters").process(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"],
ts = args["ts"],
gloss = args["t"],
pos = args["pos"],
lit = args["lit"],
},
args["sort"], args["nocap"], args["notext"], "learned")
end
 
 
function export.orthographic_borrowed(frame)
local args = frame:getParent().args
if args.gloss then
require("Module:debug").track("orthographic_borrowed/gloss param")
end
end
return require(etymology_module).format_source {
lang = args[2],
local params = {
source = args[1],
[1] = {required = true, default = "und"},
sort_key = args["sort"]
[2] = {required = true, default = "und"},
[3] = {},
[4] = {alias_of = "alt"},
[5] = {alias_of = "t"},
["alt"] = {},
["g"] = {list = true},
["gloss"] = {alias_of = "t"},
["id"] = {},
["lit"] = {},
["pos"] = {},
["t"] = {},
["tr"] = {},
["ts"] = {},
["sc"] = {},
["notext"] = {type = "boolean"},
["nocap"] = {type = "boolean"},
["sort"] = {},
}
}
args = require("Module:parameters").process(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"],
ts = args["ts"],
gloss = args["t"],
pos = args["pos"],
lit = args["lit"],
},
args["sort"], args["nocap"], args["notext"], "orthographic")
end
end




-- Supports various specialized types of borrowings, according to `frame.args.bortype`:
function export.unadapted_borrowed(frame)
--  "learned" = {{lbor}}/{{learned borrowing}}
--  "semi-learned" = {{slbor}}/{{semi-learned borrowing}}
--  "orthographic" = {{obor}}/{{orthographic borrowing}}
--  "unadapted" = {{ubor}}/{{unadapted borrowing}}
--  "calque" = {{cal}}/{{calque}}
--  "partial-calque" = {{pcal}}/{{partial calque}}
--  "semantic-loan" = {{sl}}/{{semantic loan}}
--  "transliteration" = {{translit}}/{{transliteration}}
--  "phono-semantic-matching" = {{psm}}/{{phono-semantic matching}}
function export.specialized_borrowing(frame)
local bortype = frame.args.bortype
local args = frame:getParent().args
local args = frame:getParent().args
if args.gloss then
if args.gloss then
require("Module:debug").track("borrowing/" .. bortype .. "/gloss param")
require("Module:debug").track("unadapted_borrowed/gloss param")
end
end
local params = {
[1] = {required = true, default = "und"},
[2] = {required = true, default = "und"},
[3] = {},
[4] = {alias_of = "alt"},
[5] = {alias_of = "t"},
["alt"] = {},
["g"] = {list = true},
["gloss"] = {alias_of = "t"},
["id"] = {},
["lit"] = {},
["pos"] = {},
["t"] = {},
["tr"] = {},
["ts"] = {},
["sc"] = {},
["notext"] = {type = "boolean"},
["nocap"] = {type = "boolean"},
["sort"] = {},
}
args = require("Module:parameters").process(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"],
ts = args["ts"],
gloss = args["t"],
pos = args["pos"],
lit = args["lit"],
},
args["sort"], args["nocap"], args["notext"], "unadapted")
end


-- More informative error message for {{calque}}, which used to support other params.
function export.calque(frame)
if bortype == "calque" and (args["etyl lang"] or args["etyl term"] or args["etyl t"] or args["etyl tr"]) then
local args = frame:getParent().args
local params = {
[1] = {required = true, default = "und"},
[2] = {required = true, default = "und"},
[3] = {},
[4] = {alias_of = "alt"},
[5] = {alias_of = "t"},
["alt"] = {},
["g"] = {list = true},
["id"] = {},
["lit"] = {},
["pos"] = {},
["t"] = {},
["tr"] = {},
["ts"] = {},
["sort"] = {},
["nocap"] = {type = "boolean"},
["notext"] = {type = "boolean"},
}
-- 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. " ..
error("{{[[Template:calque|calque]]}} no longer supports parameters beginning with etyl. " ..
"The parameters supported are similar to those used by " ..
"The parameters supported are similar to those used by " ..
Line 67: Line 455:
end
end
local lang, term, sources
args = require("Module:parameters").process(args, params)
args, lang, term, sources = m_internal.parse_2_lang_args(frame, "has text")
local m_etymology_specialized = require(etymology_specialized_module)
local lang = fetch_lang(args[1], 1)
if sources then
local source = fetch_source(args[2])
return m_etymology_specialized.specialized_multi_borrowing {
bortype = bortype,
return require("Module:etymology").calque(
lang = lang,
lang,
sc = term.sc,
{
sources = sources,
lang = source,
terminfo = term,
term = args[3],
sort_key = args.sort,
alt = args["alt"],
nocap = args.nocap,
genders = args["g"],
notext = args.notext,
tr = args["tr"],
nocat = args.nocat,
ts = args["ts"],
conj = args.conj,
gloss = args["t"],
senseid = args.senseid,
pos = args["pos"],
}
lit = args["lit"],
else
},
return m_etymology_specialized.specialized_borrowing {
args["sort"],
bortype = bortype,
args["nocap"],
lang = lang,
args["notext"])
terminfo = term,
end
sort_key = args.sort,
 
nocap = args.nocap,
function export.semantic_loan(frame)
notext = args.notext,
nocat = args.nocat,
local params = {
senseid = args.senseid,
[1] = {required = true, default = "und"},
[2] = {required = true, default = "und"},
[3] = {},
[4] = {alias_of = "alt"},
[5] = {alias_of = "t"},
["alt"] = {},
["g"] = {list = true},
["id"] = {},
["lit"] = {},
["pos"] = {},
["t"] = {},
["tr"] = {},
["ts"] = {},
["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"],
ts = args["ts"],
gloss = args["t"],
pos = args["pos"],
lit = args["lit"],
},
args["sort"], args["nocap"], args["notext"])
end
 
function export.psm(frame)
local params = {
[1] = {required = true, default = "und"},
[2] = {required = true, default = "und"},
[3] = {},
[4] = {alias_of = "alt"},
[5] = {alias_of = "t"},
["alt"] = {},
["g"] = {list = true},
["id"] = {},
["lit"] = {},
["pos"] = {},
["t"] = {},
["tr"] = {},
["ts"] = {},
["sc"] = {},
["nocap"] = {type = "boolean"},
["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").psm(
lang,
{
lang = source,
sc = sc,
term = args[3],
alt = args["alt"],
id = args["id"],
genders = args["g"],
tr = args["tr"],
ts = args["ts"],
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
Line 99: Line 588:




-- Implementation of miscellaneous templates such as {{back-formation}}, {{clipping}},
function export.descendant(frame)
-- {{ellipsis}}, {{rebracketing}}, and {{reduplication}} that have a single
local namespace = mw.title.getCurrentTitle().nsText
-- associated term.
do
local params = {
local function get_args(frame)
[1] = { required = true },
local alias_of_t = {alias_of = "t"}
[2] = {},
local boolean = {type = "boolean"}
[3] = {},
local plain = {}
[4] = { alias_of = "gloss" },
local params = {
["g"] = {list = true},
[1] = {required = true, type = "language", default = "und"},
["gloss"] = {},
[2] = plain,
["id"] = {},
[3] = {alias_of = "alt"},
["lit"] = {},
[4] = alias_of_t,
["pos"] = {},
["t"] = { alias_of = "gloss" },
["alt"] = plain,
["tr"] = {},
["gloss"] = alias_of_t,
["ts"] = {},
["g"] = {list = true},
["sc"] = {},
["id"] = plain,
["bor"] = { type = "boolean" },
["lit"] = plain,
["der"] = { type = "boolean" },
["pos"] = plain,
["clq"] = { type = "boolean" },
["t"] = plain,
["cal"] = { alias_of = "clq" },
["tr"] = plain,
["calq"] = { alias_of = "clq" },
["ts"] = plain,
["calque"] = { alias_of = "clq" },
["sc"] = {type = "script"},
["sl"] = { type = "boolean" },
["semantic loan"] = { alias_of = "sl" },
["nocap"] = boolean, -- should be processed in the template itself
["alts"] = { type = "boolean" },
["notext"] = boolean,
["sclb"] = { type = "boolean" },
["nocat"] = boolean,
}
["sort"] = plain,
}
local args = require("Module:parameters").process(frame:getParent().args, params)
-- |ignore-params= parameter to module invocation specifies
-- additional parameter names to allow  in template invocation, separated by
local lang = args[1]
-- commas. They must consist of ASCII letters or numbers or hyphens.
local term = args[2]
local ignore_params = frame.args["ignore-params"]
local alt = args[3]
if ignore_params then
local gloss = args["gloss"]
ignore_params = trim(ignore_params)
local tr = args["tr"]
if not ignore_params:match("^[%w%-,]+$") then
local ts = args["ts"]
error("Invalid characters in |ignore-params=: " .. ignore_params:gsub("[%w%-,]+", ""))
local sc = args["sc"]
end
for param in ignore_params:gmatch("[%w%-]+") do
if namespace == "Template" then
if params[param] then
if not ( sc or lang ) then
error("Duplicate param |" .. param
sc = "Latn"
.. " in |ignore-params=: already specified in params")
end
end
if not lang then
params[param] = plain
lang = "en"
end
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,
ts = ts,
genders = args["g"],
gloss = gloss,
pos = args["pos"],
lit = args["lit"],
},
nil,
true)
elseif ts or gloss then
-- [[Special:WhatLinksHere/Template:tracking/descendant/no term]]
require "Module:debug".track("descendant/no term")
link = require("Module:links").full_link(
{
lang = entryLang,
sc = sc,
ts = ts,
gloss = gloss,
},
nil,
true)
link = link
:gsub("<small>%[Term%?%]</small> ", "")
:gsub("%[%[Category:[^%[%]]+ term requests%]%]", "")
else -- display no link at all
-- [[Special:WhatLinksHere/Template:tracking/descendant/no term or annotations]]
require "Module:debug".track("descendant/no term or annotations")
end
local function add_tooltip(text, tooltip)
return '<span title="' .. tooltip .. '">' .. text .. '</span>'
end
local label, arrow, alts, calque, semantic_loan
if args["sclb"] then
if sc then
label = sc:getCanonicalName()
else
label = require("Module:scripts").findBestScript(term, lang):getCanonicalName()
end
end
return process_params(frame:getParent().args, params)
else
label = languageName
end
if args["bor"] then
arrow = add_tooltip("→ ", "borrowed")
elseif args["clq"] then
arrow = add_tooltip("→ ", "calque")
elseif args["sl"] then
arrow = add_tooltip("→ ", "semantic loan")
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)
end
if args["clq"] then
calque = " " .. qualifier("calque")
else
calque = ""
end
if args["sl"] then
semantic_loan = " " .. qualifier("semantic loan")
else
semantic_loan = ""
end
end
function export.misc_variant(frame)
return table.concat{arrow, label, ": ", link, alts or "", calque, semantic_loan}
local args = get_args(frame)
end
local lang = args[1]
 
local sc = args["sc"]
function export.misc_variant(frame)
local params = {
[1] = {required = true, default = "und"},
[2] = {},
[3] = {alias_of = "alt"},
[4] = {alias_of = "t"},
["alt"] = {},
["gloss"] = {alias_of = "t"},
["g"] = {list = true},
["id"] = {},
["lit"] = {},
["pos"] = {},
["t"] = {},
["tr"] = {},
["ts"] = {},
["sc"] = {},
["nocap"] = {type = "boolean"}, -- should be processed in the template itself
["notext"] = {type = "boolean"},
["nocat"] = {type = "boolean"},
["sort"] = {},
}
local args = require("Module:parameters").process(frame:getParent().args, params)
local lang = fetch_lang(args[1], 1)
local sc = fetch_script(args["sc"])


local parts = {}
local parts = {}
if not args["notext"] then
table.insert(parts, frame.args["text"])
end
if args[2] or args["alt"] then
if not args["notext"] then
if not args["notext"] then
insert(parts, frame.args["text"])
table.insert(parts, " ")
table.insert(parts, frame.args["oftext"] or "of")
table.insert(parts, " ")
end
end
if args[2] or args["alt"] then
table.insert(parts, require("Module:links").full_link(
if not args["notext"] then
{
insert(parts, " ")
lang = lang,
insert(parts, frame.args["oftext"] or "of")
sc = sc,
insert(parts, " ")
term = args[2],
end
alt = args["alt"],
insert(parts, require("Module:links").full_link(
id = args["id"],
{
tr = args["tr"],
lang = lang,
ts = args["ts"],
sc = sc,
genders = args["g"],
term = args[2],
gloss = args["t"],
alt = args["alt"],
pos = args["pos"],
id = args["id"],
lit = args["lit"],
tr = args["tr"],
},
ts = args["ts"],
"term",
genders = args["g"],
true))
gloss = args["t"],
end
pos = args["pos"],
if not args["nocat"] and frame.args["cat"] then
lit = args["lit"],
},
"term"))
end
-- Allow |cat=, |cat2=, |cat3=, etc. They must be sequential. If |cat=
-- is not defined, |cat2= will not be checked. Empty categories are ignored.
local categories = {}
local categories = {}
if not args["nocat"] and frame.args["cat"] then
table.insert(categories, lang:getCanonicalName() .. " " .. frame.args["cat"])
local cat_number
table.insert(parts, require("Module:utilities").format_categories(categories, lang, args["sort"]))
while true do
end
local cat = frame.args["cat" .. (cat_number or "")]
if not cat then break end
cat = trim(cat)
if cat ~= "" then
insert(categories, lang:getFullName() .. " " .. cat)
end
cat_number = (cat_number or 1) + 1
end
end
if #categories > 0 then
insert(
parts,
format_categories(categories, lang, args["sort"], nil, force_cat))
end


return concat(parts)
return table.concat(parts)
end
end
end


Line 208: Line 814:
-- Implementation of miscellaneous templates such as {{unknown}} that have no
-- Implementation of miscellaneous templates such as {{unknown}} that have no
-- associated terms.
-- associated terms.
do
function export.misc_variant_no_term(frame)
local function get_args(frame)
local params = {
local boolean = {type = "boolean"}
[1] = {required = true, default = "und"},
local plain = {}
 
local params = {
["title"] = {},
[1] = {required = true, type = "language", default = "und"},
["nocap"] = {type = "boolean"}, -- should be processed in the template itself
["notext"] = {type = "boolean"},
["nocat"] = {type = "boolean"},
["sort"] = {},
}


["title"] = plain,
["nocap"] = boolean, -- should be processed in the template itself
["notext"] = boolean,
["nocat"] = boolean,
["sort"] = plain,
}
if frame.args["title2_alias"] then
params[2] = {alias_of = "title"}
end
return process_params(frame:getParent().args, params)
end


function export.misc_variant_no_term(frame)
local args = get_args(frame)
local lang = args[1]
local parts = {}
if not args["notext"] then
insert(parts, args["title"] or frame.args["text"])
end
if not args["nocat"] and frame.args["cat"] then
local categories = {}
insert(categories, lang:getFullName() .. " " .. frame.args["cat"])
insert(parts, format_categories(categories, lang, args["sort"], nil, force_cat))
end


return concat(parts)
if frame.args["title2_alias"] then
params[2] = {alias_of = "title"}
end
end
local args = require("Module:parameters").process(frame:getParent().args, params)
local lang = fetch_lang(args[1], 1)


--This function works similarly to misc_variant_no_term(), but with some automatic linking to the glossary in `title`.
local parts = {}
function export.onomatopoeia(frame)
if not args["notext"] then
local args = get_args(frame)
table.insert(parts, args["title"] or frame.args["text"])
 
end
if args["title"] and (lower(args["title"]) == "imitative" or lower(args["title"]) == "imitation") then
if not args["nocat"] and frame.args["cat"] then
args["title"] = "[[Appendix:Glossary#imitative|" .. args["title"] .. "]]"
local categories = {}
end
table.insert(categories, lang:getCanonicalName() .. " " .. frame.args["cat"])
table.insert(parts, require("Module:utilities").format_categories(categories, lang, args["sort"]))
end


local lang = args[1]
return table.concat(parts)
local parts = {}
if not args["notext"] then
insert(parts, args["title"] or frame.args["text"])
end
if not args["nocat"] and frame.args["cat"] then
local categories = {}
insert(categories, lang:getFullName() .. " " .. frame.args["cat"])
insert(parts, format_categories(categories, lang, args["sort"], nil, force_cat))
end
 
return concat(parts)
end
end
end


return export
return export

Latest revision as of 23:40, 5 September 2024

Documentation for this module may be created at Module:Etymology/templates/documentation

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 args = frame:getParent().args
	
	if args.gloss then
		require("Module:debug").track("cognate/gloss param")
	end
	
	local params = {
		[1] = {required = true, default = "und"},
		[2] = {},
		[3] = {alias_of = "alt"},
		[4] = {alias_of = "t"},
		
		["alt"] = {},
		["g"] = {list = true},
		["id"] = {},
		["lit"] = {},
		["pos"] = {},
		["t"] = {},
		["gloss"] = {alias_of = "t"},
		["tr"] = {},
		["ts"] = {},
		["sc"] = {},
		
		["sort"] = {},
	}
	
	args = require("Module:parameters").process(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"],
			ts = args["ts"],
			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"] = {list = true},
		["gloss"] = {alias_of = "t"},
		["id"] = {},
		["lit"] = {},
		["pos"] = {},
		["t"] = {},
		["tr"] = {},
		["ts"] = {},
		["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"],
			ts = args["ts"],
			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"] = {list = true},
		["gloss"] = {alias_of = "t"},
		["id"] = {},
		["lit"] = {},
		["pos"] = {},
		["t"] = {},
		["tr"] = {},
		["ts"] = {},
		["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"],
			ts = args["ts"],
			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"] = {list = true},
		["gloss"] = {alias_of = "t"},
		["id"] = {},
		["lit"] = {},
		["pos"] = {},
		["t"] = {},
		["tr"] = {},
		["ts"] = {},
		["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_borrowed(
		lang,
		{
			lang = source,
			sc = sc,
			term = args[3],
			alt = args["alt"],
			id = args["id"],
			genders = args["g"],
			tr = args["tr"],
			ts = args["ts"],
			gloss = args["t"],
			pos = args["pos"],
			lit = args["lit"],
		},
		args["sort"], false, true, "plain")
end


function export.learned_borrowed(frame)
	local args = frame:getParent().args
	
	if args.gloss then
		require("Module:debug").track("learned_borrowed/gloss param")
	end
	
	local params = {
		[1] = {required = true, default = "und"},
		[2] = {required = true, default = "und"},
		[3] = {},
		[4] = {alias_of = "alt"},
		[5] = {alias_of = "t"},
		
		["alt"] = {},
		["g"] = {list = true},
		["gloss"] = {alias_of = "t"},
		["id"] = {},
		["lit"] = {},
		["pos"] = {},
		["t"] = {},
		["tr"] = {},
		["ts"] = {},
		["sc"] = {},
		
		["notext"] = {type = "boolean"},
		["nocap"] = {type = "boolean"},
		["sort"] = {},
	}
	
	args = require("Module:parameters").process(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"],
			ts = args["ts"],
			gloss = args["t"],
			pos = args["pos"],
			lit = args["lit"],
		},
		args["sort"], args["nocap"], args["notext"], "learned")
end


function export.orthographic_borrowed(frame)
	local args = frame:getParent().args
	
	if args.gloss then
		require("Module:debug").track("orthographic_borrowed/gloss param")
	end
	
	local params = {
		[1] = {required = true, default = "und"},
		[2] = {required = true, default = "und"},
		[3] = {},
		[4] = {alias_of = "alt"},
		[5] = {alias_of = "t"},
		
		["alt"] = {},
		["g"] = {list = true},
		["gloss"] = {alias_of = "t"},
		["id"] = {},
		["lit"] = {},
		["pos"] = {},
		["t"] = {},
		["tr"] = {},
		["ts"] = {},
		["sc"] = {},
		
		["notext"] = {type = "boolean"},
		["nocap"] = {type = "boolean"},
		["sort"] = {},
	}
	
	args = require("Module:parameters").process(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"],
			ts = args["ts"],
			gloss = args["t"],
			pos = args["pos"],
			lit = args["lit"],
		},
		args["sort"], args["nocap"], args["notext"], "orthographic")
end


function export.unadapted_borrowed(frame)
	local args = frame:getParent().args
	
	if args.gloss then
		require("Module:debug").track("unadapted_borrowed/gloss param")
	end
	
	local params = {
		[1] = {required = true, default = "und"},
		[2] = {required = true, default = "und"},
		[3] = {},
		[4] = {alias_of = "alt"},
		[5] = {alias_of = "t"},
		
		["alt"] = {},
		["g"] = {list = true},
		["gloss"] = {alias_of = "t"},
		["id"] = {},
		["lit"] = {},
		["pos"] = {},
		["t"] = {},
		["tr"] = {},
		["ts"] = {},
		["sc"] = {},
		
		["notext"] = {type = "boolean"},
		["nocap"] = {type = "boolean"},
		["sort"] = {},
	}
	
	args = require("Module:parameters").process(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"],
			ts = args["ts"],
			gloss = args["t"],
			pos = args["pos"],
			lit = args["lit"],
		},
		args["sort"], args["nocap"], args["notext"], "unadapted")
end

function export.calque(frame)
	local args = frame:getParent().args
	
	local params = {
		[1] = {required = true, default = "und"},
		[2] = {required = true, default = "und"},
		[3] = {},
		[4] = {alias_of = "alt"},
		[5] = {alias_of = "t"},
		
		["alt"] = {},
		["g"] = {list = true},
		["id"] = {},
		["lit"] = {},
		["pos"] = {},
		["t"] = {},
		["tr"] = {},
		["ts"] = {},
		
		["sort"] = {},
		["nocap"] = {type = "boolean"},
		["notext"] = {type = "boolean"},
	}
	
	-- 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"],
			genders = args["g"],
			tr = args["tr"],
			ts = args["ts"],
			gloss = args["t"],
			pos = args["pos"],
			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"] = {list = true},
		["id"] = {},
		["lit"] = {},
		["pos"] = {},
		["t"] = {},
		["tr"] = {},
		["ts"] = {},
		["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"],
			ts = args["ts"],
			gloss = args["t"],
			pos = args["pos"],
			lit = args["lit"],
		},
		args["sort"], args["nocap"], args["notext"])
end

function export.psm(frame)
	
	local params = {
		[1] = {required = true, default = "und"},
		[2] = {required = true, default = "und"},
		[3] = {},
		[4] = {alias_of = "alt"},
		[5] = {alias_of = "t"},
		
		["alt"] = {},
		["g"] = {list = true},
		["id"] = {},
		["lit"] = {},
		["pos"] = {},
		["t"] = {},
		["tr"] = {},
		["ts"] = {},
		["sc"] = {},
		
		["nocap"] = {type = "boolean"},
		["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").psm(
		lang,
		{
			lang = source,
			sc = sc,
			term = args[3],
			alt = args["alt"],
			id = args["id"],
			genders = args["g"],
			tr = args["tr"],
			ts = args["ts"],
			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"] = {},
		["ts"] = {},
		["sc"] = {},
		["bor"] = { type = "boolean" },
		["der"] = { type = "boolean" },
		["clq"] = { type = "boolean" },
		["cal"] = { alias_of = "clq" },
		["calq"] = { alias_of = "clq" },
		["calque"] = { alias_of = "clq" },
		["sl"] = { type = "boolean" },
		["semantic loan"] = { alias_of = "sl" },
		["alts"] = { type = "boolean" },
		["sclb"] = { 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 gloss = args["gloss"]
	local tr = args["tr"]
	local ts = args["ts"]
	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,
				ts = ts,
				genders = args["g"],
				gloss = gloss,
				pos = args["pos"],
				lit = args["lit"],
			},
			nil,
			true)
	elseif ts or gloss then
		-- [[Special:WhatLinksHere/Template:tracking/descendant/no term]]
		require "Module:debug".track("descendant/no term")
		link = require("Module:links").full_link(
			{
				lang = entryLang,
				sc = sc,
				ts = ts,
				gloss = gloss,
			},
			nil,
			true)
		link = link
			:gsub("<small>%[Term%?%]</small> ", "")
			:gsub("%[%[Category:[^%[%]]+ term requests%]%]", "")
	else -- display no link at all
		-- [[Special:WhatLinksHere/Template:tracking/descendant/no term or annotations]]
		require "Module:debug".track("descendant/no term or annotations")
	end
	
	local function add_tooltip(text, tooltip)
		return '<span title="' .. tooltip .. '">' .. text .. '</span>'
	end
	
	local label, arrow, alts, calque, semantic_loan
	
	if args["sclb"] then
		if sc then
			label = sc:getCanonicalName()
		else
			label = require("Module:scripts").findBestScript(term, lang):getCanonicalName()
		end
	else
		label = languageName
	end
	
	if args["bor"] then
		arrow = add_tooltip("→ ", "borrowed")
	elseif args["clq"] then
		arrow = add_tooltip("→ ", "calque")
	elseif args["sl"] then
		arrow = add_tooltip("→ ", "semantic loan")
	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)
	end
	
	if args["clq"] then
		calque = " " .. qualifier("calque")
	else
		calque = ""
	end
	
	if args["sl"] then
		semantic_loan = " " .. qualifier("semantic loan")
	else
		semantic_loan = ""
	end
	
	return table.concat{arrow, label, ": ", link, alts or "", calque, semantic_loan}
end

function export.misc_variant(frame)
	local params = {
		[1] = {required = true, default = "und"},
		[2] = {},
		[3] = {alias_of = "alt"},
		[4] = {alias_of = "t"},
		
		["alt"] = {},
		["gloss"] = {alias_of = "t"},
		["g"] = {list = true},
		["id"] = {},
		["lit"] = {},
		["pos"] = {},
		["t"] = {},
		["tr"] = {},
		["ts"] = {},
		["sc"] = {},
		
		["nocap"] = {type = "boolean"}, -- should be processed in the template itself
		["notext"] = {type = "boolean"},
		["nocat"] = {type = "boolean"},
		["sort"] = {},
	}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	
	local lang = fetch_lang(args[1], 1)
	local sc = fetch_script(args["sc"])

	local parts = {}
	if not args["notext"] then
		table.insert(parts, frame.args["text"])
	end
	if args[2] or args["alt"] then
		if not args["notext"] then
			table.insert(parts, " ")
			table.insert(parts, frame.args["oftext"] or "of")
			table.insert(parts, " ")
		end
		table.insert(parts, require("Module:links").full_link(
			{
				lang = lang,
				sc = sc,
				term = args[2],
				alt = args["alt"],
				id = args["id"],
				tr = args["tr"],
				ts = args["ts"],
				genders = args["g"],
				gloss = args["t"],
				pos = args["pos"],
				lit = args["lit"],
			},
			"term",
			true))
	end
	if not args["nocat"] and frame.args["cat"] then
		local categories = {}
		table.insert(categories, lang:getCanonicalName() .. " " .. frame.args["cat"])
		table.insert(parts, require("Module:utilities").format_categories(categories, lang, args["sort"]))
	end

	return table.concat(parts)
end


-- Implementation of miscellaneous templates such as {{unknown}} that have no
-- associated terms.
function export.misc_variant_no_term(frame)
	local params = {
		[1] = {required = true, default = "und"},

		["title"] = {},
		["nocap"] = {type = "boolean"}, -- should be processed in the template itself
		["notext"] = {type = "boolean"},
		["nocat"] = {type = "boolean"},
		["sort"] = {},
	}



	if frame.args["title2_alias"] then
		params[2] = {alias_of = "title"}
	end
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	
	local lang = fetch_lang(args[1], 1)

	local parts = {}
	if not args["notext"] then
		table.insert(parts, args["title"] or frame.args["text"])
	end
	if not args["nocat"] and frame.args["cat"] then
		local categories = {}
		table.insert(categories, lang:getCanonicalName() .. " " .. frame.args["cat"])
		table.insert(parts, require("Module:utilities").format_categories(categories, lang, args["sort"]))
	end

	return table.concat(parts)
end

return export