Jump to content

Module:SortedItemLinks

From Drifters Almanac

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

local p = {}

-- Return a sorted <ul> of wiki links from a comma-delimited list of item pages
function p.fromDelimited(frame)
    local raw = frame.args[1] or ""
    local delimiter = frame.args.delimiter or ","
    local items = mw.text.split(raw, delimiter)

    local entries = {}

    for _, entry in ipairs(items) do
        local pageName = mw.text.trim(entry)
        if pageName ~= "" then
            table.insert(entries, pageName)
        end
    end

    -- Sort alphabetically, case-insensitive
    table.sort(entries, function(a, b)
        return mw.ustring.lower(a) < mw.ustring.lower(b)
    end)

    local output = { '<ul style="margin-left: 1em;">' }

    for _, pageName in ipairs(entries) do
        table.insert(output, string.format('<li>[[%s]]</li>', pageName))
    end

    table.insert(output, '</ul>')
    return table.concat(output, "\n")
end

return p