Module:CSVToBulletList

From Dune: Awakening Community Wiki
Jump to navigation Jump to search

Module:CSVToBulletList

Converts a comma-separated string into a wikitext bullet list. Each comma-delimited value becomes its own `*` list item, with surrounding whitespace trimmed.

Usage

{{#invoke:CSVToBulletList|main|Item 1, Item 2, Item 3}}

Output:

* Item 1
* Item 2
* Item 3

Parameters

main

1
A comma-separated string containing the items to convert into a bullet list.

Notes

  • Empty items are ignored (e.g. `A,,B` becomes `A` and `B`).
  • If the input contains no commas, it will still be returned as a bullet list with a single `*` item.

local p = {}

function p.main(frame)
    local input = frame.args[1]

    if not input or input == '' then
        return ''
    end

    local items = {}
    
    -- split string by commas
    for item in mw.text.gsplit(input, ',') do

        -- trim whitespace
        item = mw.text.trim(item)

        if item ~= '' then
            table.insert(items, '* ' .. item)
        end
    end

    -- join items back into bullet list
    return table.concat(items, '\n')
end

return p