La documentation pour ce module peut être créée à Module:étymologie/Documentation
-- <nowiki>
local m_bases = require("Module:bases")
local m_params = require("Module:paramètres")
local m_langues = require("Module:langues")
local m_table = require("Module:table")
local m_Unicode_data = require("Module:données Unicode")
local checkLang = function(langCode, allowSpecial)
return m_langues.getName(langCode, allowSpecial) ~= nil
end
local p = {}
--- Function for templates {{sigle}}, {{abréviation}}, {{acronyme}}, etc.
--- frame.args (string) : Etymological process’ name (sigle, verlan, etc.).
--- frame.args (string, optional) : Plural form of previous parameter.
--- If not present, an "s" will be appended to the first parameter.
--- frame.args (string, optional) : Source word.
--- frame.args (string, optional) : Second source word.
--- frame.args (string, optional) : Source word transcription.
--- frame.args (string, optional) : Second source word transcription.
--- frame.args (string, optional) : Source word sense.
--- frame.args (string, optional) : Second source word sense.
--- frame.args (string, optional) : Source word link nature anchor.
--- frame.args (string, optional) : Second source word link nature anchor.
--- frame.args (string, optional) : Text to show in place of source word.
--- frame.args (string, optional) : Text to show in place of second source word.
--- frame.args (string, required if nocat is false) : An adjective to add between words “terme” and “de”.
--- frame.args (string, required if nocat is false) : Language code of target word.
--- frame.args (string, optional) : Language code of source word(s).
--- If not present, lang parameter will be used instead.
--- frame.args (string, optional) : If true, the first word’s initial letter will be in upper case.
--- frame.args (booléen, optional) : If true, no link will be created to the source word(s).
--- frame.args (booléen, optional) : If true, no category will be inserted.
--- frame.args (string, optional) : Sort key for the categories.
--- frame.args (string, optional) : First category name.
--- frame.args (string, optional) : Second category name.
--- @return string Template’s text and relevant categories.
function p.templateEtymologyConstruction(frame)
local params = {
= { required = true },
= {},
= {},
= {},
= {},
= { alias_of = "tr" },
= {},
= { alias_of = "tr2" },
= {},
= {},
= {},
= {},
= {},
= {},
= {},
= { checker = checkLang },
= { alias_of = "lang" },
= { checker = checkLang },
= { type = m_params.BOOLEAN, default = false },
= { type = m_params.BOOLEAN, default = false },
= { type = m_params.BOOLEAN, default = false },
= { default = mw.title.getCurrentTitle().text },
= {},
= {},
}
local args = m_params.process(frame.args, params)
local term = args
local termPlural = args or (term .. "s")
local caps = args
local from = args
local from2 = args
local transcription = args
local transcription2 = args
local meaning = args
local meaning2 = args
local code = args
local code2 = args
local text = args or from
local text2 = args or from2
local adj = args
local noLink = args
local noCategorization = args
local lang = args
local linkLang = args or (lang or "fr")
local sortingKey = args
local category = args
local category2 = args
local res = mw.ustring.format("]", term, caps and m_bases.ucfirst(term) or term)
if code then
linkLang = linkLang .. "-" .. code
end
-- TODO call {{lien}} instead of reinventing it
local format = function(baseWord, replacementText, baseString, additionalText, transcription, meaning)
if baseWord then
local word
if noLink then
word = baseWord
else
word = mw.ustring.format("]", baseWord, linkLang, replacementText)
end
baseString = baseString .. mw.ustring.format(additionalText .. " de ''%s''", word)
end
if transcription then
if baseString == "" then
baseString = mw.ustring.format("''%s''", transcription)
else
baseString = mw.ustring.format("%s, ''%s''", baseString, transcription)
end
end
if meaning then
if baseString == "" then
baseString = mw.ustring.format("(« %s »)", meaning)
else
baseString = mw.ustring.format("%s (« %s »)", baseString, meaning)
end
end
return baseString
end
local getCategory = function(cat_name)
return m_bases.ucfirst(cat_name) .. " " ..
(lang and "en " .. m_langues.getName(lang) or "sans langue précisée")
end
res = format(from, text, res, adj and (" " .. adj) or "", transcription, meaning)
res = format(from2, text2, res, " et", transcription2, meaning2)
if noCategorization then
return res
else
local categoryName
if category then
categoryName = category
else
if lang == nil then
error("Code de langue manquant")
end
categoryName = getCategory(termPlural)
end
res = res .. m_bases.fait_categorie_contenu(categoryName, sortingKey)
if category2 then
if lang == nil then
error("Code de langue manquant")
end
categoryName = getCategory(category2)
res = res .. m_bases.fait_categorie_contenu(categoryName, sortingKey)
end
return res
end
end
-- Various latin codes accepted by {{étyl}}.
local LATIN_CODES = {
"bas latin",
"gallo-roman",
"latin archaïque",
"latin classique",
"latin contemporain",
"latin ecclésiastique",
"latin humaniste",
"latin impérial",
"latin médiéval",
"latin populaire",
"latin tardif",
"latin vulgaire",
"néolatin",
}
local function getLanguageCode(langCode)
return m_table.contains(LATIN_CODES, langCode) and "la" or langCode
end
local function checkLangExtended(langCode)
return checkLang(getLanguageCode(langCode), true)
end
local function getLanguageName(langCode)
return m_table.contains(LATIN_CODES, langCode) and langCode or m_langues.getName(langCode, true)
end
local function italicIfLatinScript(text)
return m_Unicode_data.shouldItalicize(text) and mw.ustring.format("''%s''", text) or text
end
--- Function for templates {{étyl}} and {{étylp}}. If at least one of the two languages is unknown or missing,
--- an error message is displayed in place of the text and the category
--- ] is inserted.
--- @param frame frame
--- Parameters:
--- frame.args (string): Source language code.
--- frame.args (string): Target language code.
--- frame.args (string, optional): Source word.
--- frame.args (string, optional): Transcription of source word.
--- frame.args (string, optional): Meaning of source word.
--- frame.args (string, optional): Text to show instead of the source word.
--- frame.args (string, optional): Section code in source word’s page.
--- frame.args (int, optional): Section number in source word’s page.
--- frame.args (boolean, optional): If true and no errors are found, no categories will be returned.
--- frame.args (boolean, optinal): If true, inserts a link to the source language.
--- @return string Template’s text and relevant categories.
function p.templateEtyl(frame)
local args = m_params.process(frame:getParent().args, {
= {},
= {},
= {},
= { alias_of = "mot" },
= {},
= { alias_of = "tr" },
= { alias_of = "tr" },
= {},
= { alias_of = "sens" },
= {},
= {},
= { type = m_params.INT, checker = function(n)
return n > 0
end },
= { type = m_params.BOOLEAN, default = false },
= { type = m_params.BOOLEAN, default = false },
})
local forProperNouns = frame.args ~= nil
local originLang = args
local destLang = args
local word = args
local transcription = args
local meaning = args
local alternativeText = args
local anchorSection = args
local anchorNum = args
local noCat = args
local linkToLang = args
if not checkLangExtended(originLang) or (not noCat and not checkLangExtended(destLang)) then
return '<span style="color:red">Erreur modèle étyl : langue inconnue ou absente</span>' ..
m_bases.fait_categorie_contenu("Wiktionnaire:Modèle étyl sans langue précisée")
end
local categories = ""
local content = ""
local anchor = ""
local originLangName = getLanguageName(originLang)
local destLangName = getLanguageName(destLang)
local displayedLang = linkToLang and m_bases.lien_modele(originLangName, "fr") or originLangName
content = ""
if word then
if anchorSection then
anchor = anchorSection
end
if anchorNum then
anchor = anchor .. "-" .. tostring(anchorNum)
end
content = italicIfLatinScript(m_bases.lien_modele(word, getLanguageCode(originLang), anchor, alternativeText, true))
end
if transcription then
if content == "" then
content = mw.ustring.format("''%s''", transcription)
else
content = mw.ustring.format("%s, ''%s''", content, transcription)
end
end
if meaning then
if content == "" then
content = mw.ustring.format("(« %s »)", meaning)
else
content = mw.ustring.format("%s (« %s »)", content, meaning)
end
end
if content == "" then
content = displayedLang
else
content = mw.ustring.format("%s %s", displayedLang, content)
end
if not noCat then
local catNamePrefix = forProperNouns and "Noms propres" or "Mots"
if originLang == destLang then
categories = categories .. m_bases.fait_categorie_contenu("Appels de modèles incorrects:étyl")
elseif originLang == "onom" then
categories = categories .. m_bases.fait_categorie_contenu(mw.ustring.format(
"%s en %s issus d’une onomatopée",
catNamePrefix,
destLangName
))
else
categories = categories .. m_bases.fait_categorie_contenu(mw.ustring.format(
"%s en %s issus d’un mot en %s",
catNamePrefix,
destLangName,
originLangName
))
end
end
return content .. categories
end
--- Function for template {{étylcat}}. If at least one of the two languages is unknown or missing,
--- or the category’s name is not valid, an error message is displayed in place of the text and the category
--- ] is inserted.
--- @param frame frame
--- Parameters:
--- frame.args (string): Current language code.
--- frame.args (string): Etymological language code.
--- frame.args (string): The sort key for the category ].
--- frame.args (string): The sort key for the category ].
--- frame.args (string): The name of a template that shows an index. Defaults to CatégorieTDM.
--- Pass the value "-" to not show any template.
--- frame.args (string): The name of a second template that shows a index.
--- @return string Template’s text and relevant categories.
function p.templateEtylcat(frame)
local args = m_params.process(frame:getParent().args, {
= {},
= {},
= {},
= {},
= {},
= {},
})
local destLang = args
local originLang = args
local destLangName = getLanguageName(destLang)
local originLangName = getLanguageName(originLang)
local sortKey1 = args or destLangName
local sortKey2 = args or originLangName
local cat1 = args
local cat2 = args
if not checkLangExtended(originLang) or not checkLangExtended(destLang) then
return '<span style="color:red">Erreur modèle étylcat : langue inconnue ou absente</span>' ..
m_bases.fait_categorie_contenu("Appels de modèles incorrects:étylcat")
end
local expectedPageTitle = mw.ustring.format(
"Catégorie:Mots en %s issus d’%s",
destLangName,
originLang == "onom" and "une onomatopée" or ("un mot en " .. originLangName),
originLangName
)
if mw.title.getCurrentTitle().fullText ~= expectedPageTitle then
return '<span style="color:red">Erreur modèle étylcat : pas dans une catégorie compatible</span>' ..
m_bases.fait_categorie_contenu("Appels de modèles incorrects:étylcat")
end
local tablesOfContent = {}
if cat1 ~= "-" then
table.insert(tablesOfContent, frame:expandTemplate { title = cat1 or "CatégorieTDM" })
end
if cat2 then
table.insert(tablesOfContent, frame:expandTemplate { title = cat2 })
end
local result = mw.ustring.format(
[=[
Cette catégorie liste les termes en ] ayant pour origine étymologique %s.
Lire aussi ].
%s
]
]
]=],
destLangName,
originLang == "onom" and "une onomatopée" or mw.ustring.format("un mot en ]", originLangName),
table.concat(tablesOfContent, "\n"),
originLang == "onom" and "une onomatopée" or ("un mot en " .. originLangName),
sortKey1,
destLangName,
sortKey2
)
return mw.text.trim(result)
end
--- Function for template {{lien-ancre-étym}}.
--- frame.args (string) : Language code.
--- frame.args (string) : Section ID.
--- frame.args (int, optional) : Section’s number.
--- frame.args (int, optional) : Whether the target section is a locution. Changes the displayed text.
--- @return string Template’s text.
function p.templateLienAncreEtym(frame)
local wordTypes = mw.loadData("Module:types de mots/data")
local args = m_params.process(frame:getParent().args, {
= { required = true, checker = checkLang },
= { required = true, checker = function(v)
return wordTypes ~= nil or wordTypes ~= nil
end },
= { type = m_params.INT, checker = function(v)
return v > 0
end },
= { type = m_params.BOOLEAN, default = false },
})
local langCode = args
local wordTypeData = wordTypes] or args]
local locution = args and wordTypeData ~= nil
local text = wordTypeData
local number = args
local anchor = langCode .. "-" .. wordTypeData .. (number ~= nil and ("-" .. tostring(number)) or "")
text = m_bases.ucfirst(text)
if number ~= nil then
text = text .. " " .. tostring(number)
end
return mw.ustring.format("''(])''", anchor, text)
end
return p
-- </nowiki>