Module:MonsterPreview: Difference between revisions
Add class parameter support for pageimage (via update-page on MediaWiki MCP Server) |
Add hidden PNG fallback for PageImages when displaying webm videos (via update-page on MediaWiki MCP Server) |
||
| Line 15: | Line 15: | ||
end | end | ||
-- Build class attribute | -- Build class attribute for pageimage | ||
local classAttr = '' | local classAttr = '' | ||
if class ~= '' then | if class ~= '' then | ||
| Line 25: | Line 25: | ||
local webm = mw.title.new('File:' .. name .. '.webm') | local webm = mw.title.new('File:' .. name .. '.webm') | ||
if webm and webm.exists then | 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( | return string.format( | ||
'<span class="monster-preview">[[File:%s.webm|%spx|loop|autoplay|muted | '<span class="monster-preview">[[File:%s.webm|%spx|loop|autoplay|muted]]</span>%s', | ||
name, size, | name, size, hiddenPng | ||
) | ) | ||
end | end | ||
| Line 47: | Line 59: | ||
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 | ||
-- 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( | return string.format( | ||
'<span class="monster-preview">[[File:%s|%spx|loop|autoplay|muted | '<span class="monster-preview">[[File:%s|%spx|loop|autoplay|muted]]</span>%s', | ||
file, size, | file, size, hiddenPng | ||
) | ) | ||
else | else | ||
Latest revision as of 21:58, 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 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