Module:SpriteFile: Difference between revisions

From Modded Wiki
Jump to navigation Jump to search
added ability to use .gif files for Item Sprites
No edit summary
Line 1: Line 1:
-- Please forgive any dust, I've never touched Lua or anything related before
local p = {}
local p = {}
local function isExternalLink(str)
return str:find('://', 1, true) ~= nil
end


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( 'Module:ProcessArgs' ).merge( true )
args = require("Module:ProcessArgs").merge( true )
else
else
f = mw.getCurrentFrame()
f = mw.getCurrentFrame()
Line 13: Line 19:
scale = 1,
scale = 1,
size = 16,
size = 16,
align = 'text-top'
align = "text-top",
ext = "png",
modid = "minecraft"
}
}
local id = mw.text.trim( tostring( args[1] or '' ) )
local id = mw.text.trim(tostring(args[1] or ""))
local ext = (args.ext or default.ext):gsub("^%.", "")
local name = args.name or ''
local file = (id == "" and "Grid Unknown.png") or (name .. " " .. id .. "." .. ext)
local link = ( args.link or "")
local modPrefix = args.modid or default.modid
local useGif = id:match("\\gif$")
if link ~= "" and not isExternalLink(link) then
    if useGif then
link = modPrefix .. ":" .. link
    id = id:gsub("\\gif$", "")
