Jump to content

Module:SmartMobLinks

From Drifters Almanac
Revision as of 21:59, 12 May 2025 by Mildew (talk | contribs) (Created page with "local p = {} function p.linkOrForm(frame) local raw = frame.args[1] or "" local delimiter = frame.args.delimiter or "," local items = mw.text.split(raw, delimiter) local output = {} table.insert(output, '<ul style="margin-left: 1em;">') for _, fullPath in ipairs(items) do fullPath = mw.text.trim(fullPath) if fullPath ~= "" then local prefix, rest = string.match(fullPath, "^(.-)/(.+)$") local label = full...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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

local p = {}

function p.linkOrForm(frame)
    local raw = frame.args[1] or ""
    local delimiter = frame.args.delimiter or ","
    local items = mw.text.split(raw, delimiter)
    local output = {}

    table.insert(output, '<ul style="margin-left: 1em;">')

    for _, fullPath in ipairs(items) do
        fullPath = mw.text.trim(fullPath)

        if fullPath ~= "" then
            local prefix, rest = string.match(fullPath, "^(.-)/(.+)$")
            local label = fullPath

            -- Fallback if there's no slash (bad format)
            if not prefix or not rest then
                prefix = nil
                rest = fullPath
            end

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

            local pageTitle = mw.title.new(fullPath)

            if pageTitle and pageTitle.exists then
                table.insert(output, string.format('<li>[[%s|%s]]</li>', fullPath, label))
            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

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

return p