Module:alternative forms: difference between revisions
Jump to navigation
Jump to search
Content deleted Content added
export the getLabel function, for use in Module:grc-decl |
fix support for references in {{alt}} items |
||
(48 intermediate revisions by 6 users not shown) | |||
Line 1: | Line 1: | ||
local export = {} |
local export = {} |
||
local m_link = require('Module:links') |
|||
local m_languages = require("Module:languages") |
|||
local m_debug = require("Module:debug") |
|||
local labels_module = "Module:labels" |
|||
-- See if the language's dialectal data module has a label corresponding to the dialect argument. |
|||
local links_module = "Module:links" |
|||
function export.getLabel(dialect, dialect_data) |
|||
local parameter_utilities_module = "Module:parameter utilities" |
|||
local data = dialect_data[dialect] or ( dialect_data.labels and dialect_data.labels[dialect] ) |
|||
local parameters_module = "Module:parameters" |
|||
local alias_of = ( dialect_data.aliases and dialect_data.aliases[dialect] ) |
|||
local parse_utilities_module = "Module:parse utilities" |
|||
if not data then |
|||
local pron_qualifier_module = "Module:pron qualifier" |
|||
if alias_of then |
|||
data = dialect_data[alias_of] or ( dialect_data.labels and dialect_data.labels[alias_of] ) |
|||
local function track(page) |
|||
require("Module:debug/track")("alter/" .. page) |
|||
end |
|||
local param_mods = { |
|||
alt = {}, |
|||
t = { |
|||
-- [[Module:links]] expects the gloss in "gloss". |
|||
item_dest = "gloss", |
|||
}, |
|||
gloss = { |
|||
alias_of = "t", |
|||
}, |
|||
tr = {}, |
|||
ts = {}, |
|||
g = { |
|||
-- [[Module:links]] expects the genders in "genders". |
|||
item_dest = "genders", |
|||
sublist = true, |
|||
}, |
|||
pos = {}, |
|||
lit = {}, |
|||
id = {}, |
|||
sc = { |
|||
separate_no_index = true, |
|||
type = "script", |
|||
}, |
|||
} |
|||
--[==[ |
|||
Main function for displaying alternative forms. Extracted out from the template-callable function so this can be |
|||
called by other modules (in particular, [[Module:descendants tree]]). `show_labels_after_terms` no longer has any |
|||
meaning. `allow_self_link` causes terms the same as the pagename to be shown normally; otherwise they are displayed |
|||
unlinked. |
|||
]==] |
|||
function export.display_alternative_forms(parent_args, pagename, show_labels_after_terms, allow_self_link) |
|||
local params = { |
|||
[1] = {required = true, type = "language", etym_lang = true, default = "en"}, |
|||
[2] = {list = true, allow_holes = true}, |
|||
} |
|||
local m_param_utils = require(parameter_utilities_module) |
|||
m_param_utils.augment_param_mods_with_pron_qualifiers(param_mods, { |
|||
{param = "q", separate_no_index = false}, |
|||
{param = "l", separate_no_index = false, require_index = true}, |
|||
"ref", |
|||
}) |
|||
m_param_utils.augment_params_with_modifiers(params, param_mods) |
|||
local args = require("Module:parameters").process(parent_args, params) |
|||
local lang = args[1] |
|||
local items = m_param_utils.process_list_arguments { |
|||
args = args, |
|||
param_mods = param_mods, |
|||
termarg = 2, |
|||
parse_lang_prefix = true, |
|||
track_module = "alter", |
|||
lang = lang, |
|||
sc = args.sc.default, |
|||
stop_when = function(data) |
|||
return not data.any_param_at_index |
|||
end, |
|||
} |
|||
if not items[1] then |
|||
error("No items found!") |
|||
end |
|||
local raw_labels = {} |
|||
-- Extract the labels and make sure none are blank or omitted. |
|||
local last_item_index = items[#items].orig_index |
|||
if last_item_index < args[2].maxindex then |
|||
for i = last_item_index + 2, args[2].maxindex do |
|||
if not args[2][i] then |
|||
-- Indices in i start at 1 but parameters start at 2 to add 1 to shown index. |
|||
error("Missing/blank item not allowed in [[Template:alt]] labels, but saw such an item in parameter " |
|||
.. (i + 1)) |
|||
end |
|||
table.insert(raw_labels, args[2][i]) |
|||
end |
end |
||
end |
end |
||
if data then |
|||
-- Make sure there aren't property parameters after the last item (i.e. corresponding to labels). |
|||
local target = data.link |
|||
for k, v in pairs(args) do |
|||
local display = data.display or dialect |
|||
-- Look for named list parameters. We check: |
|||
dialect = target and ('[[w:'.. target ..'|'..display..']]') or display |
|||
-- (1) key is a string (excludes the term param, which is a number); |
|||
-- (2) value is a table, i.e. a list; |
|||
-- (3) v.maxindex is set (i.e. allow_holes was used); |
|||
-- (4) v.maxindex is past the index of the last term. |
|||
if type(k) == "string" and type(v) == "table" and v.maxindex and v.maxindex > last_item_index then |
|||
local set_values = {} |
|||
for i = last_item_index + 1, v.maxindex do |
|||
if v[i] then |
|||
table.insert(set_values, i) |
|||
end |
|||
end |
|||
error(("Extraneous values for %s= (set at position%s %s)"):format(k, #set_values > 1 and "s" or "", |
|||
table.concat(set_values, ","))) |
|||
end |
|||
end |
end |
||
return dialect |
|||
end |
|||
if not allow_self_link then |
|||
local function make_dialects(raw, lang) |
|||
-- If the to-be-linked term is the same as the pagename, display it unlinked. |
|||
local dialect_page = 'Module:'.. lang:getCode() ..':Dialects' |
|||
for _, item in ipairs(items) do |
|||
local dialect_info = mw.title.new(dialect_page).exists and require(dialect_page) or false |
|||
if not item.term and (lang:makeEntryName(item.term)) == pagename then |
|||
track("term is pagename") |
|||
local dialects = {} |
|||
item.alt = item.alt or item.term |
|||
item.term = nil |
|||
for _, dialect in pairs(raw) do |
|||
end |
|||
table.insert(dialects, dialect_info and export.getLabel(dialect, dialect_info) or dialect) |
|||
end |
|||
end |
end |
||
return dialects |
|||
end |
|||
local labels |
|||
local function track(args, arg, number) |
|||
if #raw_labels > 0 then |
|||
if args and args[arg] and args[arg][number] then |
|||
labels = require(labels_module).process_raw_labels { labels = raw_labels, lang = lang, nocat = true } |
|||
m_debug.track("alter/" .. arg) |
|||
end |
end |
||
end |
|||
local parts = {} |
|||
function export.create(frame) |
|||
local function ins(part) |
|||
NAMESPACE = mw.title.getCurrentTitle().nsText |
|||
table.insert(parts, part) |
|||
end |
|||
params = { |
|||
[1] = { required = true }, |
|||
[2] = { required = true, list = true, allow_holes = true }, |
|||
["alt"] = { list = true, allow_holes = true }, |
|||
["id"] = { list = true, allow_holes = true }, |
|||
["tr"] = { list = true, allow_holes = true }, |
|||
["sc"] = {}, |
|||
["g"] = { list = true, allow_holes = true }, |
|||
} |
|||
local args = require("Module:parameters").process(frame:getParent().args, params) |
|||
local lang = ( args[1] and m_languages.getByCode(args[1]) ) or ( NAMESPACE == "Template" and m_languages.getByCode("und") ) or m_languages.err(args[1], 1) |
|||
local sc = require("Module:scripts").getByCode(args["sc"]) |
|||
track(args, "dial") -- [[Special:WhatLinksHere/Template:tracking/alter/dial]] |
|||
local rawDialects = {} |
|||
local links = {} |
|||
local maxindex = math.max(args[2].maxindex, args.alt.maxindex, args.id.maxindex, args.tr.maxindex) |
|||
-- Construct the final output. |
|||
for i = 1, maxindex do |
|||
-- If the previous parameter was empty and we're not on the first parameter, |
|||
-- First the items, including separators, left and right regular qualifiers and left and right per-item labels. |
|||
-- this parameter and any others contain dialect or other labels. |
|||
for _, item in ipairs(items) do |
|||
if i > 1 and not args[2][i - 1] then |
|||
ins(item.separator) |
|||
rawDialects = {unpack(args[2], i, maxindex)} |
|||
local text = require(links_module).full_link(item, nil, allow_self_link) |
|||
break |
|||
if item.q and item.q[1] or item.qq and item.qq[1] or item.l and item.l[1] or item.ll and item.ll[1] |
|||
-- If there's a term, link it and add it to the list. |
|||
or item.refs and item.refs[1] then |
|||
text = require(pron_qualifier_module).format_qualifiers { |
|||
track(args, "alt", i) -- [[Special:WhatLinksHere/Template:tracking/alter/alt]] |
|||
lang = item.lang, |
|||
track(args, "id", i) -- [[Special:WhatLinksHere/Template:tracking/alter/id]] |
|||
text = text, |
|||
track(args, "tr", i) -- [[Special:WhatLinksHere/Template:tracking/alter/tr]] |
|||
q = item.q, |
|||
track(args, "sc", i) -- [[Special:WhatLinksHere/Template:tracking/alter/sc]] |
|||
qq = item.qq, |
|||
track(args, "g", i) -- [[Special:WhatLinksHere/Template:tracking/alter/g]] |
|||
l = item.l, |
|||
ll = item.ll, |
|||
refs = item.refs, |
|||
term = args[2][i], |
|||
alt = args.alt[i], |
|||
id = args.id[i], |
|||
tr = args.tr[i], |
|||
genders = args.g[i] |
|||
} |
} |
||
table.insert(links, term) |
|||
end |
end |
||
ins(text) |
|||
end |
end |
||
-- If there are labels, construct them now and append to final output. |
|||
local dialects = make_dialects(rawDialects, lang) |
|||
if labels then |
|||
local output = { table.concat(links, ', ') } |
|||
if #dialects > 0 then |
|||
if lang:hasTranslit() then |
if lang:hasTranslit() then |
||
ins(" — " .. require(labels_module).format_processed_labels { |
|||
dialect_label = " – ''" .. table.concat(dialects, ", ") .. "''" |
|||
labels = labels, lang = lang |
|||
}) |
|||
else |
else |
||
ins(" " .. require(labels_module).format_processed_labels { |
|||
dialect_label = " (''" .. table.concat(dialects, ", ") .. "'')" |
|||
labels = labels, lang = lang, open = "(", close = ")" |
|||
}) |
|||
end |
end |
||
-- Fixes the problem of '' being added to '' at the end of last dialect parameter |
|||
dialect_label = mw.ustring.gsub(dialect_label, "''''", "") |
|||
table.insert(output, dialect_label) |
|||
end |
end |
||
return table.concat( |
return table.concat(parts) |
||
end |
end |
||
--[==[ |
|||
function export.categorize(frame) |
|||
Template-callable function for displaying alternative forms. |
|||
local content = {} |
|||
]==] |
|||
function export.create(frame) |
|||
local parent_args = frame:getParent().args |
|||
local title = mw.title.getCurrentTitle() |
local title = mw.title.getCurrentTitle() |
||
local |
local PAGENAME = title.text |
||
return export.display_alternative_forms(parent_args, title) |
|||
local namespace = title.nsText |
|||
local subpagename = title.subpageText |
|||
-- subpagename ~= titletext if it is a documentation page |
|||
if namespace == "Module" and subpagename == titletext then |
|||
local langCode = mw.ustring.match(titletext, "^([^:]+):") |
|||
local lang = m_languages.getByCode(langCode) or error('"' .. langCode .. '" is not a valid language code.') |
|||
content.canonicalName = lang:getCanonicalName() |
|||
local categories = |
|||
[=[ |
|||
[[Category:<canonicalName> modules|dialects]] |
|||
[[Category:Dialectal data modules|<canonicalName>]] |
|||
]=] |
|||
local function addContent(item) |
|||
return content[item] |
|||
end |
|||
categories = mw.ustring.gsub(categories, "<([^>]+)>", addContent) |
|||
return categories |
|||
end |
|||
end |
end |
||
Latest revision as of 19:16, 16 June 2024
- The following documentation is located at Module:alternative forms/documentation. [edit]
- Useful links: subpage list • links • transclusions • testcases • sandbox (diff)
This module is called by {{alter}}
, the template that is used in Alternative forms sections to link the alternative forms of a term and to show which dialect or spelling system they belong to.
The dialect labels used after an empty parameter are now the same as those used by {{lb}}
, and can be found in the labels data modules, e.g. Module:labels/data/lang/en for English and Module:labels/data/lang/grc for Ancient Greek.
Testcases
- ὑμεῖς (humeîs)
- (using aliases of labels)
- werweißen (rare, outside of Switzerland)
- items with commas
- same old, same old; same-old, same-old; same-old same-old; same old-same old; same ol' same ol'; same ol', same ol'; same-ol' same-ol'; same ol'-same ol'
- term param missing
- testity, test
- [script needed] (dʾl /dār/)
See also
- Module:descendants tree, which retrieves alternative forms for
{{desc}}
and{{desctree}}
local export = {}
local labels_module = "Module:labels"
local links_module = "Module:links"
local parameter_utilities_module = "Module:parameter utilities"
local parameters_module = "Module:parameters"
local parse_utilities_module = "Module:parse utilities"
local pron_qualifier_module = "Module:pron qualifier"
local function track(page)
require("Module:debug/track")("alter/" .. page)
end
local param_mods = {
alt = {},
t = {
-- [[Module:links]] expects the gloss in "gloss".
item_dest = "gloss",
},
gloss = {
alias_of = "t",
},
tr = {},
ts = {},
g = {
-- [[Module:links]] expects the genders in "genders".
item_dest = "genders",
sublist = true,
},
pos = {},
lit = {},
id = {},
sc = {
separate_no_index = true,
type = "script",
},
}
--[==[
Main function for displaying alternative forms. Extracted out from the template-callable function so this can be
called by other modules (in particular, [[Module:descendants tree]]). `show_labels_after_terms` no longer has any
meaning. `allow_self_link` causes terms the same as the pagename to be shown normally; otherwise they are displayed
unlinked.
]==]
function export.display_alternative_forms(parent_args, pagename, show_labels_after_terms, allow_self_link)
local params = {
[1] = {required = true, type = "language", etym_lang = true, default = "en"},
[2] = {list = true, allow_holes = true},
}
local m_param_utils = require(parameter_utilities_module)
m_param_utils.augment_param_mods_with_pron_qualifiers(param_mods, {
{param = "q", separate_no_index = false},
{param = "l", separate_no_index = false, require_index = true},
"ref",
})
m_param_utils.augment_params_with_modifiers(params, param_mods)
local args = require("Module:parameters").process(parent_args, params)
local lang = args[1]
local items = m_param_utils.process_list_arguments {
args = args,
param_mods = param_mods,
termarg = 2,
parse_lang_prefix = true,
track_module = "alter",
lang = lang,
sc = args.sc.default,
stop_when = function(data)
return not data.any_param_at_index
end,
}
if not items[1] then
error("No items found!")
end
local raw_labels = {}
-- Extract the labels and make sure none are blank or omitted.
local last_item_index = items[#items].orig_index
if last_item_index < args[2].maxindex then
for i = last_item_index + 2, args[2].maxindex do
if not args[2][i] then
-- Indices in i start at 1 but parameters start at 2 to add 1 to shown index.
error("Missing/blank item not allowed in [[Template:alt]] labels, but saw such an item in parameter "
.. (i + 1))
end
table.insert(raw_labels, args[2][i])
end
end
-- Make sure there aren't property parameters after the last item (i.e. corresponding to labels).
for k, v in pairs(args) do
-- Look for named list parameters. We check:
-- (1) key is a string (excludes the term param, which is a number);
-- (2) value is a table, i.e. a list;
-- (3) v.maxindex is set (i.e. allow_holes was used);
-- (4) v.maxindex is past the index of the last term.
if type(k) == "string" and type(v) == "table" and v.maxindex and v.maxindex > last_item_index then
local set_values = {}
for i = last_item_index + 1, v.maxindex do
if v[i] then
table.insert(set_values, i)
end
end
error(("Extraneous values for %s= (set at position%s %s)"):format(k, #set_values > 1 and "s" or "",
table.concat(set_values, ",")))
end
end
if not allow_self_link then
-- If the to-be-linked term is the same as the pagename, display it unlinked.
for _, item in ipairs(items) do
if not item.term and (lang:makeEntryName(item.term)) == pagename then
track("term is pagename")
item.alt = item.alt or item.term
item.term = nil
end
end
end
local labels
if #raw_labels > 0 then
labels = require(labels_module).process_raw_labels { labels = raw_labels, lang = lang, nocat = true }
end
local parts = {}
local function ins(part)
table.insert(parts, part)
end
-- Construct the final output.
-- First the items, including separators, left and right regular qualifiers and left and right per-item labels.
for _, item in ipairs(items) do
ins(item.separator)
local text = require(links_module).full_link(item, nil, allow_self_link)
if item.q and item.q[1] or item.qq and item.qq[1] or item.l and item.l[1] or item.ll and item.ll[1]
or item.refs and item.refs[1] then
text = require(pron_qualifier_module).format_qualifiers {
lang = item.lang,
text = text,
q = item.q,
qq = item.qq,
l = item.l,
ll = item.ll,
refs = item.refs,
}
end
ins(text)
end
-- If there are labels, construct them now and append to final output.
if labels then
if lang:hasTranslit() then
ins(" — " .. require(labels_module).format_processed_labels {
labels = labels, lang = lang
})
else
ins(" " .. require(labels_module).format_processed_labels {
labels = labels, lang = lang, open = "(", close = ")"
})
end
end
return table.concat(parts)
end
--[==[
Template-callable function for displaying alternative forms.
]==]
function export.create(frame)
local parent_args = frame:getParent().args
local title = mw.title.getCurrentTitle()
local PAGENAME = title.text
return export.display_alternative_forms(parent_args, title)
end
return export