Jump to content

Module:PadUtils

From PAD Wiki
Revision as of 04:36, 22 August 2025 by GlifterPad (talk | contribs)

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

-- Module:PadUtils
local p = {}

-- Split helper (ignores empty parts)
local function split(text, delim)
    local out = {}
    if not text or text == "" then return out end
    for part in mw.text.gsplit(text, delim, true) do
        part = mw.text.trim(part)
        if part ~= "" then table.insert(out, part) end
    end
    return out
end

-- Build a UL/OL list of <a> links that point to subpages of `base`.
-- Args:
--   base   : page prefix; default = current page (FULLPAGENAME)
--   floors : "A;B;C" or "Target|Label;Target2|Label2"
--   ol     : "1"/"true" to use <ol>; otherwise <ul>
--   before : optional text prepended to each label (display only)
--   after  : optional text appended to each label (display only)
function p.dungeonFloors(frame)
    local args    = frame.args
    local base    = args.base and mw.text.trim(args.base) or mw.title.getCurrentTitle().fullText
    local floors  = split(args.floors or "", ";")
    local listTag = (args.ol == "1" or args.ol == "true") and "ol" or "ul"
    local before  = args.before or ""
    local after   = args.after or ""

    if #floors == 0 then
        return ""
    end

    local items = {}
    for _, item in ipairs(floors) do
        -- Support "Target|Label" per item; default label = target
        local target, label = item:match("^(.-)|(.*)$")
        if not target then
            target = item
            label  = item
        end
        target = mw.text.trim(target)
        label  = mw.text.trim(label)

        -- Build full subpage title and URL
        local titleText = base .. "/" .. target
        local titleObj  = mw.title.new(titleText)
        local url = titleObj and titleObj:localUrl() or ("/wiki/" .. mw.uri.encode(titleText))

        -- Escape label for HTML; add optional before/after decorations (display only)
        local linkText = before .. label .. after
        table.insert(items, '<li><a href="' .. url .. '">' .. mw.text.encode(linkText) .. '</a></li>')
    end

    return '<' .. listTag .. '>' .. table.concat(items) .. '</' .. listTag .. '>'
end

return p