Module:GetNPCImage

From MB Wiki
Revision as of 12:27, 26 February 2026 by Ais (talk | contribs) (Created page with "local p = {} function p.getImage(frame) local pageName = frame.args[1] or frame:getParent().args[1] or mw.title.getCurrentTitle().text local title = mw.title.new(pageName) if not title or not title.exists then return "" -- Page doesn't exist, return empty end -- Get the page content local content = title:getContent() if not content then return "" end -- Look for the image parameter in InfoboxNPC -- P...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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

local p = {}

function p.getImage(frame)
    local pageName = frame.args[1] or frame:getParent().args[1] or mw.title.getCurrentTitle().text
    local title = mw.title.new(pageName)
    
    if not title or not title.exists then
        return "" -- Page doesn't exist, return empty
    end
    
    -- Get the page content
    local content = title:getContent()
    if not content then
        return ""
    end
    
    -- Look for the image parameter in InfoboxNPC
    -- Pattern matches: | image = filename.png or |image = filename.png
    local pattern = "|%s*image%s*=%s*([^%c|]+)"
    local imageName = string.match(content, pattern)
    
    if imageName then
        -- Clean up the filename (remove whitespace, brackets, etc.)
        imageName = mw.text.trim(imageName)
        return imageName
    end
    
    -- Fallback: try to find any file link in the infobox
    local fallbackPattern = "%[%[File:([^%]]+).?%]%]"
    local fallbackName = string.match(content, fallbackPattern)
    
    if fallbackName then
        return mw.text.trim(fallbackName)
    end
    
    -- If all else fails, return empty string (DPL will skip this entry)
    return ""
end

return p