Module:SpriteFile

From Modded Wiki
Revision as of 13:58, 3 October 2023 by minecraft>MarkusRost (Replace Module:Sprite)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Documentation for this module may be created at Module:SpriteFile/doc

local p = {}

function p.sprite( f )
	local args = f
	if f == mw.getCurrentFrame() then
		args = require( 'Module:ProcessArgs' ).merge( true )
	else
		f = mw.getCurrentFrame()
	end
	
	-- Default settings
	local default = {
		scale = 1,
		size = 16,
		align = 'text-top'
	}
	
	local id = mw.text.trim( tostring( args[1] or '' ) )
	if not args.keepcase then
		id = mw.ustring.lower( id ):gsub( '[%s%+]', '-' )
	end
	
	local link = ( args.link or '' )
	if mw.ustring.lower( link ) == 'none' then
		link = ''
	end
	
	local scale = args.scale or default.scale
	local width = ( args.width or args.size or default.size ) * scale
	local height = ( args.height or args.size or default.size ) * scale
	local size = width .. 'x' .. height .. 'px'
	
	local file = args.name .. ' ' .. id .. '.png'
	local sprite = mw.html.create( 'span' ):addClass( 'sprite-file' )
	sprite:node( '[[File:' .. file .. '|' .. size .. '|link=' .. link .. '|' .. ( args.title or '' ) .. '|class=pixel-image]]' )
	
	local styles = {}
	
	local align = args.align or default.align
	if align ~= default.align then
		styles[#styles + 1] = 'vertical-align:' .. align
	end
	styles[#styles + 1] = args.css
	
	sprite:cssText( table.concat( styles, ';' ) )
	
	local root
	local spriteText
	if args.text then
		if not args['wrap'] then
			root = mw.html.create( 'span' ):addClass( 'nowrap' )
		end
		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 ) .. ']'
			end
			
			-- Internal link
			local linkPrefix = args.linkprefix or ''
			spriteText = '[[' .. linkPrefix .. link .. '|' .. tostring( spriteText ) .. ']]'
		end
	end
	
	if not root then
		root = mw.html.create( '' )
	end
	root:node( sprite )
	if spriteText then
		root:node( spriteText )
	end
	
	return tostring( root )
end

function p.link( f )
	local args = f
	if f == mw.getCurrentFrame() then
		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
	local text
	if not args.notext then
		text = args.text or args[2] or link
	end
	
	args[1] = args.id or args[1]
	args.link = args.link or link
	args.text = text
	
	return p.sprite( args )
end

return p