Jump to content

Module:RecipeLinks

From Drifters Almanac
Revision as of 20:13, 30 May 2025 by Mildew (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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

local p = {}

function p.fromDelimited(frame)
    -- 1) Raw string (first unnamed argument) and optional “delimiter”
    local raw       = frame.args[1] or ""
    local delimiter = frame.args.delimiter or ","

    -- 2) Split on delimiter, then trim each piece and filter out any blank strings
    local list = mw.text.split(raw, delimiter)
    local items = {}
    for _, entry in ipairs(list) do
        local trimmed = mw.text.trim(entry)
        if trimmed ~= "" then
            table.insert(items, trimmed)
        end
    end

    -- 3) If no valid items, return empty string
    if #items == 0 then
        return ""
    end

    -- 4) Helper to convert a single “item” into a [[PageName]] link
    local function makeLink(item)
        return string.format("[[%s]]", item)
    end

    -- 5) If exactly one entry, return just the single link
    if #items == 1 then
        return makeLink(items[1])
    end

    -- 6) Otherwise, wrap each link in a <li> inside a <ul>
    local output = {}
    table.insert(output, '<ul style="margin-left: 1.5em;">')
    for _, entry in ipairs(items) do
        table.insert(output, string.format("  <li>%s</li>", makeLink(entry)))
    end
    table.insert(output, "</ul>")

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

return p