Module:TrimArray: Difference between revisions
Appearance
No edit summary Tag: Reverted |
No edit summary Tag: Manual revert |
||
| Line 14: | Line 14: | ||
if trimmed ~= "" then | if trimmed ~= "" then | ||
alt_forms[index] = trimmed | alt_forms[index] = trimmed | ||
index = index | index = index + 1 | ||
end | end | ||
end | end | ||
Revision as of 21:47, 28 October 2025
Documentation for this module may be created at Module:TrimArray/doc
local p = {}
function p.links(frame)
local input = frame.args[1] or ""
local form = frame.args.form or ""
local alt_forms_str = frame.args.alt_forms or ""
local out = {}
-- Build a table of alt forms
local alt_forms = {}
local index = 0
for af in mw.text.gsplit(alt_forms_str, ",", true) do
local trimmed = mw.text.trim(af)
if trimmed ~= "" then
alt_forms[index] = trimmed
index = index + 1
end
end
-- Loop over each item
for item in mw.text.gsplit(input, ",", true) do
local trimmed_item = mw.text.trim(item)
if trimmed_item ~= "" then
local alt_form_params = ""
-- Build alt_form parameters only if alt_forms exist
if #alt_forms > 0 then
for i, af in ipairs(alt_forms) do
alt_form_params = alt_form_params .. string.format("|alt_form[%d]=%s", i-1, af)
end
end
-- Determine whether to include form=
local form_param = ""
if #alt_forms == 0 and form ~= "" then
form_param = string.format("|form=%s", form)
end
-- Build the #formredlink call
local template_str = string.format(
"{{#formredlink:target=%s%s%s}}",
trimmed_item,
form_param,
alt_form_params
)
table.insert(out, frame:preprocess(template_str) .. "<br>")
end
end
return table.concat(out)
end
return p