Jump to content

Module:SmartMobLinks: Difference between revisions

From Drifters Almanac
No edit summary
No edit summary
Tag: Reverted
Line 1: Line 1:
local p = {}
local p = {}


-- Returns a table of rows { target, label, exists, form } separated by :::
-- Return a fully rendered <ul> of mob links or form links
function p.parseMobList(frame)
function p.linkOrForm(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 result = {}
     local output = {}
 
    table.insert(output, '<ul style="margin-left: 1em;">')


     for _, fullPath in ipairs(items) do
     for _, fullPath in ipairs(items) do
Line 33: Line 35:
             end
             end


             local exists = false
             local pageTitle = mw.title.new(fullPath)
            local title = mw.title.new(fullPath)
 
             if title and title.exists then
             if pageTitle and pageTitle.exists then
                 exists = true
                 table.insert(output, string.format('<li>[[%s|%s]]</li>', fullPath, label))
            end
            else
                local form = prefix == "Named Mob" and "NamedMob" or "Mob"
                local preload = "Template:" .. form .. "/preload"
                local editintro = "Contributing/Creating_" .. form


            local form = (prefix == "Named Mob") and "NamedMob" or "Mob"
                local redlink = string.format('{{#formredlink:form=%s|link text=%s|target=%s|preload=%s|query string=editintro=%s}}',
                    form, label, fullPath, preload, editintro)


            table.insert(result, string.format("%s:::%s:::%s:::%s", fullPath, label, tostring(exists), form))
                table.insert(output, '<li>' .. redlink .. '</li>')
            end
         end
         end
     end
     end


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


return p
return p

Revision as of 23:20, 12 May 2025

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

local p = {}

-- Return a fully rendered <ul> of mob links or form links
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

            if not prefix or not rest then
                prefix = nil
                rest = fullPath
            end

            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
            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
                local form = prefix == "Named Mob" and "NamedMob" or "Mob"
                local preload = "Template:" .. form .. "/preload"
                local editintro = "Contributing/Creating_" .. form

                local redlink = string.format('{{#formredlink:form=%s|link text=%s|target=%s|preload=%s|query string=editintro=%s}}',
                    form, label, fullPath, preload, editintro)

                table.insert(output, '<li>' .. redlink .. '</li>')
            end
        end
    end

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

return p