Jump to content

Module:TrimArray: Difference between revisions

From PanEcoDevWiki
No edit summary
Tag: Manual revert
No edit summary
 
Line 7: Line 7:
     local out = {}
     local out = {}


     -- Build a table of alt forms
     -- Build alt_forms table
     local alt_forms = {}
     local alt_forms = {}
    local index = 0
     for af in mw.text.gsplit(alt_forms_str, ",", true) do
     for af in mw.text.gsplit(alt_forms_str, ",", true) do
         local trimmed = mw.text.trim(af)
         local trimmed = mw.text.trim(af)
         if trimmed ~= "" then
         if trimmed ~= "" then
             alt_forms[index] = trimmed
             table.insert(alt_forms, trimmed)
            index = index + 1
         end
         end
     end
     end
Line 23: Line 21:
         if trimmed_item ~= "" then
         if trimmed_item ~= "" then
             local alt_form_params = ""
             local alt_form_params = ""
            -- Build alt_form parameters only if alt_forms exist
             if #alt_forms > 0 then
             if #alt_forms > 0 then
                 for i, af in ipairs(alt_forms) do
                 for i, af in ipairs(alt_forms) do
Line 31: Line 27:
             end
             end


             -- Determine whether to include form=
             -- Include form= only if alt_forms are empty
             local form_param = ""
             local form_param = ""
             if #alt_forms == 0 and form ~= "" then
             if #alt_forms == 0 and form ~= "" then
Line 37: Line 33:
             end
             end


            -- Build the #formredlink call
             local template_str = string.format(
             local template_str = string.format(
                 "{{#formredlink:target=%s%s%s}}",
                 "{{#formredlink:target=%s%s%s}}",
Line 44: Line 39:
                 alt_form_params
                 alt_form_params
             )
             )
             table.insert(out, frame:preprocess(template_str) .. "<br>")
             table.insert(out, frame:preprocess(template_str) .. "<br>")
         end
         end

Latest revision as of 21:54, 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 alt_forms table
    local alt_forms = {}
    for af in mw.text.gsplit(alt_forms_str, ",", true) do
        local trimmed = mw.text.trim(af)
        if trimmed ~= "" then
            table.insert(alt_forms, trimmed)
        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 = ""
            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

            -- Include form= only if alt_forms are empty
            local form_param = ""
            if #alt_forms == 0 and form ~= "" then
                form_param = string.format("|form=%s", form)
            end

            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