48,355
edits
No edit summary |
No edit summary |
||
| (34 intermediate revisions by the same user not shown) | |||
| Line 4: | Line 4: | ||
local force_cat = false | local force_cat = false | ||
local function term_error(terminfo) | |||
if terminfo.lang then | |||
function | if terminfo.lang:hasType("family") then | ||
terminfo.term = "-" | |||
end | |||
end | end | ||
return terminfo | return terminfo | ||
| Line 29: | Line 14: | ||
local function | local function create_link(terminfo, template_name) | ||
local link = "" | local link = "" | ||
if terminfo.term ~= "-" then | if terminfo.term ~= "-" then | ||
link = | -- mw.log(terminfo.term) | ||
link = require("Module:links").full_link(terminfo, "term") | |||
if (link ~= "") then link = " " .. link end | |||
end | end | ||
| Line 40: | Line 27: | ||
function export. | function export.process_and_create_link(terminfo, template_name) | ||
terminfo = term_error(terminfo) | |||
return create_link(terminfo, template_name or "derived") | |||
end | |||
function export.get_display_and_cat_name(source, raw) | |||
local display, cat_name | |||
if source:getCode() == "und" then | if source:getCode() == "und" then | ||
display = "undetermined" | |||
cat_name = "other languages" | |||
elseif source:getCode() == "mul" then | elseif source:getCode() == "mul" then | ||
display = raw and "translingual" or "[[w:Translingualism|translingual]]" | |||
cat_name = "Translingual" | |||
elseif source:getCode() == "mul-tax" then | elseif source:getCode() == "mul-tax" then | ||
display = raw and "taxonomic name" or "[[w:Biological nomenclature|taxonomic name]]" | |||
cat_name = "taxonomic names" | |||
else | else | ||
display = raw and source:getCanonicalName() or source:makeWikipediaLink() | |||
cat_name = source:getDisplayForm() | |||
if source: | end | ||
return display, cat_name | |||
end | |||
function export.insert_source_cat_get_display(data) | |||
local categories, lang, source = data.categories, data.lang, data.source | |||
local display, cat_name = export.get_display_and_cat_name(source, data.raw) | |||
if lang and not data.nocat then | |||
-- Add the category, but only if there is a current language | |||
if not categories then | |||
categories = {} | |||
end | |||
local langname = lang:getFullName() | |||
-- If `lang` is an etym-only language, we need to check both it and its parent full language against `source`. | |||
-- Otherwise if e.g. `lang` is Medieval Latin and `source` is Latin, we'll end up wrongly constructing a | |||
-- category 'Latin terms derived from Latin'. | |||
if lang:getCode() == source:getCode() or lang:getFullCode() == source:getCode() then | |||
table.insert(categories, langname .. " terms borrowed back into " .. langname) | |||
else | else | ||
table.insert(categories, langname .. " terms derived from " .. cat_name) | |||
if data.borrowing_type then | |||
table.insert(categories, langname .. " terms " .. data.borrowing_type .. " from " .. cat_name) | |||
end | |||
end | end | ||
end | end | ||
-- | return display, categories | ||
end | |||
function export.format_source(data) | |||
local lang, sort_key = data.lang, data.sort_key | |||
local display, categories = export.insert_source_cat_get_display(data) | |||
if lang and not data.nocat then | |||
-- Format categories, but only if there is a current language; {{cog}} currently gets no categories | |||
categories = require("Module:utilities").format_categories(categories, lang, sort_key, nil, | |||
data.force_cat or force_cat) | |||
else | else | ||
categories = "" | categories = "" | ||
end | end | ||
return "<span class=\"etyl\">" . | return "<span class=\"etyl\">" .. display .. categories .. "</span>" | ||
end | end | ||
-- Internal implementation of {{cognate|...}} template | -- Internal implementation of {{cognate|...}} template | ||
function export.format_cognate( | function export.format_cognate(data) | ||
return export.format_derived | return export.format_derived { | ||
terminfo = data.terminfo, | |||
sort_key = data.sort_key, | |||
template_name = "cognate", | |||
} | |||
end | end | ||
-- Internal implementation of {{derived|...}} template | -- Internal implementation of {{derived|...}} template | ||
function export.format_derived(lang, terminfo, sort_key, nocat, | function export.format_derived(data) | ||
local lang, terminfo, sort_key, nocat, template_name = | |||
data.lang, data.terminfo, data.sort_key, data.nocat, data.template_name | |||
return export.format_source { | |||
lang = lang, | |||
source = terminfo.lang, | |||
sort_key = sort_key, | |||
nocat = nocat, | |||
borrowing_type = data.borrowing_type, | |||
force_cat = data.force_cat, | |||
} .. export.process_and_create_link(terminfo, template_name) | |||
end | end | ||
do | |||
-- | -- Generate the non-ancestor error message. | ||
function | local function showLanguage(lang) | ||
local retval = ("%s (%s)"):format(lang:makeCategoryLink(), lang:getCode()) | |||
if lang:hasType("etymology-only") then | |||
retval = retval .. (" (an etymology-only language whose regular parent is %s)"):format( | |||
showLanguage(lang:getParent())) | |||
end | |||
return retval | |||
end | |||
-- Check that `lang` has `otherlang` (which may be an etymology-only language) as an ancestor. Throw an error if not. | |||
function export.check_ancestor(lang, otherlang) | |||
-- FIXME: I don't know if this function works correctly with etym-only languages in `lang`. I have fixed up | |||
-- the module link code appropriately (June 2024) but the remaining logic is untouched. | |||
return | if lang:hasAncestor(otherlang) or mw.title.getCurrentTitle().nsText == "Template" then | ||
return | |||
end | end | ||
local postscript | local ancestors, postscript = lang:getAncestors() | ||
local | local etymModuleLink = lang:hasType("etymology-only") and "[[Module:etymology languages/data]] or " or "" | ||
local moduleLink = "[[Module:" | local moduleLink = "[[Module:" | ||
.. require("Module:languages").getDataModuleName(lang: | .. require("Module:languages").getDataModuleName(lang:getFullCode()) | ||
.. "]]" | .. "]]" | ||
if not ancestors[1] then | if not ancestors[1] then | ||
| Line 142: | Line 155: | ||
ancestors[2] and "are" or "is", ancestorList) | ancestors[2] and "are" or "is", ancestorList) | ||
end | end | ||
error(("%s is not set as an ancestor of %s in %s. %s") | error(("%s is not set as an ancestor of %s in %s%s. %s") | ||
:format(showLanguage( | :format(showLanguage(otherlang), showLanguage(lang), etymModuleLink, moduleLink, postscript)) | ||
end | end | ||
end | end | ||
-- Internal implementation of {{ | -- Internal implementation of {{inherited|...}} template | ||
function export. | function export.format_inherited(data) | ||
local lang, terminfo, sort_key, nocat = data.lang, data.terminfo, data.sort_key, data.nocat | |||
local source = terminfo.lang | local source = terminfo.lang | ||
local categories = {} | local categories = {} | ||
if not nocat then | |||
if | table.insert(categories, lang:getFullName() .. " terms inherited from " .. source:getCanonicalName()) | ||
table.insert(categories, lang: | |||
end | end | ||
local link = export.process_and_create_link(terminfo, "inherited") | |||
export.check_ancestor(lang, source) | |||
return export.format_source { | |||
lang = lang, | |||
source = source, | |||
sort_key = sort_key, | |||
categories = categories, | |||
nocat = nocat, | |||
force_cat = data.force_cat, | |||
} .. link | |||
end | end | ||
function export.insert_borrowed_cat(categories, lang, source) | |||
function export. | local category | ||
local | -- Do the same check as in insert_source_cat_get_display() (inverted). | ||
if not (lang:getCode() == source:getCode() or lang:getFullCode() == source:getCode()) then | |||
if not | -- If both are the same, we want e.g. [[:Category:English terms borrowed back into English]] not | ||
-- [[:Category:English terms borrowed from English]]; the former is inserted automatically by format_source(). | |||
-- The second parameter here doesn't matter as it only affects `display`, which we don't use. | |||
local display, cat_name = export.get_display_and_cat_name(source, "raw") | |||
category = " terms borrowed from " .. cat_name | |||
end | end | ||
if category then | |||
table.insert(categories, lang:getFullName() .. category) | |||
if | |||
end | end | ||
end | end | ||
-- Internal implementation of {{ | -- Internal implementation of {{borrowed|...}} template. | ||
function export. | function export.format_borrowed(data) | ||
local | local lang, terminfo, sort_key, nocat = data.lang, data.terminfo, data.sort_key, data.nocat | ||
local source = terminfo.lang | |||
local categories = {} | |||
if not nocat then | |||
export.insert_borrowed_cat(categories, lang, source) | |||
if not | |||
end | end | ||
return | return export.format_source { | ||
lang = lang, | |||
source = source, | |||
sort_key = sort_key, | |||
categories = categories, | |||
nocat = nocat, | |||
force_cat = data.force_cat, | |||
} .. export.process_and_create_link(terminfo, "borrowed") | |||
end | end | ||
return export | return export | ||