Jump to content

Module:MonsterPreview

From Apogea Wiki
Revision as of 21:52, 30 January 2026 by Dane (talk | contribs) (Add class parameter support for pageimage (via update-page on MediaWiki MCP Server))

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

local p = {}

function p.render(frame)
    local name = frame.args[1]
    local size = frame.args[2] or frame.args.size or '128'
    local mode = frame.args.mode or 'auto'  -- auto, image, video
    local file = frame.args.file  -- custom file override
    local class = frame.args.class or ''
    
    if not name and (not file or file == '') then return '' end
    
    -- Custom file specified (must be non-empty)
    if file and file ~= '' then
        return p.renderFile(file, size, name or file, class)
    end
    
    -- Build class attribute
    local classAttr = ''
    if class ~= '' then
        classAttr = '|class=' .. class
    end
    
    -- Check for webm first (unless image-only mode)
    if mode ~= 'image' then
        local webm = mw.title.new('File:' .. name .. '.webm')
        if webm and webm.exists then
            return string.format(
                '<span class="monster-preview">[[File:%s.webm|%spx|loop|autoplay|muted%s]]</span>',
                name, size, classAttr
            )
        end
    end
    
    -- Fall back to png
    return string.format(
        '<span class="monster-preview pixel-sprite">[[File:%s.png|%spx|link=|alt=%s%s]]</span>',
        name, size, name, classAttr
    )
end

function p.renderFile(file, size, alt, class)
    local classAttr = ''
    if class and class ~= '' then
        classAttr = '|class=' .. class
    end
    
    local ext = file:match('%.([^%.]+)$') or ''
    if ext == 'webm' or ext == 'mp4' or ext == 'gif' then
        return string.format(
            '<span class="monster-preview">[[File:%s|%spx|loop|autoplay|muted%s]]</span>',
            file, size, classAttr
        )
    else
        return string.format(
            '<span class="monster-preview pixel-sprite">[[File:%s|%spx|link=|alt=%s%s]]</span>',
            file, size, alt, classAttr
        )
    end
end

return p