Jump to content

Module:ListComponents: Difference between revisions

From Drifters Almanac
Created page with "local p = {} function p.fromDelimited(frame) local raw = frame.args[1] or "" local delimiter = frame.args.delimiter or "," local combinations = mw.text.split(raw, delimiter) local output = {} table.insert(output, '<ul style="margin-left: 1em;">') for _, combo in ipairs(combinations) do combo = mw.text.trim(combo) if combo ~= "" then local parts = mw.text.split(combo, "+") local linked_parts = {}..."
 
No edit summary
Line 7: Line 7:
     local output = {}
     local output = {}


     table.insert(output, '<ul style="margin-left: 1em;">')
     table.insert(output, '\n<ul style="margin-left: 1em;">')
     for _, combo in ipairs(combinations) do
     for _, combo in ipairs(combinations) do
         combo = mw.text.trim(combo)
         combo = mw.text.trim(combo)
Line 23: Line 23:
         end
         end
     end
     end
     table.insert(output, '</ul>')
     table.insert(output, '</ul>\n')


     return table.concat(output, "\n")
     return table.concat(output, "\n")

Revision as of 15:35, 23 April 2025

Documentation for this module may be created at Module:ListComponents/doc

local p = {}

function p.fromDelimited(frame)
    local raw = frame.args[1] or ""
    local delimiter = frame.args.delimiter or ","
    local combinations = mw.text.split(raw, delimiter)
    local output = {}

    table.insert(output, '\n<ul style="margin-left: 1em;">')
    for _, combo in ipairs(combinations) do
        combo = mw.text.trim(combo)
        if combo ~= "" then
            local parts = mw.text.split(combo, "+")
            local linked_parts = {}
            for _, part in ipairs(parts) do
                local trimmed = mw.text.trim(part)
                local subparts = mw.text.split(trimmed, "::")
                local target = mw.text.trim(subparts[1])
                local label = subparts[2] and mw.text.trim(subparts[2]) or target
                table.insert(linked_parts, string.format("[[%s|%s]]", target, label))
            end
            table.insert(output, string.format("<li>%s</li>", table.concat(linked_parts, " + ")))
        end
    end
    table.insert(output, '</ul>\n')

    return table.concat(output, "\n")
end

return p