Module:Sprite

local p = {} function p.base( f ) local args = f	if f == mw.getCurrentFrame then args = require( 'Module:ProcessArgs' ).merge( true ) end local name = args.name local image = args.image local scale = args.scale or 1 local sheetWidth = ( args.sheetsize or 256 ) * scale local size = ( args.size or 16 ) * scale local defaultPos = args.defaultpos or 1 local pos = math.abs( args.pos or defaultPos ) - 1 local link = args.link local align = args.align or 'text-top' local tiles = sheetWidth / size local class = args.class or '' local left = pos % tiles * size local top = math.floor( pos / tiles ) * size local text = args.text or '' local css = args.css local styles = {} local imgClasses = { Block = 1, Entity = 1, Item = 1, Schematic = 1 }	if imgClasses[name] then class = name:lower .. '-sprite ' .. class else table.insert( styles, 'background-image:' ) end if left > 0 or top > 0 then table.insert( styles, 'background-position:-' .. left .. 'px -' .. top .. 'px' ) end if scale ~= 1 then table.insert( styles, 'background-size:' .. sheetWidth .. 'px auto' ) end if size ~= 16 then table.insert( styles, 'height:' .. size .. 'px;width:' .. size .. 'px' ) end if align ~= 'text-top' then table.insert( styles, 'vertical-align:' .. align ) end if css then table.insert( styles, css ) end local sprite = table.concat( {		' '	}, ' ' ) sprite = sprite:gsub( '%s([">])', '%1' )	if text ~=  then		text = ' ' .. text .. ' '	end	if link then		if link:find( '//' ) then			-- External link			return '[' .. link .. ' ' .. sprite .. text .. ']'		else			-- Internal link			return  .. sprite .. text .. ''		end	else		return sprite .. text	end end

function p.sprite( f ) local args = f	if f == mw.getCurrentFrame then args = require( 'Module:ProcessArgs' ).merge( true ) end local category = '' if tonumber( args[1] ) then args.pos = args[1] else local ids = mw.loadData( 'Module:Sprite/' .. args.name ) local id = mw.text.trim( args[1] or '' ) local pos = ids[id] or ids[id:lower:gsub( '[%s%+]', '-' )] if not pos then category = '' end args.pos = pos end return p.base( args ) .. category 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 not args.id then link = args[1]:match( '^(.-)%+' ) or args[1] end local text = args[2] or link args[1] = args.id or args[1] args.link = link args.text = text return p.sprite( args ) end

function p.doc( f ) local args = f.args local idTable = mw.title.new( 'Module:Sprite/' .. args.name ):getContent idTable = idTable:gsub( '(\n%s*%-%-%s*.-%s*%-%-%s*\n)', '%1,' ):gsub( '^return {',  ):gsub( '}$',  ) local html = {} local ids = {} local posKeys = {} local section = '' for line in mw.text.gsplit( idTable, ',' ) do		line = mw.text.trim( line ) id = line:match( '^%[[\'"](.+)[\'"]%]' ) or line:match( '^%w+' ) or 		pos = line:match( '=%s*(%d+)%s*,?$' ) or 		section = line:match( '^%-%-%s*(.+)%s*%-%-$' ) or section		if id ~=  and pos ~=  then			if ids[pos] then				if type( ids[pos].id ) == 'table' then					table.insert( ids[pos].id, id )				else					ids[pos].id = { ids[pos].id, id }				end			else				ids[pos] = { id = id, section = section }				table.insert( posKeys, pos )			end		end	end	local list = {}	local listHead = ''	local listFoot = ''	local lastSection = 	for i, pos in ipairs( posKeys ) do		local id = ids[pos].id		local newSection = mw.text.trim( ids[pos].section )		if newSection ~= lastSection or i == 1 then			if newSection ~= lastSection then				if lastSection ~=  then					table.insert( list, listFoot )				end				table.insert( list, '\n===' .. newSection .. '===\n' )				lastSection = newSection			end			table.insert( list, listHead )		end		table.insert( list, ' ' )				else					table.insert( list, ' ' )				end			end		else			args[1] = id			table.insert( list, p.sprite( args ) .. '  ' )		end		table.insert( list, '   ' )		if i == #posKeys then			table.insert( list, listFoot )		end	end	return f:preprocess( '' ) .. '' .. table.concat( list ) .. ' ' end return p