Jump to content

Module:MonsterPreview

From Apogea Wiki
Revision as of 21:58, 30 January 2026 by Dane (talk | contribs) (Add hidden PNG fallback for PageImages when displaying webm videos (via update-page on MediaWiki MCP Server))
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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 for pageimage
    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
            -- When showing webm, also include a hidden png for PageImages to detect
            local hiddenPng = ''
            if class ~= '' then
                local png = mw.title.new('File:' .. name .. '.png')
                if png and png.exists then
                    hiddenPng = string.format(
                        '<span class="pageimage-fallback" style="display:none;">[[File:%s.png|1px|class=%s]]</span>',
                        name, class
                    )
                end
            end
            
            return string.format(
                '<span class="monster-preview">[[File:%s.webm|%spx|loop|autoplay|muted]]</span>%s',
                name, size, hiddenPng
            )
        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
        -- When showing video, also include hidden png fallback for PageImages
        local hiddenPng = ''
        if class and class ~= '' then
            -- Try to find a png version
            local baseName = file:match('(.+)%.[^%.]+$') or file
            local png = mw.title.new('File:' .. baseName .. '.png')
            if png and png.exists then
                hiddenPng = string.format(
                    '<span class="pageimage-fallback" style="display:none;">[[File:%s.png|1px|class=%s]]</span>',
                    baseName, class
                )
            end
        end
        
        return string.format(
            '<span class="monster-preview">[[File:%s|%spx|loop|autoplay|muted]]</span>%s',
            file, size, hiddenPng
        )
    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