Module:Etymology/templates: Difference between revisions
Jump to navigation
Jump to search
(Inception.) |
No edit summary |
||
Line 2: | Line 2: | ||
local m_languages = require("Module:languages") | local m_languages = require("Module:languages") | ||
local m_internal = require("Module:etymology/templates/internal") | |||
local m_params = require("Module:parameters") | |||
-- For testing | |||
local | local force_cat = false | ||
Line 39: | Line 16: | ||
} | } | ||
local args = | local args = m_params.process(frame:getParent().args, params, nil, "etymology/templates", "etyl") | ||
local source = fetch_source(args[1]) | local source = m_internal.fetch_source(args[1]) | ||
local lang = args[2] | local lang = args[2] | ||
local sort_key = args["sort"] | local sort_key = args["sort"] | ||
Line 52: | Line 29: | ||
end | end | ||
if lang then | if lang then | ||
lang = fetch_lang(lang, 2) | lang = m_internal.fetch_lang(lang, 2) | ||
end | end | ||
if lang and (lang:getCode() == "la" or lang:getCode() == "nl") then | if lang and (lang:getCode() == "la" or lang:getCode() == "nl") then | ||
Line 63: | Line 40: | ||
-- 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 bortype = frame.args.bortype | |||
function export. | |||
local | |||
local args = frame:getParent().args | local args = frame:getParent().args | ||
if args.gloss then | if args.gloss then | ||
require("Module:debug").track(" | require("Module:debug").track("borrowing/" .. bortype .. "/gloss param") | ||
end | end | ||
-- More informative error message for {{calque}}, which used to support other params. | |||
if bortype == "calque" and (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 455: | Line 65: | ||
end | end | ||
local args, lang, term, sources = m_internal.parse_2_lang_args(frame, "has text") | |||
local m_etymology_specialized = require("Module:etymology/specialized") | |||
local | if sources then | ||
return m_etymology_specialized.specialized_multi_borrowing(bortype, lang, term.sc, sources, term, | |||
args.sort, args.nocap, args.notext, args.nocat, args.conj, args.senseid) | |||
else | |||
return m_etymology_specialized.specialized_borrowing(bortype, lang, term, args.sort, | |||
args.nocap, args.notext, args.nocat, args.senseid) | |||
local | |||
end | end | ||
end | 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) | function export.misc_variant(frame) | ||
local params = { | local params = { | ||
Line 770: | Line 104: | ||
} | } | ||
local args = | -- |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 = frame.args["ignore-params"] | |||
if ignore_params then | |||
ignore_params = mw.text.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] = {} | |||
end | |||
end | |||
local args = m_params.process(frame:getParent().args, params, nil, "etymology/templates", "misc_variant") | |||
local lang = fetch_lang(args[1], 1) | local lang = m_internal.fetch_lang(args[1], 1) | ||
local sc = fetch_script(args["sc"]) | local sc = m_internal.fetch_script(args["sc"], "sc") | ||
local parts = {} | local parts = {} | ||
Line 802: | Line 154: | ||
true)) | true)) | ||
end | 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 | if not args["nocat"] and frame.args["cat"] then | ||
local | local cat_number | ||
while true do | |||
table.insert(parts, require("Module:utilities").format_categories(categories, lang, args["sort"])) | 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"], nil, force_cat)) | |||
end | end | ||
Line 811: | Line 178: | ||
end | end | ||
function export. | |||
-- Implementation of miscellaneous templates such as {{unknown}} that have no | |||
-- associated terms. | |||
function export.misc_variant_no_term(frame) | |||
local params = { | local params = { | ||
[1] = {required = true, default = "und"}, | [1] = {required = true, default = "und"}, | ||
Line 826: | Line 196: | ||
end | end | ||
local args = | local args = m_params.process(frame:getParent().args, params, nil, "etymology/templates", "misc_variant_no_term") | ||
local lang = fetch_lang(args[1], 1) | local lang = m_internal.fetch_lang(args[1], 1) | ||
local parts = {} | local parts = {} | ||
Line 837: | Line 207: | ||
local categories = {} | local categories = {} | ||
table.insert(categories, lang:getCanonicalName() .. " " .. frame.args["cat"]) | table.insert(categories, lang:getCanonicalName() .. " " .. frame.args["cat"]) | ||
table.insert(parts, require("Module:utilities").format_categories(categories, lang, args["sort"])) | table.insert(parts, require("Module:utilities").format_categories(categories, lang, args["sort"], nil, force_cat)) | ||
end | end | ||
Revision as of 23:55, 15 July 2023
Documentation for this module may be created at Module:Etymology/templates/documentation
local export = {}
local m_languages = require("Module:languages")
local m_internal = require("Module:etymology/templates/internal")
local m_params = require("Module:parameters")
-- For testing
local force_cat = false
function export.etyl(frame)
local params = {
[1] = {required = true, default = "und"},
[2] = {},
["sort"] = {},
}
local args = m_params.process(frame:getParent().args, params, nil, "etymology/templates", "etyl")
local source = m_internal.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 = m_internal.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
-- 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 bortype = frame.args.bortype
local args = frame:getParent().args
if args.gloss then
require("Module:debug").track("borrowing/" .. bortype .. "/gloss param")
end
-- More informative error message for {{calque}}, which used to support other params.
if bortype == "calque" and (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
local args, lang, term, sources = m_internal.parse_2_lang_args(frame, "has text")
local m_etymology_specialized = require("Module:etymology/specialized")
if sources then
return m_etymology_specialized.specialized_multi_borrowing(bortype, lang, term.sc, sources, term,
args.sort, args.nocap, args.notext, args.nocat, args.conj, args.senseid)
else
return m_etymology_specialized.specialized_borrowing(bortype, lang, term, args.sort,
args.nocap, args.notext, args.nocat, args.senseid)
end
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 = {
[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"] = {},
}
-- |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 = frame.args["ignore-params"]
if ignore_params then
ignore_params = mw.text.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] = {}
end
end
local args = m_params.process(frame:getParent().args, params, nil, "etymology/templates", "misc_variant")
local lang = m_internal.fetch_lang(args[1], 1)
local sc = m_internal.fetch_script(args["sc"], "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
-- 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"], nil, force_cat))
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 = m_params.process(frame:getParent().args, params, nil, "etymology/templates", "misc_variant_no_term")
local lang = m_internal.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"], nil, force_cat))
end
return table.concat(parts)
end
return export