Module:KnightBot4/SpriteSub

local p = {}

function p.sub( f ) local args = f	if mw.getCurrentFrame == f then args = f:getParent.args end local name = args.name -- Find the name of the sprite sheet local module, sprite if name:find( 'Sprite' ) then module = name:gsub( '^(.-)Sprite.*$', '%1' ) sprite = true else module = name:gsub( '^(.-)Link.*$', '%1' ) end -- if the sprite template does not have a sprite module with the standard name, skip it local ids = mw.loadData( 'Module:' .. module .. 'Sprite' .. '/IDs' ).ids or {}

-- Inverting the table into a "number = name" format local names = {} for id, data in pairs( ids ) do		if not data.deprecated then local num = data.pos if not names[num] then names[num] = {} end table.insert( names[num], id ) end end local id = args.id or args[1] -- If the id is a number, replace it with a name if tonumber( id ) then id = tonumber( id ) else -- otherwise check if the name is deprecated local idData = ids[id] or ids[mw.ustring.lower( id ):gsub( '[%s%+]', '-' )] if idData and idData.deprecated then id = idData.pos else id = nil end end -- if we have something to fix if id then if args.id then -- check all available names for the number to see if any match args[1] local check = mw.ustring.lower( args[1] ):gsub( '[%s%+]', '-' ) for _, v in ipairs( names[id] ) do				if args[1] == v or check == v then args.id = nil break end end -- if none do, then pull the first name, or if none exist just use the number if args.id then args.id = names[id] and names[id][1] or id			end else -- only set args[1] if we are using sprite mode, otherwise set the id			if sprite then args[1] = names[id] and names[id][1] or id			else args.id = names[id] and names[id][1] or id			end end end -- Return the added args local outArgs = { '' } local argTypes = { 'id', 'link', 1, 2, 'text' } for _, arg in ipairs( argTypes ) do		if args[arg] then if arg == 1 or arg == 2 then table.insert( outArgs, args[arg] ) else table.insert( outArgs, arg .. '=' .. args[arg] ) end end end

return '' end

return p