Jump to content

Module:ZoneMobLinks

From Drifters Almanac

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

local p = {}

-- Return a <ul> of wiki links from a comma-delimited list of mob paths.
-- Accepts:
--   Mob/Mob Name
--   Mob/Mob Name/Zone Name
--   Mob/Mob Name/Zone Name/Location within Zone (or deeper)
-- Display label:
--   Mob Name (Location)   -- ignores Zone Name entirely
--   Mob Name              -- if no location present
function p.fromDelimited(frame)
    local raw = frame.args[1] or ""
    local delimiter = frame.args.delimiter or ","
    local items = mw.text.split(raw, delimiter)
    local output = { '<ul style="margin-left: 1em;">' }

    for _, entry in ipairs(items) do
        local fullPath = mw.text.trim(entry)
        if fullPath ~= "" then
            local parts = mw.text.split(fullPath, "/")

            -- Optional prefix: "Mob" or "Named Mob"
            local prefix = parts[1]
            local hasPrefix = (prefix == "Mob" or prefix == "Named Mob")
            local baseIndex = hasPrefix and 2 or 1  -- mob name index

            local mobName = parts[baseIndex] or fullPath

            -- Structure after mob name:
            -- parts[baseIndex+1] = zone (ignored in label)
            -- parts[baseIndex+2...] = location (displayed)
            local location = ""
            local locStart = baseIndex + 2
            if #parts >= locStart then
                local locParts = {}
                for i = locStart, #parts do
                    if parts[i] and parts[i] ~= "" then
                        table.insert(locParts, parts[i])
                    end
                end
                location = table.concat(locParts, "/")
            end

            local label = mobName
            if location ~= "" then
                label = string.format("%s (%s)", mobName, location)
            end

            -- Star marker for named mobs, appended after the link
            local marker = ""
            if prefix == "Named Mob" then
                marker = " ★"
            end

            table.insert(output, string.format('<li>[[%s|%s]]%s</li>', fullPath, label, marker))
        end
    end

    table.insert(output, '</ul>')
    return table.concat(output, "\n")
end

return p