Module:Sprite: Difference between revisions
Add Sprite module for stack-aware sprite rendering (via create-page on MediaWiki MCP Server) |
Limit scale to integers 1-10 (via update-page on MediaWiki MCP Server) |
||
| (2 intermediate revisions by the same user not shown) | |||
| Line 16: | Line 16: | ||
function p.render(frame) | function p.render(frame) | ||
local name = frame.args[1] | local name = frame.args[1] | ||
local | 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 | if not name then return '' end | ||
local suffix = p.getHighestStack(frame) | local suffix = p.getHighestStack(frame) | ||
local filename = name .. suffix .. '.png' | 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( | return string.format( | ||
'<span class="pixel-sprite">[[File:%s|%spx|link=|alt=%s]]</span>', | '<span class="pixel-sprite">[[File:%s|%spx|link=|alt=%s%s]]</span>', | ||
filename, size, name | filename, size, name, classAttr | ||
) | ) | ||
end | end | ||
return p | return p | ||
Latest revision as of 00:52, 28 January 2026
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