Jump to content

Module:ListLinks: Difference between revisions

From Drifters Almanac
mNo edit summary
mNo edit summary
Tag: Reverted
Line 11: Line 11:
         item = mw.text.trim(item)
         item = mw.text.trim(item)
         if item ~= "" then
         if item ~= "" then
             local parts = mw.text.split(item, "::")
             local parts = mw.text.split(item, "::") -- << THIS is what parses custom label
             local target = mw.text.trim(parts[1])
             local target = mw.text.trim(parts[1])
             local label = parts[2] and mw.text.trim(parts[2]) or target
             local label = parts[2] and mw.text.trim(parts[2]) or target

Revision as of 03:37, 23 April 2025

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

local p = {}

function p.fromDelimited(frame)
    local raw = frame:getParent().args[1] or ""
    local delimiter = frame:getParent().args.delimiter or ","
    local list = mw.text.split(raw, delimiter)
    local output = {}

    table.insert(output, '<ul style="margin-left: 1em;">')
    for _, item in ipairs(list) do
        item = mw.text.trim(item)
        if item ~= "" then
            local parts = mw.text.split(item, "::")  -- << THIS is what parses custom label
            local target = mw.text.trim(parts[1])
            local label = parts[2] and mw.text.trim(parts[2]) or target
            table.insert(output, string.format('<li>[[%s|%s]]</li>', target, label))
        end
    end
    table.insert(output, '</ul>')

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

return p