Module:Sprite
Documentation for this module may be created at Module:Sprite/doc
local p = {}
function p.getHighestStack(frame)
local name = frame.args[1]
if not name then return '' end
for i = 20, 1, -1 do
local file = mw.title.new('File:' .. name .. i .. '.png')
if file and file.exists then
return tostring(i)
end
end
return ''
end
function p.render(frame)
local name = frame.args[1]
local sizeArg = frame.args[2] or frame.args.size or '32'
local scale = frame.args.scale
local class = frame.args.class or ''
if not name then return '' end
local suffix = p.getHighestStack(frame)
local filename = name .. suffix .. '.png'
-- Determine final size
local size
-- Check if sizeArg is a multiplier (starts with 'x')
local multiplier = sizeArg:match('^x(%d+)$')
if multiplier then
multiplier = tonumber(multiplier)
elseif scale then
multiplier = math.floor(tonumber(scale) or 1)
end
-- Clamp multiplier to reasonable integer range
if multiplier then
multiplier = math.max(1, math.min(multiplier, 10))
-- Get original file dimensions
local file = mw.title.new('File:' .. filename)
if file and file.file and file.file.width then
size = file.file.width * multiplier
else
-- Fallback: assume 16px base size
size = 16 * multiplier
end
else
size = sizeArg
end
local classAttr = ''
if class ~= '' then
classAttr = '|class=' .. class
end
return string.format(
'<span class="pixel-sprite">[[File:%s|%spx|link=|alt=%s%s]]</span>',
filename, size, name, classAttr
)
end
return p