Jump to content

Module:ListComponents: Difference between revisions

From Drifters Almanac
m Protected "Module:ListComponents" ([Edit=Allow only administrators] (indefinite) [Move=Allow only administrators] (indefinite))
No edit summary
 
Line 2: Line 2:


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


    table.insert(output, '\n<ul style="margin-left: 1em;">')
     for _, entry in ipairs(entries) do
     for _, combo in ipairs(combinations) do
         local trimmed = mw.text.trim(entry)
         combo = mw.text.trim(combo)
         if trimmed ~= "" then
         if combo ~= "" then
             table.insert(result, string.format("[[%s]]", trimmed, trimmed))
             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
     end
     end
    table.insert(output, '</ul>\n')


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


return p
return p

Latest revision as of 12:23, 6 May 2025

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

local p = {}

function p.fromDelimited(frame)
    local input = frame.args[1] or ""
    local delimiter = frame.args.delimiter or ","
    local entries = mw.text.split(input, delimiter)
    local result = {}

    for _, entry in ipairs(entries) do
        local trimmed = mw.text.trim(entry)
        if trimmed ~= "" then
            table.insert(result, string.format("[[%s]]", trimmed, trimmed))
        end
    end

    return table.concat(result, " • ")
end

return p