Module:Params: Difference between revisions
m 1 revision imported: Content from Template:Citation on Wikipedia |
:wikipedia>Grufo An unused variable has been removed from private function ‘make_groups()’; the code for the ‘scoring’ public function has been added commented out, for future inclusions |
||
| Line 110: | Line 110: | ||
-- always parsing numeric strings as numbers | -- always parsing numeric strings as numbers | ||
local function steal_if_renamed (val, src, skey, dest, dkey) | local function steal_if_renamed (val, src, skey, dest, dkey) | ||
local realkey = | local realkey = dkey:match'^%s*(.-)%s*$' | ||
if realkey == '0' or realkey:find'^%-?[1-9]%d*$' ~= nil then | |||
realkey = tonumber(realkey) | |||
end | |||
if skey ~= realkey then | if skey ~= realkey then | ||
dest[realkey] = val | dest[realkey] = val | ||
src[skey] = nil | src[skey] = nil | ||
end | end | ||
end | |||
-- Given a table, create two new tables containing the sorted list of keys | |||
local function get_key_list_sorted (ctx, tbl, sort_fn) | |||
local nums = {} | |||
local words = {} | |||
local nn = 0 | |||
local nw = 0 | |||
for key, val in pairs(tbl) do | |||
if type(key) == 'number' then | |||
nn = nn + 1 | |||
nums[nn] = key | |||
else | |||
nw = nw + 1 | |||
words[nw] = key | |||
end | |||
end | |||
table.sort(nums) | |||
table.sort(words, sort_fn) | |||
return nums, words, nn, nw | |||
end | end | ||
| Line 170: | Line 194: | ||
-- Memory slots (functions and modifiers MUST avoid these names) | -- Memory slots (functions and modifiers MUST avoid these names) | ||
local memoryslots = { | local memoryslots = { | ||
h = 'header', | |||
f = 'footer', | |||
i = 'itersep', | i = 'itersep', | ||
l = 'lastsep', | l = 'lastsep', | ||
n = 'ifngiven', | |||
p = 'pairsep', | p = 'pairsep', | ||
s = 'oxfordsep' | |||
} | } | ||
| Line 226: | Line 251: | ||
list = true, | list = true, | ||
list_values = true, | list_values = true, | ||
list_maybe_with_names = true, | |||
value_of = true | value_of = true | ||
} | } | ||
| Line 240: | Line 266: | ||
concat_and_invoke = true, | concat_and_invoke = true, | ||
concat_and_magic = true, | concat_and_magic = true, | ||
renaming_to_uppercase = true, | |||
renaming_to_lowercase = true, | |||
count = true, | count = true, | ||
--inserting = true, | --inserting = true, | ||
| Line 332: | Line 358: | ||
local tbl = {} | local tbl = {} | ||
local pin = start_from | local pin = start_from | ||
if src[pin] ~= nil and src[pin]:match'^%s*let%s*$' then | if src[pin] ~= nil and src[pin]:match'^%s*let%s*$' and | ||
src[pin + 1] ~= nil and src[pin + 2] ~= nil | |||
then | |||
names = {} | names = {} | ||
repeat | repeat | ||
tmp = src[pin + 1] | tmp = src[pin + 1]:match'^%s*(.-)%s*$' | ||
if tmp == '0' or tmp:find'^%-?[1-9]%d*$' ~= nil then | |||
src[pin + 2] | tmp = tonumber(tmp) | ||
end | |||
names[tmp] = src[pin + 2] | |||
pin = pin + 3 | pin = pin + 3 | ||
until src[pin] == nil or not src[pin]:match'^%s*let%s*$' | until src[pin] == nil or not src[pin]:match'^%s*let%s*$' or | ||
src[pin + 1] == nil or src[pin + 2] == nil | |||
end | end | ||
tmp = tonumber(src[pin]) | tmp = tonumber(src[pin]) | ||
| Line 371: | Line 402: | ||
tmp = style[6] | tmp = style[6] | ||
if tmp > -1 then | if tmp > -1 then | ||
karg = src[tmp + shf]:match'^%s*(.-)%s*$' | |||
if karg == '0' or karg:find'^%-?[1-9]%d*$' ~= nil then | |||
if karg == | karg = tonumber(karg) | ||
n_exist = math.max(n_exist, karg) | |||
end | |||
end | end | ||
tmp = style[7] | tmp = style[7] | ||
if tmp > -1 then | if tmp > -1 then | ||
varg = src[tmp + shf]:match'^%s*(.-)%s*$' | |||
if varg == '0' or varg:find'^%-?[1-9]%d*$' ~= nil then | |||
if varg == | varg = tonumber(varg) | ||
n_exist = math.max(n_exist, varg) | |||
end | |||
end | end | ||
local dest, nargs = load_child_opts(src, style[2] + shf, n_exist) | local dest, nargs = load_child_opts(src, style[2] + shf, n_exist) | ||
| Line 551: | Line 584: | ||
local function make_groups (src) | local function make_groups (src) | ||
-- NOTE: `src` might be the original metatable! | -- NOTE: `src` might be the original metatable! | ||
local prefix | local prefix | ||
local gid | local gid | ||
| Line 557: | Line 589: | ||
for key, val in pairs(src) do | for key, val in pairs(src) do | ||
-- `key` must only be a string or a number... | -- `key` must only be a string or a number... | ||
if type(key) == 'string' then | |||
prefix, gid = key:match'^%s*(.-)%s*(%-?%d*)%s*$' | prefix, gid = key:match'^%s*(.-)%s*(%-?%d*)%s*$' | ||
gid = tonumber(gid) or '' | gid = tonumber(gid) or '' | ||
else prefix = '' end | else | ||
prefix = '' | |||
gid = key | |||
end | |||
if groups[gid] == nil then groups[gid] = {} end | if groups[gid] == nil then groups[gid] = {} end | ||
if prefix == '0' or prefix:find'^%-?[1-9]%d*$' ~= nil then | |||
prefix = tonumber(prefix) | |||
if | if prefix < 1 then prefix = prefix - 1 end | ||
end | end | ||
groups[gid][prefix] = val | groups[gid][prefix] = val | ||
| Line 618: | Line 652: | ||
idx = idx + 1 | idx = idx + 1 | ||
else | else | ||
key = str:sub(1, spos1 - 1) | key = str:sub(1, spos1 - 1):match'^%s*(.-)%s*$' | ||
if key == '0' or key:find'^%-?[1-9]%d*$' ~= nil then | |||
key = tonumber(key) | |||
end | |||
val = str:sub(spos2 + 1) | val = str:sub(spos2 + 1) | ||
if trn then val = val:match'^%s*(.-)%s*$' end | if trn then val = val:match'^%s*(.-)%s*$' end | ||
| Line 647: | Line 683: | ||
idx = idx + 1 | idx = idx + 1 | ||
else | else | ||
key = val:sub(1, spos1 - 1) | key = val:sub(1, spos1 - 1):match'^%s*(.-)%s*$' | ||
if key == '0' or key:find'^%-?[1-9]%d*$' ~= nil then | |||
key = tonumber(key) | |||
end | |||
val = val:sub(spos2 + 1) | val = val:sub(spos2 + 1) | ||
if trn then val = val:match'^%s*(.-)%s*$' end | if trn then val = val:match'^%s*(.-)%s*$' end | ||
| Line 695: | Line 733: | ||
end | end | ||
if ctx.sorttype > 0 then | if ctx.sorttype > 0 then | ||
local nums | local nums, words, nn, nw = get_key_list_sorted(ctx, tbl, natural_sort) | ||
if ctx.sorttype == 2 then | if ctx.sorttype == 2 then | ||
for idx = 1, nw do fn(words[idx], tbl[words[idx]]) end | for idx = 1, nw do fn(words[idx], tbl[words[idx]]) end | ||
| Line 729: | Line 753: | ||
-- Flush the parameters by calling one of two custom functions for each value | |||
--[[ Modifiers ]]-- | -- (after this function has been invoked `ctx.params` will be no longer usable) | ||
----------------------------- | local function mixed_flush_params (ctx, fn_seq, fn_oth) | ||
if ctx.subset == 1 then | |||
for key, val in ipairs(ctx.params) do fn_seq(key, val) end | |||
return | |||
end | |||
if ctx.subset == -1 then | |||
flush_params(ctx, fn_oth) | |||
return | |||
end | |||
local tbl = ctx.params | |||
if ctx.sorttype > 0 then | |||
local nums, words, nn, nw = get_key_list_sorted(ctx, tbl, natural_sort) | |||
local sequence = {} | |||
for key, val in ipairs(tbl) do sequence[key] = val end | |||
if ctx.sorttype == 2 then | |||
for idx = 1, nw do fn_oth(words[idx], tbl[words[idx]]) end | |||
end | |||
for idx = 1, nn do | |||
if sequence[nums[idx]] then | |||
fn_seq(nums[idx], sequence[nums[idx]]) | |||
else | |||
fn_oth(nums[idx], tbl[nums[idx]]) | |||
end | |||
end | |||
if ctx.sorttype ~= 2 then | |||
for idx = 1, nw do fn_oth(words[idx], tbl[words[idx]]) end | |||
end | |||
return | |||
end | |||
if ctx.subset ~= -1 then | |||
for key, val in ipairs(tbl) do | |||
fn_seq(key, val) | |||
tbl[key] = nil | |||
end | |||
end | |||
for key, val in pairs(tbl) do fn_oth(key, val) end | |||
end | |||
-- Finalize and return a concatenated list | |||
local function finalize_and_return_concatenated_list (ctx, lst, len, modsize) | |||
if len > 0 then | |||
local tmp = ctx.oxfordsep or ctx.lastsep | |||
if tmp ~= nil and len > modsize * 2 then | |||
lst[len - modsize + 1] = tmp | |||
elseif len > modsize and ctx.lastsep ~= nil then | |||
lst[len - modsize + 1] = ctx.lastsep | |||
end | |||
lst[1] = ctx.header or '' | |||
if ctx.footer ~= nil then lst[len + 1] = ctx.footer end | |||
ctx.text = table.concat(lst) | |||
else ctx.text = ctx.ifngiven or '' end | |||
end | |||
--[[ Modifiers ]]-- | |||
----------------------------- | |||
| Line 813: | Line 894: | ||
-- Syntax: #invoke:params|squeezing|pipe to | -- Syntax: #invoke:params|scoring|new parameter name|pipe to | ||
--[[ | |||
library.scoring = function (ctx) | |||
if ctx.pipe[1] == nil then error(modulename .. | |||
', ‘scoring’: No parameter name was provided', 0) end | |||
local retval = 0 | |||
for _ in pairs(ctx.params) do retval = retval + 1 end | |||
ctx.params[ctx.pipe[1]:match'^%s*(.-)%s*$'] = tostring(retval) | |||
return context_iterate(ctx, 2) | |||
end | |||
]]-- | |||
-- Syntax: #invoke:params|squeezing|pipe to | |||
library.squeezing = function (ctx) | library.squeezing = function (ctx) | ||
local tbl = ctx.params | local tbl = ctx.params | ||
| Line 1,123: | Line 1,217: | ||
', ‘imposing’: Missing parameter name to impose', 0) end | ', ‘imposing’: Missing parameter name to impose', 0) end | ||
local key = ctx.pipe[1]:match'^%s*(.-)%s*$' | local key = ctx.pipe[1]:match'^%s*(.-)%s*$' | ||
if key == '0' or key:find'^%-?[1-9]%d*$' then key = tonumber(key) end | |||
ctx.params[key] = ctx.pipe[2] | |||
return context_iterate(ctx, 3) | return context_iterate(ctx, 3) | ||
end | end | ||
| Line 1,133: | Line 1,228: | ||
', ‘providing’: Missing parameter name to provide', 0) end | ', ‘providing’: Missing parameter name to provide', 0) end | ||
local key = ctx.pipe[1]:match'^%s*(.-)%s*$' | local key = ctx.pipe[1]:match'^%s*(.-)%s*$' | ||
key = tonumber(key) | if key == '0' or key:find'^%-?[1-9]%d*$' ~= nil then | ||
key = tonumber(key) | |||
end | |||
if ctx.params[key] == nil then ctx.params[key] = ctx.pipe[2] end | if ctx.params[key] == nil then ctx.params[key] = ctx.pipe[2] end | ||
return context_iterate(ctx, 3) | return context_iterate(ctx, 3) | ||
| Line 1,143: | Line 1,240: | ||
if ctx.pipe[1] == nil then error(modulename .. | if ctx.pipe[1] == nil then error(modulename .. | ||
', ‘discarding’: Missing parameter name to discard', 0) end | ', ‘discarding’: Missing parameter name to discard', 0) end | ||
local key | local key | ||
local len = tonumber(ctx.pipe[2]) | local len = tonumber(ctx.pipe[2]) | ||
if len == nil then | if len == nil then | ||
ctx. | key = ctx.pipe[1]:match'^%s*(.-)%s*$' | ||
if key == '0' or key:find'^%-?[1-9]%d*$' ~= nil then | |||
key = tonumber(key) | |||
end | |||
ctx.params[key] = nil | |||
return context_iterate(ctx, 2) | return context_iterate(ctx, 2) | ||
end | end | ||
key = tonumber( | key = tonumber(ctx.pipe[1]) | ||
if key == nil then error(modulename .. | if key == nil then error(modulename .. | ||
', ‘discarding’: A range was provided, but the initial parameter name is not numeric', 0) end | ', ‘discarding’: A range was provided, but the initial parameter name is not numeric', 0) end | ||
| Line 1,194: | Line 1,295: | ||
ptn = targets[idx] | ptn = targets[idx] | ||
if ptn[3] then | if ptn[3] then | ||
tmp = | tmp = ptn[1] | ||
if tmp == '0' or tmp:find'^%-?[1-9]%d*$' ~= nil then | |||
tmp = tonumber(tmp) | |||
end | |||
newparams[tmp] = tbl[tmp] | newparams[tmp] = tbl[tmp] | ||
else | else | ||
| Line 1,218: | Line 1,322: | ||
if nptns == 1 and targets[1][3] then | if nptns == 1 and targets[1][3] then | ||
local tmp = targets[1][1] | local tmp = targets[1][1] | ||
tbl[tonumber(tmp) | if tmp == '0' or tmp:find'^%-?[1-9]%d*$' ~= nil then | ||
tbl[tonumber(tmp)] = nil | |||
else tbl[tmp] = nil end | |||
return context_iterate(ctx, argc) | return context_iterate(ctx, argc) | ||
end | end | ||
| Line 1,309: | Line 1,415: | ||
-- Syntax: #invoke:params| | -- Syntax: #invoke:params|mapping_to_lowercase|pipe to | ||
library. | library.mapping_to_lowercase = function (ctx) | ||
local tbl = ctx.params | local tbl = ctx.params | ||
for key, val in pairs(tbl) do tbl[key] = val:lower() end | for key, val in pairs(tbl) do tbl[key] = val:lower() end | ||
| Line 1,317: | Line 1,423: | ||
-- Syntax: #invoke:params| | -- Syntax: #invoke:params|mapping_to_uppercase|pipe to | ||
library. | library.mapping_to_uppercase = function (ctx) | ||
local tbl = ctx.params | local tbl = ctx.params | ||
for key, val in pairs(tbl) do tbl[key] = val:upper() end | for key, val in pairs(tbl) do tbl[key] = val:upper() end | ||
| Line 1,420: | Line 1,526: | ||
local tbl = ctx.params | local tbl = ctx.params | ||
if mix == '$#' then | if mix == '$#' then | ||
for key in pairs(tbl) do tbl[key] = key end | for key in pairs(tbl) do tbl[key] = tostring(key) end | ||
return context_iterate(ctx, 2) | return context_iterate(ctx, 2) | ||
end | end | ||
| Line 1,444: | Line 1,550: | ||
-- Syntax: #invoke:params| | -- Syntax: #invoke:params|renaming_to_lowercase|pipe to | ||
library. | library.renaming_to_lowercase = function (ctx) | ||
-- NOTE: `ctx.params` might be the original metatable! As a modifier, | -- NOTE: `ctx.params` might be the original metatable! As a modifier, | ||
-- this function MUST create a copy of it before returning | -- this function MUST create a copy of it before returning | ||
| Line 1,458: | Line 1,564: | ||
-- Syntax: #invoke:params| | -- Syntax: #invoke:params|renaming_to_uppercase|pipe to | ||
library. | library.renaming_to_uppercase = function (ctx) | ||
-- NOTE: `ctx.params` might be the original metatable! As a modifier, | -- NOTE: `ctx.params` might be the original metatable! As a modifier, | ||
-- this function MUST create a copy of it before returning | -- this function MUST create a copy of it before returning | ||
| Line 1,512: | Line 1,618: | ||
'’ does not exist', 0) end | '’ does not exist', 0) end | ||
map_names(ctx.params, rargs, karg, varg, looptype, function () | map_names(ctx.params, rargs, karg, varg, looptype, function () | ||
return tostring(mfunc(ctx.frame:newChild(model))) | |||
end) | end) | ||
return context_iterate(ctx, argc) | return context_iterate(ctx, argc) | ||
| Line 1,545: | Line 1,650: | ||
local tbl = ctx.params | local tbl = ctx.params | ||
if flg == 3 then | if flg == 3 then | ||
ptn = ptn:match'^%s*(.-)%s*$' | |||
local val = tbl[ | if ptn == '0' or ptn:find'^%-?[1-9]%d*$' ~= nil then | ||
ptn = tonumber(ptn) | |||
end | |||
local val = tbl[ptn] | |||
if val ~= nil then | if val ~= nil then | ||
tbl[ | tbl[ptn] = nil | ||
repl = repl:match'^%s*(.-)%s*$' | |||
if repl == '0' or repl:find'^%-?[1-9]%d*$' ~= nil then | |||
repl = tonumber(repl) | |||
end | |||
tbl[repl] = val | |||
end | end | ||
else | else | ||
| Line 1,575: | Line 1,687: | ||
local mix = ctx.pipe[1]:match'^%s*(.-)%s*$' | local mix = ctx.pipe[1]:match'^%s*(.-)%s*$' | ||
local cache = {} | local cache = {} | ||
local tmp | |||
if mix == '$@' then | if mix == '$@' then | ||
for _, val in pairs(ctx.params) do cache[val] = val end | for _, val in pairs(ctx.params) do | ||
tmp = val:match'^%s*(.-)%s*$' | |||
if tmp == '0' or tmp:find'^%-?[1-9]%d*$' ~= nil then | |||
cache[tonumber(tmp)] = val | |||
else cache[tmp] = val end | |||
end | |||
else | else | ||
local skel, canvas, n_parts = parse_placeholder_string(mix) | local skel, canvas, n_parts = parse_placeholder_string(mix) | ||
for key, val in pairs(ctx.params) do | for key, val in pairs(ctx.params) do | ||
for idx = 2, n_parts, 2 do | for idx = 2, n_parts, 2 do | ||
| Line 1,585: | Line 1,702: | ||
else canvas[idx] = tostring(key) end | else canvas[idx] = tostring(key) end | ||
end | end | ||
tmp = table.concat(canvas) | tmp = table.concat(canvas):match'^%s*(.-)%s*$' | ||
cache[tonumber(tmp) | if tmp == '0' or tmp:find'^%-?[1-9]%d*$' ~= nil then | ||
cache[tonumber(tmp)] = val | |||
else cache[tmp] = val end | |||
end | end | ||
end | end | ||
| Line 1,661: | Line 1,780: | ||
load_parse_opts(opts, 2) | load_parse_opts(opts, 2) | ||
local tbl = ctx.params | local tbl = ctx.params | ||
local tmp = | local tmp = opts[1]:match'^%s*(.-)%s*$' | ||
if tmp == '0' or tmp:find'^%-?[1-9]%d*$' ~= nil then | |||
tmp = tonumber(tmp) | |||
end | |||
local str = tbl[tmp] | local str = tbl[tmp] | ||
if str ~= nil then | if str ~= nil then | ||
| Line 1,683: | Line 1,805: | ||
local mix_v = ctx.pipe[2] | local mix_v = ctx.pipe[2] | ||
local cache = {} | local cache = {} | ||
local tmp | |||
if mix_k == '$@' and mix_v == '$@' then | if mix_k == '$@' and mix_v == '$@' then | ||
for _, val in pairs(ctx.params) do cache[val] = val end | for _, val in pairs(ctx.params) do | ||
tmp = val:match'^%s*(.-)%s*$' | |||
if tmp == '0' or tmp:find'^%-?[1-9]%d*$' ~= nil then | |||
cache[tonumber(tmp)] = val | |||
else cache[tmp] = val end | |||
end | |||
elseif mix_k == '$@' and mix_v == '$#' then | elseif mix_k == '$@' and mix_v == '$#' then | ||
for key, val in pairs(ctx.params) do cache[ | for key, val in pairs(ctx.params) do | ||
tmp = val:match'^%s*(.-)%s*$' | |||
if tmp == '0' or tmp:find'^%-?[1-9]%d*$' ~= nil then | |||
cache[tonumber(tmp)] = tostring(key) | |||
else cache[tmp] = tostring(key) end | |||
end | |||
elseif mix_k == '$#' and mix_v == '$#' then | elseif mix_k == '$#' and mix_v == '$#' then | ||
for _, val in pairs(ctx.params) do cache[key] = key end | for _, val in pairs(ctx.params) do cache[key] = tostring(key) end | ||
else | else | ||
local skel_k, cnv_k, n_parts_k = parse_placeholder_string(mix_k) | local skel_k, cnv_k, n_parts_k = parse_placeholder_string(mix_k) | ||
local skel_v, cnv_v, n_parts_v = parse_placeholder_string(mix_v) | local skel_v, cnv_v, n_parts_v = parse_placeholder_string(mix_v) | ||
for key, val in pairs(ctx.params) do | for key, val in pairs(ctx.params) do | ||
tmp = tostring(key) | tmp = tostring(key) | ||
| Line 1,701: | Line 1,833: | ||
if skel_v[idx] then cnv_v[idx] = val else cnv_v[idx] = tmp end | if skel_v[idx] then cnv_v[idx] = val else cnv_v[idx] = tmp end | ||
end | end | ||
tmp = table.concat(cnv_k) | tmp = table.concat(cnv_k):match'^%s*(.-)%s*$' | ||
if tmp == '0' or tmp:find'^%-?[1-9]%d*$' ~= nil then | |||
tmp = tonumber(tmp) | |||
end | |||
cache[tmp] = table.concat(cnv_v) | |||
end | end | ||
end | end | ||
| Line 1,735: | Line 1,870: | ||
if merge_into == nil then error(modulename .. | if merge_into == nil then error(modulename .. | ||
', ‘combining_by_calling’: No parameter name was provided', 0) end | ', ‘combining_by_calling’: No parameter name was provided', 0) end | ||
merge_into = | merge_into = merge_into:match'^%s*(.-)%s*$' | ||
if merge_into == '0' or merge_into:find'^%-?[1-9]%d*$' ~= nil then | |||
merge_into = tonumber(merge_into) | |||
end | |||
ctx.params = { | ctx.params = { | ||
[merge_into] = ctx.frame:expandTemplate{ | [merge_into] = ctx.frame:expandTemplate{ | ||
| Line 1,793: | Line 1,931: | ||
local tmp = ctx.n_parents | local tmp = ctx.n_parents | ||
if tmp < 1 then parent = ctx.oparams else parent = ctx.parents[tmp] end | if tmp < 1 then parent = ctx.oparams else parent = ctx.parents[tmp] end | ||
tmp = | tmp = opts[1]:match'^%s*(.-)%s*$' | ||
if tmp == '0' or tmp:find'^%-?[1-9]%d*$' ~= nil then | |||
tmp = tonumber(tmp) | |||
end | |||
if parent[tmp] ~= nil then ctx.params[tmp] = parent[tmp] end | if parent[tmp] ~= nil then ctx.params[tmp] = parent[tmp] end | ||
return context_iterate(ctx, 2) | return context_iterate(ctx, 2) | ||
| Line 1,810: | Line 1,951: | ||
-- Syntax: #invoke:params| | -- Syntax: #invoke:params|dropping_substack|pipe to | ||
library. | library.dropping_substack = function (ctx) | ||
local ncurrparent = ctx.n_parents | local ncurrparent = ctx.n_parents | ||
if ncurrparent < 1 then error(modulename .. | if ncurrparent < 1 then error(modulename .. | ||
', ‘leaving_substack’: No substack has been created', 0) end | ', ‘dropping_substack’: No substack has been created', 0) end | ||
ctx.params = ctx.parents[ncurrparent] | |||
ctx.parents[ncurrparent] = nil | |||
ctx.n_parents = ncurrparent - 1 | |||
return context_iterate(ctx, 1) | |||
end | |||
-- Syntax: #invoke:params|leaving_substack|pipe to | |||
library.leaving_substack = function (ctx) | |||
local ncurrparent = ctx.n_parents | |||
if ncurrparent < 1 then error(modulename .. | |||
', ‘leaving_substack’: No substack has been created', 0) end | |||
local currsnap = ctx.n_children + 1 | local currsnap = ctx.n_children + 1 | ||
if ctx.children == nil then ctx.children = { ctx.params } | if ctx.children == nil then ctx.children = { ctx.params } | ||
| Line 1,936: | Line 2,089: | ||
-- NOTE: `ctx.pipe` and `ctx.params` might be the original metatables! | -- NOTE: `ctx.pipe` and `ctx.params` might be the original metatables! | ||
local opts = ctx.pipe | local opts = ctx.pipe | ||
if opts[1] == nil then error(modulename .. | |||
if opts[1] | |||
', ‘value_of’: No parameter name was provided', 0) end | ', ‘value_of’: No parameter name was provided', 0) end | ||
local | local val | ||
local key = opts[1]:match'^%s*(.-)%s*$' | |||
if key == '0' or key:find'^%-?[1-9]%d*$' ~= nil then | |||
key = tonumber(key) | |||
val = ctx.params[key] | |||
-- No worries: #ctx.params is unused if the modifier in first position | |||
if val ~= nil and ( | |||
ctx.subset ~= -1 or key > #ctx.params or key < 1 | |||
ctx.text = (ctx.header or '') .. val .. (ctx.footer or '') | ) and ( | ||
ctx.subset ~= 1 or (key <= #ctx.params and key > 0) | |||
) then | |||
ctx.text = (ctx.header or '') .. val .. (ctx.footer or '') | |||
else ctx.text = ctx.ifngiven or '' end | |||
else | |||
val = ctx.params[key] | |||
if ctx.subset ~= 1 and val ~= nil then | |||
ctx.text = (ctx.header or '') .. val .. (ctx.footer or '') | |||
else ctx.text = ctx.ifngiven or '' end | |||
end | end | ||
return false | return false | ||
end | end | ||
| Line 1,973: | Line 2,131: | ||
end | end | ||
) | ) | ||
finalize_and_return_concatenated_list(ctx, ret, nss, 4) | |||
return false | return false | ||
end | end | ||
| Line 2,002: | Line 2,151: | ||
end | end | ||
) | ) | ||
finalize_and_return_concatenated_list(ctx, ret, nss, 2) | |||
return false | |||
ret[nss | end | ||
-- Syntax: #invoke:params|list_maybe_with_names | |||
library.list_maybe_with_names = function (ctx) | |||
-- NOTE: `ctx.pipe` might be the original metatable! | |||
local kvs = ctx.pairsep or '' | |||
local pps = ctx.itersep or '' | |||
local ret = {} | |||
local nss = 0 | |||
mixed_flush_params( | |||
ctx, | |||
function (key, val) | |||
ret[nss + 1] = pps | |||
ret[nss + 2] = '' | |||
ret[nss + 3] = '' | |||
ret[nss + 4] = val | |||
nss = nss + 4 | |||
end, | |||
function (key, val) | |||
ret[nss + 1] = pps | |||
ret[nss + 2] = key | |||
ret[nss + 3] = kvs | |||
ret[nss + 4] = val | |||
nss = nss + 4 | |||
end | end | ||
) | |||
finalize_and_return_concatenated_list(ctx, ret, nss, 4) | |||
return false | return false | ||
end | end | ||
| Line 2,018: | Line 2,187: | ||
-- Syntax: #invoke:params|coins|[first coin = value 1]|[second coin = value | -- Syntax: #invoke:params|coins|[first coin = value 1]|[second coin = value | ||
-- 2]|[...]|[last coin = value N] | -- 2]|[...]|[last coin = value N] | ||
library.coins = function (ctx) | library.coins = function (ctx) | ||
-- NOTE: `ctx.pipe` might be the original metatable! | -- NOTE: `ctx.pipe` might be the original metatable! | ||
local opts = ctx.pipe | local opts = ctx.pipe | ||
local tbl = ctx.params | local tbl = ctx.params | ||
for key, val in pairs(tbl) do tbl[key] = opts[tonumber( | local tmp | ||
return library.list_values(ctx) | for key, val in pairs(tbl) do | ||
end | tmp = val:match'^%s*(.-)%s*$' | ||
if tmp == '0' or tmp:find'^%-?[1-9]%d*$' ~= nil then | |||
tbl[key] = opts[tonumber(tmp)] | |||
-- Syntax: #invoke:params|unique_coins|[first coin = value 1]|[second coin = | else tbl[key] = opts[tmp] end | ||
-- value 2]|[...]|[last coin = value N] | end | ||
library.unique_coins = function (ctx) | return library.list_values(ctx) | ||
local opts = ctx.pipe | end | ||
local tbl = ctx.params | |||
local tmp | |||
for key, val in pairs(tbl) do | -- Syntax: #invoke:params|unique_coins|[first coin = value 1]|[second coin = | ||
tmp = tonumber( | -- value 2]|[...]|[last coin = value N] | ||
library.unique_coins = function (ctx) | |||
local opts = ctx.pipe | |||
local tbl = ctx.params | |||
local tmp | |||
for key, val in pairs(tbl) do | |||
tmp = val:match'^%s*(.-)%s*$' | |||
if tmp == '0' or tmp:find'^%-?[1-9]%d*$' ~= nil then | |||
tmp = tonumber(tmp) | |||
end | |||
tbl[key] = opts[tmp] | tbl[key] = opts[tmp] | ||
opts[tmp] = nil | opts[tmp] = nil | ||
| Line 2,062: | Line 2,240: | ||
end | end | ||
) | ) | ||
finalize_and_return_concatenated_list(ctx, ret, nss, 2) | |||
return false | return false | ||
end | end | ||
| Line 2,100: | Line 2,269: | ||
end | end | ||
) | ) | ||
finalize_and_return_concatenated_list(ctx, ret, nss, 2) | |||
return false | return false | ||
end | end | ||
| Line 2,142: | Line 2,302: | ||
end | end | ||
) | ) | ||
finalize_and_return_concatenated_list(ctx, ret, nss, 2) | |||
return false | return false | ||
end | end | ||
| Line 2,179: | Line 2,330: | ||
end | end | ||
) | ) | ||
finalize_and_return_concatenated_list(ctx, ret, nss, 2) | |||
return false | return false | ||
end | end | ||
| Line 2,215: | Line 2,357: | ||
end | end | ||
) | ) | ||
finalize_and_return_concatenated_list(ctx, ret, nss, 2) | |||
return false | return false | ||
end | end | ||
| Line 2,257: | Line 2,390: | ||
end | end | ||
) | ) | ||
finalize_and_return_concatenated_list(ctx, ret, nss, 2) | |||
return false | return false | ||
end | end | ||
| Line 2,292: | Line 2,416: | ||
end | end | ||
) | ) | ||
finalize_and_return_concatenated_list(ctx, ret, nss, 2) | |||
return false | return false | ||
end | end | ||
| Line 2,338: | Line 2,453: | ||
end | end | ||
) | ) | ||
finalize_and_return_concatenated_list(ctx, ret, nss, 2) | |||
return false | return false | ||
end | end | ||