Jump to content

Module:SmartMobLinks: Difference between revisions

From Drifters Almanac
m Protected "Module:SmartMobLinks" ([Edit=Allow only administrators] (indefinite) [Move=Allow only administrators] (indefinite))
No edit summary
 
(14 intermediate revisions by the same user not shown)
Line 1: Line 1:
local p = {}
local p = {}


function p.linkOrForm(frame)
-- Always return a <ul> of wiki links from a comma-delimited list of mob paths
function p.fromDelimited(frame)
     local raw = frame.args[1] or ""
     local raw = frame.args[1] or ""
     local delimiter = frame.args.delimiter or ","
     local delimiter = frame.args.delimiter or ","
     local items = mw.text.split(raw, delimiter)
     local items = mw.text.split(raw, delimiter)
     local output = {}
     local output = { '<ul style="margin-left: 1em;">' }
 
    table.insert(output, '<ul style="margin-left: 1em;">')
 
    for _, fullPath in ipairs(items) do
        fullPath = mw.text.trim(fullPath)


    for _, entry in ipairs(items) do
        local fullPath = mw.text.trim(entry)
         if fullPath ~= "" then
         if fullPath ~= "" then
            -- Parse prefix (Mob or Named Mob) and path remainder
             local prefix, rest = string.match(fullPath, "^(.-)/(.+)$")
             local prefix, rest = string.match(fullPath, "^(.-)/(.+)$")
             local label = fullPath
             if not rest then
 
            -- Fallback if there's no slash (bad format)
            if not prefix or not rest then
                 prefix = nil
                 prefix = nil
                 rest = fullPath
                 rest = fullPath
             end
             end


             -- Handle label formatting based on prefix
             -- Separate name and zone if present
             if prefix == "Mob" then
             local base, zone = string.match(rest, "^(.-)/(.+)$")
                local base, zone = string.match(rest, "^(.-)/(.+)$")
            local label = rest
                if base and zone then
            if base and zone then
                    label = base .. " (" .. zone .. ")"
                label = base .. " (" .. zone .. ")"
                else
                    label = rest
                end
            elseif prefix == "Named Mob" then
                label = rest
            else
                label = rest -- fallback if prefix is unknown
             end
             end


             local pageTitle = mw.title.new(fullPath)
            -- Star marker for named mobs, appended after the link
             local marker = ""
            if prefix == "Named Mob" then
                marker = " ★"
            end


             if pageTitle and pageTitle.exists then
             -- Generate list item with default bullet and optional marker
                table.insert(output, string.format('<li>[[%s|%s]]</li>', fullPath, label))
            table.insert(output, string.format('<li>[[%s|%s]]%s</li>', fullPath, label, marker))
            else
                -- Determine appropriate form and preload
                local form = prefix == "Named Mob" and "NamedMob" or "Mob"
                local preload = "Template:" .. form .. "/preload"
                local editintro = "Contributing/Creating_" .. form
 
                table.insert(output,
                    string.format('<li>{{#formredlink:form=%s|link text=%s|target=%s|preload=%s|query string=editintro=%s}}</li>',
                        form, label, fullPath, preload, editintro)
                )
            end
         end
         end
     end
     end

Latest revision as of 07:39, 28 May 2025

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

local p = {}

-- Always return a <ul> of wiki links from a comma-delimited list of mob paths
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
            -- Parse prefix (Mob or Named Mob) and path remainder
            local prefix, rest = string.match(fullPath, "^(.-)/(.+)$")
            if not rest then
                prefix = nil
                rest = fullPath
            end

            -- Separate name and zone if present
            local base, zone = string.match(rest, "^(.-)/(.+)$")
            local label = rest
            if base and zone then
                label = base .. " (" .. zone .. ")"
            end

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

            -- Generate list item with default bullet and optional marker
            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