Module:MonsterPreview: Difference between revisions
Add MonsterPreview module for webm/png preview (via create-page on MediaWiki MCP Server) |
Add hidden PNG fallback for PageImages when displaying webm videos (via update-page on MediaWiki MCP Server) |
||
| (3 intermediate revisions by the same user not shown) | |||
| 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 then return '' end | if not name and (not file or file == '') then return '' end | ||
-- Custom file specified | -- Custom file specified (must be non-empty) | ||
if 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 for pageimage | |||
local classAttr = '' | |||
if class ~= '' then | |||
classAttr = '|class=' .. class | |||
end | end | ||
| Line 18: | 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>', | '<span class="monster-preview">[[File:%s.webm|%spx|loop|autoplay|muted]]</span>%s', | ||
name, size | name, size, hiddenPng | ||
) | ) | ||
end | end | ||
end | end | ||
-- Fall back to png | -- Fall back to png | ||
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.png|%spx|link=|alt=%s%s]]</span>', | ||
name, size, name, classAttr | |||
) | ) | ||
end | end | ||
function p. | function p.renderFile(file, size, alt, class) | ||
local classAttr = '' | |||
if class and class ~= '' then | |||
classAttr = '|class=' .. class | |||
end | 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 | ||
-- 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>', | '<span class="monster-preview">[[File:%s|%spx|loop|autoplay|muted]]</span>%s', | ||
file, size | file, size, hiddenPng | ||
) | ) | ||
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 | ||
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