Jump to content

Module:MonsterPreview: Difference between revisions

From Apogea Wiki
Dane (talk | contribs)
Fix empty string check for file parameter - empty strings are truthy in Lua (via update-page on MediaWiki MCP Server)
Dane (talk | contribs)
Add class parameter support for pageimage (via update-page on MediaWiki MCP Server)
Line 6: Line 6:
     local mode = frame.args.mode or 'auto'  -- auto, image, video
     local mode = frame.args.mode or 'auto'  -- auto, image, video
     local file = frame.args.file  -- custom file override
     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
     if not name and (not file or file == '') then return '' end
Line 11: Line 12:
     -- Custom file specified (must be non-empty)
     -- Custom file specified (must be non-empty)
     if file and file ~= '' then
     if file and file ~= '' then
         return p.renderFile(file, size, name or file)
         return p.renderFile(file, size, name or file, class)
    end
   
    -- Build class attribute
    local classAttr = ''
    if class ~= '' then
        classAttr = '|class=' .. class
     end
     end
      
      
Line 19: Line 26:
         if webm and webm.exists then
         if webm and webm.exists then
             return string.format(
             return string.format(
                 '<span class="monster-preview">[[File:%s.webm|%spx|loop|autoplay|muted]]</span>',
                 '<span class="monster-preview">[[File:%s.webm|%spx|loop|autoplay|muted%s]]</span>',
                 name, size
                 name, size, classAttr
             )
             )
         end
         end
Line 27: Line 34:
     -- Fall back to png
     -- Fall back to png
     return string.format(
     return string.format(
         '<span class="monster-preview pixel-sprite">[[File:%s.png|%spx|link=|alt=%s]]</span>',
         '<span class="monster-preview pixel-sprite">[[File:%s.png|%spx|link=|alt=%s%s]]</span>',
         name, size, name
         name, size, name, classAttr
     )
     )
end
end


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

Revision as of 21:52, 30 January 2026

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