Module:SpriteFile: Difference between revisions
No edit summary |
No edit summary |
||
| Line 1: | Line 1: | ||
local p = {} | local p = {} | ||
function p.sprite( f ) | function p.sprite( f ) | ||
local args = f | local args = f | ||
if f == mw.getCurrentFrame() then | if f == mw.getCurrentFrame() then | ||
args = require( | args = require( 'Module:ProcessArgs' ).merge( true ) | ||
else | else | ||
f = mw.getCurrentFrame() | f = mw.getCurrentFrame() | ||
| Line 19: | Line 13: | ||
scale = 1, | scale = 1, | ||
size = 16, | size = 16, | ||
align = | align = 'text-top' | ||
} | } | ||
local id = mw.text.trim(tostring(args[1] or | local id = mw.text.trim( tostring( args[1] or '' ) ) | ||
if not args.keepcase then | |||
id = mw.ustring.lower( id ):gsub( '[%s%+]', '-' ) | |||
end | end | ||
local link = ( args.link or | local link = ( args.link or '' ) | ||
if mw.ustring.lower( link ) == 'none' then | |||
link = '' | |||
elseif link ~= '' then | |||
link = | local linkPrefix = ( not link:find( '//' ) and args.linkprefix ) or '' | ||
link = linkPrefix .. link | |||
end | end | ||
local scale = args.scale or default.scale | local scale = args.scale or default.scale | ||
local height = ( args.height or args.size or default.size ) * scale | local height = ( args.height or args.size or default.size ) * scale | ||
local width = ( args.width or args.size or default.size ) * scale | local width = ( args.width or args.size or default.size ) * scale | ||
local size = width .. | local size = width .. 'x' .. height .. 'px' | ||
local styles = {} | local styles = {} | ||
if height ~= default.size then | if height ~= default.size then | ||
styles[#styles + 1] = | styles[#styles + 1] = 'height:' .. height .. 'px' | ||
end | end | ||
if width ~= default.size then | if width ~= default.size then | ||
styles[#styles + 1] = | styles[#styles + 1] = 'width:' .. width .. 'px' | ||
end | |||
local name = args.name or '' | |||
if name == 'InvSprite' then | |||
name = 'Invicon' | |||
end | |||
local file = name .. ' ' .. id .. '.png' | |||
local altText = '' | |||
if link == '' then | |||
altText = file .. ': Sprite image for ' .. id .. ' in Minecraft' | |||
end | |||
if id == '' then | |||
file = 'Grid Unknown.png' | |||
altText = 'Unknown sprite image' | |||
end | end | ||
local sprite = mw.html.create( 'span' ):addClass( 'sprite-file' ) | |||
local img = '[[File:' .. file .. '|' .. size .. '|link=' .. link .. '|alt=' .. altText .. '|class=pixel-image|' .. ( args.title or '' ) .. ']]' | |||
sprite:node( img ) | |||
local align = args.align or default.align | local align = args.align or default.align | ||
if align ~= default.align then styles[#styles+1] = | if align ~= default.align then | ||
styles[#styles + 1] = '--vertical-align:' .. align | |||
end | |||
styles[#styles + 1] = args.css | |||
sprite:cssText( table.concat( styles, ';' ) ) | |||
if | local root | ||
spriteText = mw.html.create( | local spriteText | ||
if args.title then spriteText:attr( | if args.text then | ||
if not args['wrap'] then | |||
if link ~= | root = mw.html.create( 'span' ):addClass( 'nowrap' ) | ||
if | end | ||
spriteText = | spriteText = mw.html.create( 'span' ):addClass( 'sprite-text' ):wikitext( args.text ) | ||
if args.title then | |||
spriteText:attr( 'title', args.title ) | |||
end | |||
if link ~= '' then | |||
-- External link | |||
if link:find( '//' ) then | |||
spriteText = '[' .. link .. ' ' .. tostring( spriteText ) .. ']' | |||
else | else | ||
spriteText = | spriteText = '[[' .. link .. '|' .. tostring( spriteText ) .. ']]' | ||
end | end | ||
end | end | ||
end | end | ||
root:node(sprite) | if not root then | ||
if spriteText then root:node(spriteText) end | root = mw.html.create( '' ) | ||
end | |||
return tostring(root) | root:node( sprite ) | ||
if spriteText then | |||
root:node( spriteText ) | |||
end | |||
return tostring( root ) | |||
end | end | ||
function p.link(f) | function p.link( f ) | ||
local args = f | local args = f | ||
if f == mw.getCurrentFrame() then | if f == mw.getCurrentFrame() then | ||
args = require( | args = require( 'Module:ProcessArgs' ).merge( true ) | ||
end | |||
local link = args[1] | |||
if args[1] and not args.id then | |||
link = args[1]:match( '^(.-)%+' ) or args[1] | |||
end | end | ||
local text | |||
local | |||
if not args.notext then | if not args.notext then | ||
text = args.text or args[2] or link | |||
end | end | ||
args[1] | args[1] = args.id or args[1] | ||
args.link = args.link or | args.link = args.link or link | ||
return p.sprite(args) | args.text = text | ||
return p.sprite( args ) | |||
end | end | ||
return p | return p | ||