end
end
local link = ( args.link or '' )
-- if mw.ustring.lower( link ) == 'none' then
if mw.ustring.lower( link ) == 'none' then
-- link = ''
link = ''
-- elseif link ~= '' then
elseif link ~= '' then
-- local linkPrefix = ( not link:find( '//' ) and args.linkprefix ) or ''
local linkPrefix = ( not link:find( '//' ) and args.linkprefix ) or ''
-- link = linkPrefix .. link
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 .. 'x' .. height .. 'px'
local size = width .. "x" .. height .. "px"
local styles = {}
local styles = {}
if height ~= default.size then
if height ~= default.size then
styles[#styles + 1] = 'height:' .. height .. 'px'
styles[#styles + 1] = "height:" .. height .. "px"
end
end
if width ~= default.size then
if width ~= default.size then
styles[#styles + 1] = 'width:' .. width .. 'px'
styles[#styles + 1] = "width:" .. width .. "px"
end
end
local name = args.name
local altText = file .. ": Sprite image for " .. id
if link ~= '' then altText = altText .. " linking to " .. link end
local fileExtension = useGif and '.gif' or '.png'
if id == '' then altText = "Unknown sprite image" end
    local file = name .. ' ' .. id .. fileExtension
 
    local altText = file .. ': Sprite image for ' .. id .. ' in Minecraft'
local sprite = mw.html.create("span"):addClass("sprite-file")
    if link ~= '' then
local img   = string.format("[[File:%s|%s|link=%s|alt=%s|class=pixel-image|%s]]",
        altText = altText .. ' linking to ' .. link
file, size, link, altText, args.title or '')
    end
sprite:node(img)
    if id == '' then
 
        file = 'Grid Unknown.png'
        altText = 'Unknown sprite image'
    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
if align ~= default.align then styles[#styles+1] = "--vertical-align:" .. align end
styles[#styles + 1] = '--vertical-align:' .. align
if args.css then styles[#styles+1] = args.css end
end
sprite:cssText(table.concat(styles, ";"))
styles[#styles + 1] = args.css
sprite:cssText( table.concat( styles, ';' ) )
local root = mw.html.create("")
local haveText = args.text or args["wrap"] == nil
local spriteText = nil
local root
if haveText then
local spriteText
spriteText = mw.html.create("span"):addClass("sprite-text"):wikitext(args.text or "")
if args.text then
if args.title then spriteText:attr("title", args.title) end
if not args['wrap'] then
root = mw.html.create( 'span' ):addClass( 'nowrap' )
if link ~= "" then
end
if isExternalLink(link) then
spriteText = mw.html.create( 'span' ):addClass( 'sprite-text' ):wikitext( args.text )
spriteText = "[" .. link .. " " .. tostring(spriteText) .. "]"
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 = '[[' .. link .. '|' .. tostring( spriteText ) .. ']]'
spriteText = "[[" .. link .. "|" .. tostring(spriteText) .. "]]"
end
end
end
end
end
end
 
if not root then
root:node(sprite)
root = mw.html.create( '' )
if spriteText then root:node(spriteText) end
end
 
root:node( sprite )
return tostring(root)
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( 'Module:ProcessArgs' ).merge( true )
args = require("Module:ProcessArgs").merge(true)
end
end
local link = args[1]
link = link and link:gsub("\\gif$", "")
if args[1] and not args.id then
        link = link:match('^(.-)%+') or link
    end


    local text
local raw = args[1] or ""
    if not args.notext then
raw = raw:gsub("\\[^\\]+$", "")
        text = args.text or args[2] or link
 
    end
if not args.notext then
args.text = args.text or args[2] or raw
args[1] = args.id or args[1]
end
    args.link = args.link or link
    args.text = text


    return p.sprite(args)
args[1]  = args.id or raw
args.link = args.link or raw
return p.sprite(args)
end
end


return p
return p

Revision as of 11:49, 30 April 2025

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

-- Please forgive any dust, I've never touched Lua or anything related before

local p = {}

local function isExternalLink(str)
	return str:find('://', 1, true) ~= nil
end

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",
		ext = "png",
		modid = "minecraft"
	}
	
	local id = mw.text.trim(tostring(args[1] or ""))
	local ext = (args.ext or default.ext):gsub("^%.", "")
	local name = args.name or ''
	local file = (id == "" and "Grid Unknown.png") or (name .. " " .. id .. "." .. ext)
	
	local link = ( args.link or "")
	local modPrefix = args.modid or default.modid
	
	if link ~= "" and not isExternalLink(link) then
		link = modPrefix .. ":" .. link
	end
	
	-- if mw.ustring.lower( link ) == 'none' then
	-- 	link = ''
	-- elseif link ~= '' then
	-- 	local linkPrefix = ( not link:find( '//' ) and args.linkprefix ) or ''
	-- 	link = linkPrefix .. link
	-- end
	
	local scale = args.scale or default.scale
	local height = ( args.height or args.size or default.size ) * scale
	local width = ( args.width or args.size or default.size ) * scale
	local size = width .. "x" .. height .. "px"
	
	local styles = {}
	if height ~= default.size then
		styles[#styles + 1] = "height:" .. height .. "px"
	end
	if width ~= default.size then
		styles[#styles + 1] = "width:" .. width .. "px"
	end
	
	local altText = file .. ": Sprite image for " .. id
	if link ~= '' then altText = altText .. " linking to " .. link end
	if id == '' then altText = "Unknown sprite image" end

	local sprite = mw.html.create("span"):addClass("sprite-file")
	local img    = string.format("[[File:%s|%s|link=%s|alt=%s|class=pixel-image|%s]]",
		file, size, link, altText, args.title or '')
	sprite:node(img)

	local align = args.align or default.align
	if align ~= default.align then styles[#styles+1] = "--vertical-align:" .. align end
	if args.css then styles[#styles+1] = args.css end
	sprite:cssText(table.concat(styles, ";"))
	
	local root = mw.html.create("")
	local haveText = args.text or args["wrap"] == nil
	local spriteText = nil
	
	if haveText then
		spriteText = mw.html.create("span"):addClass("sprite-text"):wikitext(args.text or "")
		if args.title then spriteText:attr("title", args.title) end
		
		if link ~= "" then
			if isExternalLink(link) then
				spriteText = "[" .. link .. " " .. tostring(spriteText) .. "]"
			else
				spriteText = "[[" .. link .. "|" .. tostring(spriteText) .. "]]"
			end
		end
	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 raw = args[1] or ""
	raw = raw:gsub("\\[^\\]+$", "")

	if not args.notext then
		args.text = args.text or args[2] or raw
	end

	args[1]  = args.id or raw
	args.link = args.link or raw
	return p.sprite(args)
end

return p