Jump to content

Module:PadUtils: Difference between revisions

From PAD Wiki
No edit summary
No edit summary
Line 1: Line 1:
-- Module:PadUtils
local p = {}
local p = {}


-- split "A;B;C" safely (ignores empty pieces)
local function split(text, delim)
local function split(text, delim)
     local out = {}
     local out = {}
Line 11: Line 13:
end
end


-- Accept any of:
--  "Floor Name"
--  "Floor_Target|Floor Label"
--  "[[Dungeon/Floor_Target|Floor Label]]"
--  "[[Floor_Target|Floor Label]]"
local function parseItem(item)
local function parseItem(item)
    -- Strip [[...]] if present
     local inner = item:match("^%[%[(.*)%]%]$")
     local inner = item:match("^%[%[(.*)%]%]$")
     if inner then item = inner end
     if inner then item = inner end
    -- Split on pipe "|"
     local t, l = item:match("^(.-)|(.*)$")
     local t, l = item:match("^(.-)|(.*)$")
     local target = mw.text.trim(t or item)
     local target = mw.text.trim(t or item)
Line 20: Line 29:
end
end


-- Always normalize to "base/floor" for the link target.
-- If target already starts with "base/", strip that prefix first.
local function normalizeTarget(base, target)
local function normalizeTarget(base, target)
     target = mw.text.trim(target)
     target = mw.text.trim(target)
Line 33: Line 44:
     local base    = args.base and mw.text.trim(args.base) or mw.title.getCurrentTitle().fullText
     local base    = args.base and mw.text.trim(args.base) or mw.title.getCurrentTitle().fullText
     local floors  = split(args.floors or "", ";")
     local floors  = split(args.floors or "", ";")
     local listTag = (args.ol == "1" or args.ol == "true") and "ol" or "ul"
     local ordered = (args.ol == "1" or args.ol == "true")
     local before  = args.before or ""
     local before  = args.before or ""
     local after  = args.after or ""
     local after  = args.after or ""
Line 39: Line 50:
     if #floors == 0 then return "" end
     if #floors == 0 then return "" end


     -- Use mw.html so MediaWiki treats the output as safe HTML
     -- Build a wikitext list with internal link syntax, then parse it.
     local list = mw.html.create(listTag)
     local lines = {}
 
     for _, raw in ipairs(floors) do
     for _, raw in ipairs(floors) do
         local target, label = parseItem(raw)
         local target, label = parseItem(raw)
         local titleText = normalizeTarget(base, target)
         local titleText = normalizeTarget(base, target)
        local titleObj  = mw.title.new(titleText)
        local url = titleObj and titleObj:fullUrl() or ("/wiki/" .. mw.uri.encode(titleText))


         list:tag("li")
         -- Build a proper Title, use its canonical text if possible
            :tag("a")
        local titleObj = mw.title.new(titleText)
                :attr("href", url)
        local linkTarget = titleObj and titleObj:prefixedText() or titleText
                :wikitext(before .. label .. after)
 
            :done()
        -- Construct one bullet line of wikitext
         :done()
        local bullet = ordered and "#" or "*"
        table.insert(lines,
            string.format("%s [[%s|%s%s%s]]", bullet, linkTarget, before, label, after)
         )
     end
     end


     return tostring(list) -- ensures proper HTML rendering
    -- Force the parser to render the wikitext into HTML
     return frame:preprocess(table.concat(lines, "\n"))
end
end


return p
return p

Revision as of 04:49, 22 August 2025

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

-- Module:PadUtils
local p = {}

-- split "A;B;C" safely (ignores empty pieces)
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

-- Accept any of:
--   "Floor Name"
--   "Floor_Target|Floor Label"
--   "[[Dungeon/Floor_Target|Floor Label]]"
--   "[[Floor_Target|Floor Label]]"
local function parseItem(item)
    -- Strip [[...]] if present
    local inner = item:match("^%[%[(.*)%]%]$")
    if inner then item = inner end
    -- Split on pipe "|"
    local t, l = item:match("^(.-)|(.*)$")
    local target = mw.text.trim(t or item)
    local label  = mw.text.trim(l or item)
    return target, label
end

-- Always normalize to "base/floor" for the link target.
-- If target already starts with "base/", strip that prefix first.
local function normalizeTarget(base, target)
    target = mw.text.trim(target)
    local withSlash = base .. "/"
    if target:sub(1, #withSlash) == withSlash then
        target = target:sub(#withSlash + 1)
    end
    return base .. "/" .. target
end

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 ordered = (args.ol == "1" or args.ol == "true")
    local before  = args.before or ""
    local after   = args.after or ""

    if #floors == 0 then return "" end

    -- Build a wikitext list with internal link syntax, then parse it.
    local lines = {}
    for _, raw in ipairs(floors) do
        local target, label = parseItem(raw)
        local titleText = normalizeTarget(base, target)

        -- Build a proper Title, use its canonical text if possible
        local titleObj = mw.title.new(titleText)
        local linkTarget = titleObj and titleObj:prefixedText() or titleText

        -- Construct one bullet line of wikitext
        local bullet = ordered and "#" or "*"
        table.insert(lines,
            string.format("%s [[%s|%s%s%s]]", bullet, linkTarget, before, label, after)
        )
    end

    -- Force the parser to render the wikitext into HTML
    return frame:preprocess(table.concat(lines, "\n"))
end

return p