Module:LeftAlignedList

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

Module:LeftAlignedList

Converts a comma-separated string into a wikitext bullet list. Each comma-delimited value becomes its own `*` list item, with surrounding whitespace trimmed. Additional optional functions are also available to support the use cases of other templates and modules.

Usage

Core functionality

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

Output:

* Item 1
* Item 2
* Item 3

Convert each item into wikilink

{{#invoke:LeftAlignedList|main|Item 1, Item 2, Item 3|link=true}}

Output:

* [[Item 1]]
* [[Item 2]]
* [[Item 3]]

Prepend a value to each item

{{#invoke:LeftAlignedList|main|Item 1, Item 2, Item 3|prepend=prefix/}}

Output:

* Prefix/Item 1
* Prefix/Item 2
* Prefix/Item 3

When combined with link=true, the prepended value is included in the link target, but the item is displayed without it:

{{#invoke:LeftAlignedList|main|Item 1, Item 2, Item 3|link=true|prepend=prefix/}}

Output:

* [[Prefix/Item 1|Item 1]]
* [[Prefix/Item 2|Item 2]]
* [[Prefix/Item 3|Item 3]]

Parameters

main

1
string
A comma-separated string containing the items to convert into a bullet list.
link (optional)
boolean
If specified with a truthy value, each item will be wrapped in double square brackets, rendering it as a wikilink.
Valid values:
true
yes
1
prepend (optional)
string
If specified, this value is prepended to each item. When link=true, the prepended value is included in the link target, but not the display label.

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 = {}

local function isTruthy(s)
    s = mw.ustring.lower(mw.text.trim(s or ""))
    return s == "true" or s == "yes" or s == "1"
end

function p.main(frame)
    local content = frame.args[1]
    local link = frame.args["link"] or ""
    local prepend = mw.text.trim(frame.args["prepend"] or "")

    if not content or content == "" then return "" end

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

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

        if item ~= "" then
            if isTruthy(link) then

                -- form wikilink but do not display the prepend value
                if prepend ~= "" then
                    item = "[[" .. prepend .. item .. "|" .. item .. "]]"

                -- convert item to link
                else
                    item = "[[" .. item .. "]]"
                end

            -- prepend value
            elseif prepend ~= "" then item = prepend .. item end

            table.insert(items, "* " .. item)
        end
    end

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

return p