Module:parameter utilities: Difference between revisions
No edit summary |
No edit summary |
||
| Line 22: | Line 22: | ||
overhead after the first call, since the target functions are called directly in any subsequent calls. | overhead after the first call, since the target functions are called directly in any subsequent calls. | ||
]==] | ]==] | ||
local function is_callable(...) | local function is_callable(...) | ||
is_callable = require(functions_module).is_callable | is_callable = require(functions_module).is_callable | ||
| Line 72: | Line 71: | ||
export.default_special_separators = { | export.default_special_separators = { | ||
[";"] = "; ", | [";"] = "; ", | ||
["_"] = " ", | |||
["~"] = " ~ ", | |||
["→"] = " → ", | |||
} | |||
-- Table listing how subitem delimiters display. Unlike for `default_special_separators`, the presence of an item in | |||
-- this table does not mean that the delimiter is recognized; only those specified by `data.splitchar` are recognized. | |||
export.default_subitem_separator_map = { | |||
[";"] = "; ", | |||
[","] = ", ", | |||
["/"] = "/", | |||
["_"] = " ", | ["_"] = " ", | ||
["~"] = " ~ ", | ["~"] = " ~ ", | ||
| Line 766: | Line 776: | ||
-- otherwise the properties are stored directly into `termobj`. | -- otherwise the properties are stored directly into `termobj`. | ||
-- `has_subitems`: True if there are subitems. | -- `has_subitems`: True if there are subitems. | ||
-- `subitem_separator_map`: If `has_subitems` and this is specified, controls the assignment of the `separator` field | |||
-- in subitems. If not specified or a delimiter is not in the map, it is copied unchanged. | |||
-- `lang`: Language object to store into all items. | -- `lang`: Language object to store into all items. | ||
-- `sc`: Script object to store into all items, or nil. | -- `sc`: Script object to store into all items, or nil. | ||
| Line 802: | Line 814: | ||
local argval = fetch_separate_param(args, param_mod, itemno) | local argval = fetch_separate_param(args, param_mod, itemno) | ||
if not argval_missing(argval) then | if not argval_missing(argval) then | ||
local destobj = fetch_destobj(param_mod, dest) | local destobj = fetch_destobj(param_mod, param_mod_spec, dest) | ||
-- Don't overwrite a value already set by an inline modifier. | -- Don't overwrite a value already set by an inline modifier. | ||
if argval_missing(destobj[dest]) then | if argval_missing(destobj[dest]) then | ||
| Line 842: | Line 854: | ||
termobj.terms[1] = {} | termobj.terms[1] = {} | ||
end | end | ||
local function fetch_destobj(param_mod, dest) | local function fetch_destobj(param_mod, param_mod_spec, dest) | ||
if param_mod_spec.overall then | |||
return termobj | |||
end | |||
if data.subitem_param_handling == "only" and termobj.terms[2] then | if data.subitem_param_handling == "only" and termobj.terms[2] then | ||
error(("Can't specify a value for separate parameter %s%s= because there are " .. | error(("Can't specify a value for separate parameter %s%s= because there are " .. | ||
| Line 864: | Line 879: | ||
copy_separate_params_to_termobj(fetch_destobj) | copy_separate_params_to_termobj(fetch_destobj) | ||
for | for i, subitem in ipairs(termobj.terms) do | ||
set_lang_and_sc(subitem) | set_lang_and_sc(subitem) | ||
if subitem.delimiter then | |||
subitem.separator = i == 1 and "" or | |||
data.subitem_separator_map and data.subitem_separator_map[subitem.delimiter] or | |||
subitem.delimiter | |||
end | |||
if data.postprocess_termobj then | if data.postprocess_termobj then | ||
data.postprocess_termobj(subitem, data) | data.postprocess_termobj(subitem, data) | ||
| Line 882: | Line 902: | ||
local function postprocess_termobj(item, data) | local function postprocess_termobj(item, data) | ||
if not (data.disallow_custom_separators or data.use_semicolon) then | if not (data.disallow_custom_separators or data.use_semicolon) then | ||
if data.has_subitems and item. | if data.has_subitems and item.separator and item.separator:find(",", nil, true) then | ||
data.use_semicolon = true | data.use_semicolon = true | ||
else | else | ||
| Line 919: | Line 939: | ||
latter case where raw argument processing is done by the caller, they must build the partial `params` structure; | latter case where raw argument processing is done by the caller, they must build the partial `params` structure; | ||
augment it themselves using `augment_params_with_modifiers()`; call [[Module:parameters]] themselves; and pass in the | augment it themselves using `augment_params_with_modifiers()`; call [[Module:parameters]] themselves; and pass in the | ||
processed arguments. In both cases, the return value of this function contains | processed arguments. In both cases, the return value of this function contains three values: a list of objects, one | ||
term, specifying the term and all properties; | per term, specifying the term and all properties; the processed arguments structure, so that the non-term-property | ||
arguments can be processed as appropriate. | arguments can be processed as appropriate; and an object containing miscellaneous global computed properties | ||
(currently only `use_semicolon`; see below). | |||
# Optionally, each term can consist of a number of ''subitems'' separated by delimiters (usually a comma, but the | # Optionally, each term can consist of a number of ''subitems'' separated by delimiters (usually a comma, but the | ||
possible delimiter or delimiters are controllable). Each subitem can have its own inline modifiers. This functionality | possible delimiter or delimiters are controllable). Each subitem can have its own inline modifiers. This functionality | ||
| Line 1,052: | Line 1,073: | ||
in the last subitem). The default is {"only"}. As a special case, an {{para|scN}} separate parameter will be stored | in the last subitem). The default is {"only"}. As a special case, an {{para|scN}} separate parameter will be stored | ||
into all subitems. | into all subitems. | ||
* `subitem_separator_map`: Table mapping user-specified delimiters to displayed separators, stored in the `separator` | |||
field of the subitem. If not specified, it defaults to `default_subitem_separator_map`. Note that the presence of an | |||
item in this table does not mean that it can be used as a delimiter; only the delimiters specified using `splitchar` | |||
are recognized. Delimiters not in this map display as-is. | |||
6. Other fields: | 6. Other fields: | ||
| Line 1,073: | Line 1,098: | ||
signal the end of items and the start of labels. | signal the end of items and the start of labels. | ||
Three values are returned: the list of items; the processed `args` structure; and an object of miscellaneous computed | |||
field set for each specified property (either through inline modifiers or separate parameters). If subitems are not | global values (currently only `use_semicolon`, indicating that commas were found in individual arguments and so the | ||
allowed, each item directly has fields set on it for the specified properties. If subitems ''are'' allowed, each item | default separator should be a semicolon). In each returned item, there will be one field set for each specified property | ||
contains a `terms` field, which is a list of subitem objects, each of which has fields set on it for the specified | (either through inline modifiers or separate parameters). If subitems are not allowed, each item directly has fields set | ||
properties of that subitem. In addition, the following fields may be set on each item or subitem: | on it for the specified properties. If subitems ''are'' allowed, each item contains a `terms` field, which is a list of | ||
subitem objects, each of which has fields set on it for the specified properties of that subitem. In addition, the | |||
following fields may be set on each item or subitem: | |||
* `term`: The term portion of the item (minus inline modifiers and language prefixes). {nil} if no term was given. | * `term`: The term portion of the item (minus inline modifiers and language prefixes). {nil} if no term was given. | ||
* `orig_index`: The original index into the item in the items table returned by `process()` in [[Module:parameters]]. | * `orig_index`: The original index into the item in the items table returned by `process()` in [[Module:parameters]]. | ||
| Line 1,092: | Line 1,119: | ||
* `sc`: The script object of the item. This is set when either (a) the `sc` property is allowed and specified; (b) | * `sc`: The script object of the item. This is set when either (a) the `sc` property is allowed and specified; (b) | ||
`sc` isn't otherwise set and the `sc` field of the overall `data` object is set, providing a default value. | `sc` isn't otherwise set and the `sc` field of the overall `data` object is set, providing a default value. | ||
* `delimiter`: If subitems are allowed, this specifies the delimiter used prior to the given subitem (e.g. {","}). | * `delimiter`: If subitems are allowed, this is set on subitems and specifies the delimiter used prior to the given | ||
subitem (e.g. {","}). | |||
`disallow_custom_separators` is not given | * `separator`: The separator to display before the item. Always set on subitems, and set on top-level items if | ||
`disallow_custom_separators` is not given. Controlled by `special_separators` (for top-level items) and | |||
`subitem_separator_map` (for subitems). | |||
]==] | ]==] | ||
function export.parse_list_with_inline_modifiers_and_separate_params(data) | function export.parse_list_with_inline_modifiers_and_separate_params(data) | ||
| Line 1,227: | Line 1,256: | ||
sc = args.sc and args.sc[itemno] or sc, | sc = args.sc and args.sc[itemno] or sc, | ||
subitem_param_handling = data.subitem_param_handling, | subitem_param_handling = data.subitem_param_handling, | ||
subitem_separator_map = data.subitem_separator_map or export.default_subitem_separator_map, | |||
allow_conflicting_inline_mods_and_separate_params = | allow_conflicting_inline_mods_and_separate_params = | ||
data.allow_conflicting_inline_mods_and_separate_params, | data.allow_conflicting_inline_mods_and_separate_params, | ||
| Line 1,253: | Line 1,283: | ||
end | end | ||
return items, args | return items, args, {use_semicolon = use_semicolon} | ||
end | end | ||
| Line 1,389: | Line 1,419: | ||
in the last subitem). The default is {"only"}. As a special case, an {{para|scN}} separate parameter will be stored | in the last subitem). The default is {"only"}. As a special case, an {{para|scN}} separate parameter will be stored | ||
into all subitems. | into all subitems. | ||
* `subitem_separator_map`: Table mapping user-specified delimiters to displayed separators, stored in the `separator` | |||
field of the subitem. If not specified, it defaults to `default_subitem_separator_map`. Note that the presence of an | |||
item in this table does not mean that it can be used as a delimiter; only the delimiters specified using `splitchar` | |||
are recognized. Delimiters not in this map display as-is. | |||
Two values are returned, an object describing the item (or subitems) and the processed `args` structure. In the returned | Two values are returned, an object describing the item (or subitems) and the processed `args` structure. In the returned | ||
| Line 1,406: | Line 1,440: | ||
`sc` isn't otherwise set and the `sc` field of the overall `data` object is set, providing a default value. | `sc` isn't otherwise set and the `sc` field of the overall `data` object is set, providing a default value. | ||
* `delimiter`: If subitems are allowed, this specifies the delimiter used prior to the given subitem (e.g. {","}). | * `delimiter`: If subitems are allowed, this specifies the delimiter used prior to the given subitem (e.g. {","}). | ||
* `separator`: If subitems are allowed, this specifies the displayed form of the delimiter to be shown before a given | |||
subitem. The mapping from user-specified delimiters to displayed separators is handled by `subitem_separator_map`; | |||
see above. The first subitem always has a blank string in the `separator` field. | |||
]==] | ]==] | ||
function export.parse_term_with_inline_modifiers_and_separate_params(data) | function export.parse_term_with_inline_modifiers_and_separate_params(data) | ||
| Line 1,481: | Line 1,518: | ||
sc = sc, | sc = sc, | ||
subitem_param_handling = data.subitem_param_handling, | subitem_param_handling = data.subitem_param_handling, | ||
subitem_separator_map = data.subitem_separator_map or export.default_subitem_separator_map, | |||
allow_conflicting_inline_mods_and_separate_params = data.allow_conflicting_inline_mods_and_separate_params, | allow_conflicting_inline_mods_and_separate_params = data.allow_conflicting_inline_mods_and_separate_params, | ||
} | } | ||