Module:etymology/templates: Difference between revisions

From Linguifex
Jump to navigation Jump to search
No edit summary
No edit summary
(11 intermediate revisions by the same user not shown)
Line 1: Line 1:
local export = {}
local export = {}


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


local rsplit = mw.text.split
local concat = table.concat
local rsubn = mw.ustring.gsub
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 dump = mw.dumpObject


-- version of rsubn() that discards all but the first return value
local etymology_module = "Module:etymology"
local function rsub(term, foo, bar)
local etymology_specialized_module = "Module:etymology/specialized"
local retval = rsubn(term, foo, bar)
local parameter_utilities_module = "Module:parameter utilities"
return retval
 
end
-- For testing
local force_cat = false


local function fetch_lang(lang, parameter)
local allowed_conjs = {"and", "or", ",", "/", "~", ";"}
return m_languages.getByCode(lang) or m_languages.err(lang, parameter)
end


local function parse_etym_args(parent_args, base_params, has_dest_lang)
local m_param_utils = require(parameter_utilities_module)
local param_mods = m_param_utils.construct_param_mods {
{group = {"link", "q", "l", "ref"}},
}


local function fetch_source(code, disallow_family)
local sourcearg, termarg
local source =
if has_dest_lang then
m_languages.getByCode(code)
sourcearg, termarg = 2, 3
or require("Module:etymology languages").getByCode(code)
or not disallow_family and require("Module:families").getByCode(code)
if source then
return source
else
else
error("The language" .. (not disallow_family and ", family" or "") .. " or etymology language code \"" .. code .. "\" is not valid.")
sourcearg, termarg = 1, 2
end
local terms, args = m_param_utils.parse_term_with_inline_modifiers_and_separate_params {
params = base_params,
param_mods = param_mods,
raw_args = parent_args,
termarg = termarg,
track_module = "etymology",
lang = function(args)
return args[sourcearg][#args[sourcearg]]
end,
sc = "sc",
-- Don't do this, doesn't seem to make sense.
-- parse_lang_prefix = true,
make_separate_g_into_list = true,
splitchar = ",",
subitem_param_handling = "last",
}
-- If term param 3= is empty, there will be no terms in terms.terms. To facilitate further code and for
-- compatibility,, insert one. It will display as <small>[Term?]</small>.
if not terms.terms[1] then
terms.terms[1] = {
lang = args[sourcearg][#args[sourcearg]],
sc = args.sc,
}
end
end
end


local function fetch_script(sc)
return terms.terms, args
return require("Module:scripts").getByCode(sc) or error("The script code \"" .. sc .. "\" is not valid.")
end
end




function export.etyl(frame)
function export.parse_2_lang_args(parent_args, has_text, no_family)
local boolean = {type = "boolean"}
local params = {
local params = {
[1] = {required = true, default = "und"},
[1] = {
[2] = {},
required = true,
["sort"] = {},
type = "language",
default = "und"
},
[2] = {
required = true,
sublist = true,
type = "language",
family = not no_family,
default = "und"
},
[3] = true,
[4] = {alias_of = "alt"},
[5] = {alias_of = "t"},
 
["senseid"] = true,
["nocat"] = boolean,
["sort"] = true,
["sourceconj"] = true,
["conj"] = {set = allowed_conjs, default = ","},
}
}
if has_text then
local args = require("Module:parameters").process(frame:getParent().args, params)
params["notext"] = boolean
params["nocap"] = boolean
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
end
if lang then
lang = fetch_lang(lang, 2)
end
return m_etymology.format_etyl(lang, source, sort_key)
end
function export.cognate(frame)
local args = frame:getParent().args
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 m_etymology.format_cognate(
return parse_etym_args(parent_args, params, "has dest lang")
{
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
end




function export.noncognate(frame)
-- Implementation of deprecated {{etyl}}. Provided to make histories more legible.
return export.cognate(frame)
function export.etyl(frame)
end
 
local function parse_2_lang_args(frame, has_text, no_family)
local params = {
local params = {
[1] = {required = true, default = "und"},
[1] = {required = true, type = "language", default = "und"},
[2] = {required = true, default = "und"},
[2] = {type = "language", default = "en"},
[3] = {},
[4] = {alias_of = "alt"},
[5] = {alias_of = "t"},
["alt"] = {},
["g"] = {list = true},
["gloss"] = {alias_of = "t"},
["id"] = {},
["lit"] = {},
["pos"] = {},
["t"] = {},
["tr"] = {},
["ts"] = {},
["sc"] = {},
 
["nocat"] = {type = "boolean"},
["sort"] = {},
["sort"] = {},
}
}
 
-- Empty language means English, but "-" means no language. Yes, confusing...
if has_text then
local args = frame:getParent().args
params["notext"] = {type = "boolean"}
if args[2] and trim(args[2]) == "-" then
params["nocap"] = {type = "boolean"}
params[2] = nil
args = process_params({
[1] = args[1],
["sort"] = args.sort
}, params)
else
args = process_params(args, params)
end
end
 
return require(etymology_module).format_source {
local args = require("Module:parameters").process(frame:getParent().args, params)
lang = args[2],
source = args[1],
local lang = fetch_lang(args[1], 1)
sort_key = args.sort,
local source = fetch_source(args[2], no_family)
force_cat = force_cat,
local sc = fetch_script(args["sc"])
 
return args, 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"],
nocont = true,
}
}
end
end


-- Implementation of {{derived}}/{{der}}.
function export.derived(frame)
function export.derived(frame)
local args, lang, term = parse_2_lang_args(frame)
local parent_args = frame:getParent().args
return m_etymology.format_derived(lang, term, args["sort"], args["nocat"], "derived")
local terms, args = export.parse_2_lang_args(parent_args)
end
return require(etymology_module).format_derived {
 
lang = args[1],
function export.inherited(frame)
sources = args[2],
local args, lang, term = parse_2_lang_args(frame, nil, "no family")
terms = terms,
return m_etymology.format_inherited(lang, term, args["sort"], args["nocat"])
sort_key = args.sort,
nocat = args.nocat,
sourceconj = args.sourceconj,
conj = args.conj,
template_name = "derived",
force_cat = force_cat,
}
end
end


-- Implementation of {{borrowed}}/{{bor}}.
function export.borrowed(frame)
function export.borrowed(frame)
local args, lang, term = parse_2_lang_args(frame)
local parent_args = frame:getParent().args
return m_etymology.format_borrowed(lang, term, args["sort"],
local terms, args = export.parse_2_lang_args(parent_args)
false, true, args["nocat"], "plain")
return require(etymology_module).format_borrowed {
lang = args[1],
sources = args[2],
terms = terms,
sort_key = args.sort,
nocat = args.nocat,
sourceconj = args.sourceconj,
conj = args.conj,
force_cat = force_cat,
}
end
end


function export.learned_borrowing(frame)
function export.inherited(frame)
 
local parent_args = frame:getParent().args
local args, lang, term = parse_2_lang_args(frame, "has text")
local terms, args = export.parse_2_lang_args(parent_args)
return m_etymology.format_borrowed(lang, term, args["sort"],
local sources = args[2]
args["nocap"], args["notext"], args["nocat"], "learned")
if sources[2] then
end
-- Because this doesn't really make sense.
 
error("[[Template:inherited]] doesn't support multiple comma-separated sources")
function export.semi_learned_borrowing(frame)
 
local args, lang, term = parse_2_lang_args(frame, "has text")
return m_etymology.format_borrowed(lang, term, args["sort"],
args["nocap"], args["notext"], args["nocat"], "semi-learned")
end
 
function export.orthographic_borrowing(frame)
 
local args, lang, term = parse_2_lang_args(frame, "has text")
return m_etymology.format_borrowed(lang, term, args["sort"],
args["nocap"], args["notext"], args["nocat"], "orthographic")
end
 
function export.unadapted_borrowing(frame)
 
local args, lang, term = parse_2_lang_args(frame, "has text")
return m_etymology.format_borrowed(lang, term, args["sort"],
args["nocap"], args["notext"], args["nocat"], "unadapted")
end
 
function export.calque(frame)
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
end
return require(etymology_module).format_inherited {
local args, lang, term = parse_2_lang_args(frame, "has text")
lang = args[1],
return m_etymology.calque(lang, term, args["sort"],
terms = terms,
args["nocap"], args["notext"], args["nocat"])
sort_key = args.sort,
nocat = args.nocat,
conj = args.conj,
force_cat = force_cat,
}
end
end


function export.partial_calque(frame)
function export.cognate(frame)
local params = {
[1] = {
required = true,
sublist = true,
type = "language",
family = true,
default = "und"
},
[2] = true,
[3] = {alias_of = "alt"},
[4] = {alias_of = "t"},
sourceconj = true,
["conj"] = {set = allowed_conjs, default = ","},
sort = true,
}


local args, lang, term = parse_2_lang_args(frame, "has text")
local parent_args = frame:getParent().args
return m_etymology.partial_calque(lang, term, args["sort"],
local terms, args = parse_etym_args(parent_args, params, false)
args["nocap"], args["notext"], args["nocat"])
end


function export.semantic_loan(frame)
return require(etymology_module).format_cognate {
local args, lang, term = parse_2_lang_args(frame, "has text")
sources = args[1],
return m_etymology.semantic_loan(lang, term, args["sort"],
terms = terms,
args["nocap"], args["notext"], args["nocat"])
sort_key = args.sort,
sourceconj = args.sourceconj,
conj = args.conj,
force_cat = force_cat,
}
end
end


function export.psm(frame)
function export.noncognate(frame)
local args, lang, term = parse_2_lang_args(frame, "has text")
return export.cognate(frame)
return m_etymology.phono_semantic_matching(lang, term, args["sort"],
args["nocap"], args["notext"], args["nocat"])
end
end


local function qualifier(content)
-- Supports various specialized types of borrowings, according to `frame.args.bortype`:
if content then
--  "learned" = {{lbor}}/{{learned borrowing}}
return table.concat{
--  "semi-learned" = {{slbor}}/{{semi-learned borrowing}}
'<span class="ib-brac qualifier-brac">(</span>',
--  "orthographic" = {{obor}}/{{orthographic borrowing}}
'<span class="ib-content qualifier-content">',
--  "unadapted" = {{ubor}}/{{unadapted borrowing}}
content,
--  "calque" = {{cal}}/{{calque}}
'</span>',
--  "partial-calque" = {{pcal}}/{{partial calque}}
'<span class="ib-brac qualifier-brac">)</span>'
-"semantic-loan" = {{sl}}/{{semantic loan}}
}
--  "transliteration" = {{translit}}/{{transliteration}}
end
--  "phono-semantic-matching" = {{psm}}/{{phono-semantic matching}}
function export.specialized_borrowing(frame)
local parent_args = frame:getParent().args
local terms, args = export.parse_2_lang_args(parent_args, "has text")
local m_etymology_specialized = require(etymology_specialized_module)
return m_etymology_specialized.specialized_borrowing {
bortype = frame.args.bortype,
lang = args[1],
sources = args[2],
terms = terms,
sort_key = args.sort,
nocap = args.nocap,
notext = args.notext,
nocat = args.nocat,
sourceconj = args.sourceconj,
conj = args.conj,
senseid = args.senseid,
force_cat = force_cat,
}
end
end


local function desc_or_desc_tree(frame, desc_tree)
local params
if desc_tree then
params = {
[1] = {required = true, default = "gem-pro"},
[2] = {required = true, default = "*fuhsaz"},
["notext"] = { type = "boolean" },
["noalts"] = { type = "boolean" },
["noparent"] = { type = "boolean" },
}
else
params = {
[1] = { required = true },
[2] = {},
["alts"] = { type = "boolean" }
}
end


for k, v in pairs({
-- Implementation of miscellaneous templates such as {{abbrev}}, {{back-formation}}, {{clipping}}, {{ellipsis}},
[3] = {},
-- {{rebracketing}} and {{reduplication}} that have a single associated term.
[4] = { alias_of = "gloss" },
function export.misc_variant(frame)
["g"] = {list = true},
local iparams = {
["gloss"] = {},
["ignore-params"] = true,
["id"] = {},
text = {required = true},
["lit"] = {},
oftext = true,
["pos"] = {},
cat = {list = true}, -- allow and compress holes
["t"] = { alias_of = "gloss" },
conj = true,
["tr"] = {},
}
["ts"] = {},
["sc"] = {},
["bor"] = { type = "boolean" },
["lbor"] = { type = "boolean" },
["slb"] = { type = "boolean" },
["der"] = { type = "boolean" },
["clq"] = { type = "boolean" },
["cal"] = { alias_of = "clq" },
["calq"] = { alias_of = "clq" },
["calque"] = { alias_of = "clq" },
["pclq"] = { type = "boolean" },
["sml"] = { type = "boolean" },
["unc"] = { type = "boolean" },
["sclb"] = { type = "boolean" },
["nolb"] = { type = "boolean" },
["q"] = {},
["sandbox"] = { type = "boolean" },
}) do
params[k] = v
end


local namespace = mw.title.getCurrentTitle().nsText
local iargs = process_params(frame.args, iparams)


local args
local boolean = {type = "boolean"}
if frame.args[1] then
args = require("Module:parameters").process(frame.args, params)
else
args = require("Module:parameters").process(frame:getParent().args, params)
end


if args.sandbox then
if namespace == "" or namespace == "Reconstruction" then
error('The sandbox module, Module:descendants tree/sandbox, should not be used in entries.')
end
end
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"]
local id = args["id"]
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 = m_etymology.getNonEtymological(lang)
if not desc_tree and entryLang:getType() == "family" then
error("Cannot use language family code in [[Template:desc]].")
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 = id,
tr = tr,
ts = ts,
genders = args["g"],
gloss = gloss,
pos = args["pos"],
lit = args["lit"],
},
nil,
true)
elseif ts or gloss or #args["g"] > 0 then
link = require("Module:links").full_link(
{
lang = entryLang,
sc = sc,
ts = ts,
gloss = gloss,
genders = args["g"],
},
nil,
true)
link = link
:gsub("<small>%[Term%?%]</small> ", "")
:gsub("<small>%[Term%?%]</small>&nbsp;", "")
:gsub("%[%[Category:[^%[%]]+ term requests%]%]", "")
end
local function add_tooltip(text, tooltip)
return '<span class="desc-arr" title="' .. tooltip .. '">' .. text .. '</span>'
end
local label, arrow, descendants, alts, semi_learned, calque, partial_calque, semantic_loan, qual
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["lbor"] then
arrow = add_tooltip("→", "learned borrowed")
elseif args["slb"] then
arrow = add_tooltip("→", "semi-learned borrowing")
elseif args["clq"] then
arrow = add_tooltip("→", "calque")
elseif args["pclq"] then
arrow = add_tooltip("→", "partial calque")
elseif args["sml"] then
arrow = add_tooltip("→", "semantic loan")
elseif args["unc"] and not args["der"] then
arrow = add_tooltip(">", "inherited")
else
arrow = ""
end
-- allow der=1 in conjunction with bor=1 to indicate e.g. English "pars recta"
-- derived and borrowed from Latin "pars".
if args["der"] then
arrow = arrow .. add_tooltip("⇒", "reshaped by analogy or addition of morphemes")
end
if args["unc"]then
arrow = arrow .. add_tooltip("?", "uncertain")
end
local m_desctree
if desc_tree or args["alts"] then
if args.sandbox or require("Module:yesno")(frame.args.sandbox, false) then
m_desctree = require("Module:descendants tree/sandbox")
else
m_desctree = require("Module:descendants tree")
end
end
if desc_tree then
descendants = m_desctree.getDescendants(entryLang, term, id)
end
if desc_tree and not args["noalts"] or not desc_tree and args["alts"] then
alts = m_desctree.getAlternativeForms(entryLang, term, id)
end
if args["lbor"] then
learned = " " .. qualifier("learned")
else
learned = ""
end
if args["slb"] then
semi_learned = " " .. qualifier("semi-learned")
else
semi_learned = ""
end
if args["clq"] then
calque = " " .. qualifier("calque")
else
calque = ""
end
if args["pclq"] then
partial_calque = " " .. qualifier("partial calque")
else
partial_calque = ""
end
if args["sml"] then
semantic_loan = " " .. qualifier("semantic loan")
else
semantic_loan = ""
end
if args["q"] then
qual = " " .. require("Module:qualifier").format_qualifier(args["q"])
else
qual = ""
end
if args["noparent"] then
return descendants
end
if arrow and arrow ~= "" then
arrow = arrow .. " "
end
local linktext = table.concat{link, alts or "", learned, semi_learned, calque,
partial_calque, semantic_loan, qual, descendants or ""}
if args["notext"] then
return linktext
elseif args["nolb"] then
return arrow .. linktext
else
return table.concat{arrow, label, ":", linktext ~= "" and " " or "", linktext}
end
end
function export.descendant(frame)
return desc_or_desc_tree(frame, false) .. require("Module:TemplateStyles")("Template:descendant/style.css")
end
function export.descendants_tree(frame)
return desc_or_desc_tree(frame, true)
end
-- Implementation of miscellaneous templates such as {{back-formation}}, {{clipping}},
-- {{ellipsis}}, {{rebracketing}}, and {{reduplication}} that have a single
-- associated term.
function export.misc_variant(frame)
local params = {
local params = {
[1] = {required = true, default = "und"},
[1] = {required = true, type = "language", default = "und"},
[2] = {},
[2] = true,
[3] = {alias_of = "alt"},
[3] = {alias_of = "alt"},
[4] = {alias_of = "t"},
[4] = {alias_of = "t"},
 
["alt"] = {},
nocap = boolean, -- should be processed in the template itself
["gloss"] = {alias_of = "t"},
notext = boolean,
["g"] = {list = true},
nocat = boolean,
["id"] = {},
conj = {set = allowed_conjs},
["lit"] = {},
sort = true,
["pos"] = {},
["t"] = {},
["tr"] = {},
["ts"] = {},
["sc"] = {},
["nocap"] = {type = "boolean"}, -- should be processed in the template itself
["notext"] = {type = "boolean"},
["nocat"] = {type = "boolean"},
["sort"] = {},
}
}
 
-- |ignore-params= parameter to module invocation specifies
-- |ignore-params= parameter to module invocation specifies
-- additional parameter names to allow  in template invocation, separated by
-- additional parameter names to allow  in template invocation, separated by
-- commas. They must consist of ASCII letters or numbers or hyphens.
-- commas. They must consist of ASCII letters or numbers or hyphens.
local ignore_params = frame.args["ignore-params"]
local ignore_params = iargs["ignore-params"]
if ignore_params then
if ignore_params then
ignore_params = mw.text.trim(ignore_params)
ignore_params = trim(ignore_params)
if not ignore_params:match "^[%w%-,]+$" then
if not ignore_params:match("^[%w%-,]+$") then
error("Invalid characters in |ignore-params=: " .. ignore_params:gsub("[%w%-,]+", ""))
error("Invalid characters in |ignore-params=: " .. ignore_params:gsub("[%w%-,]+", ""))
end
end
for param in ignore_params:gmatch "[%w%-]+" do
for param in ignore_params:gmatch("[%w%-]+") do
if params[param] then
if params[param] then
error("Duplicate param |" .. param
error("Duplicate param |" .. param
.. " in |ignore-params=: already specified in params")
.. " in |ignore-params=: already specified in params")
end
end
params[param] = {}
params[param] = true
end
end
end
end
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 m_param_utils = require(parameter_utilities_module)
if not args["notext"] then
local param_mods = m_param_utils.construct_param_mods {
table.insert(parts, frame.args["text"])
{group = {"link", "q", "l", "ref"}},
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
-- 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 = {}
if not args["nocat"] and frame.args["cat"] then
local cat_number
while true do
local cat = frame.args["cat" .. (cat_number or "")]
if not cat then break end
cat = mw.text.trim(cat)
if cat ~= "" then
table.insert(categories, lang:getCanonicalName() .. " " .. cat)
end
cat_number = (cat_number or 1) + 1
end
end
if #categories > 0 then
table.insert(
parts,
require("Module:utilities").format_categories(categories, lang, args["sort"]))
end


return table.concat(parts)
local parent_args = frame:getParent().args
end


local terms, args = m_param_utils.parse_term_with_inline_modifiers_and_separate_params {
params = params,
param_mods = param_mods,
raw_args = parent_args,
termarg = 2,
track_module = "etymology",
-- Don't set lang here as we want to know whether there was a lang prefix or not.
sc = "sc",
parse_lang_prefix = true,
allow_multiple_lang_prefixes = true,
make_separate_g_into_list = true,
splitchar = ",",
subitem_param_handling = "last",
}


local function get_parsed_part(template, lang, args, terms, i)
return require(etymology_module).format_misc_variant {
local term = terms[i]
lang = args[1],
local alt = args["alt"][i]
notext = args.notext,
local id = args["id"][i]
text = iargs.text,
local sc = fetch_script(args["sc"][i])
oftext = iargs.oftext,
 
terms = terms.terms,
local tr = args["tr"][i]
sort_key = args.sort,
local ts = args["ts"][i]
conj = args.conj or iargs.conj or "and",
local gloss = args["t"][i]
nocat = args.nocat,
local pos = args["pos"][i]
cats = iargs.cat,
local lit = args["lit"][i]
force_cat = force_cat,
local g = args["g"][i]
}
 
if term or alt or tr or ts then
return require("Module:links").full_link(
{ term = term, alt = alt, id = id, lang = lang, sc = sc, tr = tr,
ts = ts, gloss = gloss, pos = pos, lit = lit,
genders = g and rsplit(g, ",") or {}
}, "term", true)
end
end
end




local function get_parsed_parts(template, lang, args, terms)
-- Implementation of miscellaneous templates such as {{doublet}} that can take multiple terms. Doesn't handle {{blend}}
local parts = {}
-- or {{univerbation}}, which display + signs between elements and use compound_like in [[Module:affix/templates]].
function export.misc_variant_multiple_terms(frame)
local iparams = {
text = {required = true},
oftext = true,
cat = {list = true}, -- allow and compress holes
conj = true,
}


-- Find the maximum index among any of the list parameters.
local iargs = process_params(frame.args, iparams)
local maxmaxindex = 0
for k, v in pairs(args) do
if type(v) == "table" and v.maxindex and v.maxindex > maxmaxindex then
maxmaxindex = v.maxindex
end
end


for index = 1, maxmaxindex do
local boolean = {type = "boolean"}
table.insert(parts, get_parsed_part(template, lang, args, terms, index))
end
return parts
end


-- Implementation of miscellaneous templates such as {{doublet}} that can take
-- multiple terms. Doesn't handle {{blend}} or {{univerbation}}, which display
-- + signs between elements and use compound_like in [[Module:compound/templates]].
function export.misc_variant_multiple_terms(frame)
local params = {
local params = {
[1] = {required = true, default = "und"},
[1] = {required = true, type = "language", template_default = "und"},
[2] = {list = true, allow_holes = true},
[2] = {list = true, allow_holes = true},
nocap = boolean, -- should be processed in the template itself
notext = boolean,
nocat = boolean,
conj = {set = allowed_conjs},
sort = true,
}
    local m_param_utils = require(parameter_utilities_module)
local param_mods = m_param_utils.construct_param_mods {
-- We want to require an index for all params.
{default = true, require_index = true},
{group = {"link", "q", "l", "ref"}},
}


["t"] = {list = true, allow_holes = true, require_index = true},
local parent_args = frame:getParent().args
["gloss"] = {list = true, allow_holes = true, require_index = true, alias_of = "t"},
["tr"] = {list = true, allow_holes = true, require_index = true},
["ts"] = {list = true, allow_holes = true, require_index = true},
["g"] = {list = true, allow_holes = true, require_index = true},
["id"] = {list = true, allow_holes = true, require_index = true},
["alt"] = {list = true, allow_holes = true, require_index = true},
["lit"] = {list = true, allow_holes = true, require_index = true},
["pos"] = {list = true, allow_holes = true, require_index = true},
["sc"] = {list = true, allow_holes = true, require_index = true},


["nocap"] = {type = "boolean"}, -- should be processed in the template itself
local terms, args = m_param_utils.parse_list_with_inline_modifiers_and_separate_params {
["notext"] = {type = "boolean"},
params = params,
["nocat"] = {type = "boolean"},
param_mods = param_mods,
["sort"] = {},
raw_args = parent_args,
termarg = 2,
parse_lang_prefix = true,
allow_multiple_lang_prefixes = true,
track_module = "etymology-templates-doublet",
disallow_custom_separators = true,
-- For compatibility, we need to not skip completely unspecified items. It is common, for example, to do
-- {{suffix|lang||foo}} to generate "+ -foo".
dont_skip_items = true,
-- Don't set lang here as we want to know whether there was a lang prefix or not.
sc = "sc.default",
}
}


local args = require("Module:parameters").process(frame:getParent().args, params)
return require(etymology_module).format_misc_variant {
lang = args[1],
local lang = fetch_lang(args[1], 1)
notext = args.notext,
text = iargs.text,
oftext = iargs.oftext,
terms = terms,
sort_key = args.sort,
conj = args.conj or iargs.conj or "and",
nocat = args.nocat,
cats = iargs.cat,
force_cat = force_cat,
}
end


local parts = {}
-- Implementation of miscellaneous templates such as {{unknown}} that have no associated terms.
if not args["notext"] then
do
table.insert(parts, frame.args["text"])
local function get_args(frame)
end
local boolean = {type = "boolean"}
if #args[2] > 0 or #args["alt"] > 0 then
local params = {
if not args["notext"] then
[1] = {required = true, type = "language", default = "und"},
table.insert(parts, " ")
 
table.insert(parts, frame.args["oftext"] or "of")
["title"] = true,
table.insert(parts, " ")
["nocap"] = boolean, -- should be processed in the template itself
["notext"] = boolean,
["nocat"] = boolean,
["sort"] = true,
}
if frame.args.title2_alias then
params[2] = {alias_of = "title"}
end
end
local formatted_terms = get_parsed_parts(mw.ustring.lower(
return process_params(frame:getParent().args, params)
-- Remove link and convert uppercase to lowercase to get an
-- approximation of the original template name.
rsub(rsub(frame.args["text"], "^%[%[.*|", ""), "%]%]$", "")),
lang, args, args[2])
table.insert(parts, require("Module:table").serialCommaJoin(formatted_terms))
end
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


function export.misc_variant_no_term(frame)
local args = get_args(frame)


-- Implementation of miscellaneous templates such as {{unknown}} that have no
return require(etymology_module).format_misc_variant_no_term {
-- associated terms.
lang = args[1],
function export.misc_variant_no_term(frame)
notext = args.notext,
local params = {
title = args.title or frame.args.text,
[1] = {required = true, default = "und"},
nocat = args.nocat,
cat = frame.args.cat,
sort_key = args.sort,
force_cat = force_cat,
}
end


["title"] = {},
-- This function works similarly to misc_variant_no_term(), but with some automatic linking to the glossary in
["nocap"] = {type = "boolean"}, -- should be processed in the template itself
-- `title`.
["notext"] = {type = "boolean"},
function export.onomatopoeia(frame)
["nocat"] = {type = "boolean"},
local args = get_args(frame)
["sort"] = {},
}


if frame.args["title2_alias"] then
local title = args.title
params[2] = {alias_of = "title"}
if title and (lower(title) == "imitative" or lower(title) == "imitation") then
end
title = "[[wikt:Appendix:Glossary#imitative|" .. title .. "]]"
end
local args = require("Module:parameters").process(frame:getParent().args, params)
local lang = fetch_lang(args[1], 1)


local parts = {}
return require(etymology_module).format_misc_variant_no_term {
if not args["notext"] then
lang = args[1],
table.insert(parts, args["title"] or frame.args["text"])
notext = args.notext,
title = title or frame.args.text,
nocat = args.nocat,
cat = frame.args.cat,
sort_key = args.sort,
force_cat = force_cat,
}
end
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
end


return export
return export

Revision as of 23:16, 24 March 2026



local export = {}

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

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 dump = mw.dumpObject

local etymology_module = "Module:etymology"
local etymology_specialized_module = "Module:etymology/specialized"
local parameter_utilities_module = "Module:parameter utilities"

-- For testing
local force_cat = false

local allowed_conjs = {"and", "or", ",", "/", "~", ";"}

local function parse_etym_args(parent_args, base_params, has_dest_lang)
	local m_param_utils = require(parameter_utilities_module)
	local param_mods = m_param_utils.construct_param_mods {
		{group = {"link", "q", "l", "ref"}},
	}

	local sourcearg, termarg
	if has_dest_lang then
		sourcearg, termarg = 2, 3
	else
		sourcearg, termarg = 1, 2
	end
	local terms, args = m_param_utils.parse_term_with_inline_modifiers_and_separate_params {
		params = base_params,
		param_mods = param_mods,
		raw_args = parent_args,
		termarg = termarg,
		track_module = "etymology",
		lang = function(args)
			return args[sourcearg][#args[sourcearg]]
		end,
		sc = "sc",
		-- Don't do this, doesn't seem to make sense.
		-- parse_lang_prefix = true,
		make_separate_g_into_list = true,
		splitchar = ",",
		subitem_param_handling = "last",
	}
	-- If term param 3= is empty, there will be no terms in terms.terms. To facilitate further code and for
	-- compatibility,, insert one. It will display as <small>[Term?]</small>.
	if not terms.terms[1] then
		terms.terms[1] = {
			lang = args[sourcearg][#args[sourcearg]],
			sc = args.sc,
		}
	end

	return terms.terms, args
end


function export.parse_2_lang_args(parent_args, has_text, no_family)
	local boolean = {type = "boolean"}
	local params = {
		[1] = {
			required = true,
			type = "language",
			default = "und"
		},
		[2] = {
			required = true,
			sublist = true,
			type = "language",
			family = not no_family,
			default = "und"
		},
		[3] = true,
		[4] = {alias_of = "alt"},
		[5] = {alias_of = "t"},

		["senseid"] = true,
		["nocat"] = boolean,
		["sort"] = true,
		["sourceconj"] = true,
		["conj"] = {set = allowed_conjs, default = ","},
	}
	if has_text then
		params["notext"] = boolean
		params["nocap"] = boolean
	end

	return parse_etym_args(parent_args, params, "has dest lang")
end


-- Implementation of deprecated {{etyl}}. Provided to make histories more legible.
function export.etyl(frame)
	local params = {
		[1] = {required = true, type = "language", default = "und"},
		[2] = {type = "language", default = "en"},
		["sort"] = {},
	}
	-- Empty language means English, but "-" means no language. Yes, confusing...
	local args = frame:getParent().args
	if args[2] and trim(args[2]) == "-" then
		params[2] = nil
		args = process_params({
			[1] = args[1],
			["sort"] = args.sort
		}, params)
	else
		args = process_params(args, params)
	end
	return require(etymology_module).format_source {
		lang = args[2],
		source = args[1],
		sort_key = args.sort,
		force_cat = force_cat,
	}
end


-- Implementation of {{derived}}/{{der}}.
function export.derived(frame)
	local parent_args = frame:getParent().args
	local terms, args = export.parse_2_lang_args(parent_args)
	return require(etymology_module).format_derived {
		lang = args[1],
		sources = args[2],
		terms = terms,
		sort_key = args.sort,
		nocat = args.nocat,
		sourceconj = args.sourceconj,
		conj = args.conj,
		template_name = "derived",
		force_cat = force_cat,
	}
end

-- Implementation of {{borrowed}}/{{bor}}.
function export.borrowed(frame)
	local parent_args = frame:getParent().args
	local terms, args = export.parse_2_lang_args(parent_args)
	return require(etymology_module).format_borrowed {
		lang = args[1],
		sources = args[2],
		terms = terms,
		sort_key = args.sort,
		nocat = args.nocat,
		sourceconj = args.sourceconj,
		conj = args.conj,
		force_cat = force_cat,
	}
end

function export.inherited(frame)
	local parent_args = frame:getParent().args
	local terms, args = export.parse_2_lang_args(parent_args)
	local sources = args[2]
	if sources[2] then
		-- Because this doesn't really make sense.
		error("[[Template:inherited]] doesn't support multiple comma-separated sources")
	end
	return require(etymology_module).format_inherited {
		lang = args[1],
		terms = terms,
		sort_key = args.sort,
		nocat = args.nocat,
		conj = args.conj,
		force_cat = force_cat,
	}
end

function export.cognate(frame)
	local params = {
		[1] = {
			required = true,
			sublist = true,
			type = "language",
			family = true,
			default = "und"
		},
		[2] = true,
		[3] = {alias_of = "alt"},
		[4] = {alias_of = "t"},
		sourceconj = true,
		["conj"] = {set = allowed_conjs, default = ","},
		sort = true,
	}

	local parent_args = frame:getParent().args
	local terms, args = parse_etym_args(parent_args, params, false)

	return require(etymology_module).format_cognate {
		sources = args[1],
		terms = terms,
		sort_key = args.sort,
		sourceconj = args.sourceconj,
		conj = args.conj,
		force_cat = force_cat,
	}
end

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

-- Supports various specialized types of borrowings, according to `frame.args.bortype`:
--   "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 parent_args = frame:getParent().args
	local terms, args = export.parse_2_lang_args(parent_args, "has text")
	local m_etymology_specialized = require(etymology_specialized_module)
	return m_etymology_specialized.specialized_borrowing {
		bortype = frame.args.bortype,
		lang = args[1],
		sources = args[2],
		terms = terms,
		sort_key = args.sort,
		nocap = args.nocap,
		notext = args.notext,
		nocat = args.nocat,
		sourceconj = args.sourceconj,
		conj = args.conj,
		senseid = args.senseid,
		force_cat = force_cat,
	}
end


-- Implementation of miscellaneous templates such as {{abbrev}}, {{back-formation}}, {{clipping}}, {{ellipsis}},
-- {{rebracketing}} and {{reduplication}} that have a single associated term.
function export.misc_variant(frame)
	local iparams = {
		["ignore-params"] = true,
		text = {required = true},
		oftext = true,
		cat = {list = true}, -- allow and compress holes
		conj = true,
	}

	local iargs = process_params(frame.args, iparams)

	local boolean = {type = "boolean"}

	local params = {
		[1] = {required = true, type = "language", default = "und"},
		[2] = true,
		[3] = {alias_of = "alt"},
		[4] = {alias_of = "t"},

		nocap = boolean, -- should be processed in the template itself
		notext = boolean,
		nocat = boolean,
		conj = {set = allowed_conjs},
		sort = true,
	}

	-- |ignore-params= parameter to module invocation specifies
	-- additional parameter names to allow  in template invocation, separated by
	-- commas. They must consist of ASCII letters or numbers or hyphens.
	local ignore_params = iargs["ignore-params"]
	if ignore_params then
		ignore_params = trim(ignore_params)
		if not ignore_params:match("^[%w%-,]+$") then
			error("Invalid characters in |ignore-params=: " .. ignore_params:gsub("[%w%-,]+", ""))
		end
		for param in ignore_params:gmatch("[%w%-]+") do
			if params[param] then
				error("Duplicate param |" .. param
					.. " in |ignore-params=: already specified in params")
			end
			params[param] = true
		end
	end

	local m_param_utils = require(parameter_utilities_module)
	local param_mods = m_param_utils.construct_param_mods {
		{group = {"link", "q", "l", "ref"}},
	}

	local parent_args = frame:getParent().args

	local terms, args = m_param_utils.parse_term_with_inline_modifiers_and_separate_params {
		params = params,
		param_mods = param_mods,
		raw_args = parent_args,
		termarg = 2,
		track_module = "etymology",
		-- Don't set lang here as we want to know whether there was a lang prefix or not.
		sc = "sc",
		parse_lang_prefix = true,
		allow_multiple_lang_prefixes = true,
		make_separate_g_into_list = true,
		splitchar = ",",
		subitem_param_handling = "last",
	}

	return require(etymology_module).format_misc_variant {
		lang = args[1],
		notext = args.notext,
		text = iargs.text,
		oftext = iargs.oftext,
		terms = terms.terms,
		sort_key = args.sort,
		conj = args.conj or iargs.conj or "and",
		nocat = args.nocat,
		cats = iargs.cat,
		force_cat = force_cat,
	}
end


-- Implementation of miscellaneous templates such as {{doublet}} that can take multiple terms. Doesn't handle {{blend}}
-- or {{univerbation}}, which display + signs between elements and use compound_like in [[Module:affix/templates]].
function export.misc_variant_multiple_terms(frame)
	local iparams = {
		text = {required = true},
		oftext = true,
		cat = {list = true}, -- allow and compress holes
		conj = true,
	}

	local iargs = process_params(frame.args, iparams)

	local boolean = {type = "boolean"}

	local params = {
		[1] = {required = true, type = "language", template_default = "und"},
		[2] = {list = true, allow_holes = true},
		nocap = boolean, -- should be processed in the template itself
		notext = boolean,
		nocat = boolean,
		conj = {set = allowed_conjs},
		sort = true,
	}

    local m_param_utils = require(parameter_utilities_module)
	local param_mods = m_param_utils.construct_param_mods {
		-- We want to require an index for all params.
		{default = true, require_index = true},
		{group = {"link", "q", "l", "ref"}},
	}

	local parent_args = frame:getParent().args

	local terms, args = m_param_utils.parse_list_with_inline_modifiers_and_separate_params {
		params = params,
		param_mods = param_mods,
		raw_args = parent_args,
		termarg = 2,
		parse_lang_prefix = true,
		allow_multiple_lang_prefixes = true,
		track_module = "etymology-templates-doublet",
		disallow_custom_separators = true,
		-- For compatibility, we need to not skip completely unspecified items. It is common, for example, to do
		-- {{suffix|lang||foo}} to generate "+ -foo".
		dont_skip_items = true,
		-- Don't set lang here as we want to know whether there was a lang prefix or not.
		sc = "sc.default",
	}

	return require(etymology_module).format_misc_variant {
		lang = args[1],
		notext = args.notext,
		text = iargs.text,
		oftext = iargs.oftext,
		terms = terms,
		sort_key = args.sort,
		conj = args.conj or iargs.conj or "and",
		nocat = args.nocat,
		cats = iargs.cat,
		force_cat = force_cat,
	}
end

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

			["title"] = true,
			["nocap"] = boolean, -- should be processed in the template itself
			["notext"] = boolean,
			["nocat"] = boolean,
			["sort"] = true,
		}
		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)

		return require(etymology_module).format_misc_variant_no_term {
			lang = args[1],
			notext = args.notext,
			title = args.title or frame.args.text,
			nocat = args.nocat,
			cat = frame.args.cat,
			sort_key = args.sort,
			force_cat = force_cat,
		}
	end

	-- This function works similarly to misc_variant_no_term(), but with some automatic linking to the glossary in
	-- `title`.
	function export.onomatopoeia(frame)
		local args = get_args(frame)

		local title = args.title
		if title and (lower(title) == "imitative" or lower(title) == "imitation") then
			title = "[[wikt:Appendix:Glossary#imitative|" .. title .. "]]"
		end

		return require(etymology_module).format_misc_variant_no_term {
			lang = args[1],
			notext = args.notext,
			title = title or frame.args.text,
			nocat = args.nocat,
			cat = frame.args.cat,
			sort_key = args.sort,
			force_cat = force_cat,
		}
	end
end

return export