Module:SpriteFile: Difference between revisions

From Modded Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
 
(9 intermediate revisions by 3 users not shown)
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 19: Line 13:
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 = default.modid
local file = (id == "" and "Grid Unknown.png") or (name .. " " .. id .. "." .. ext)
local link = ( args.link or "")
if not args.keepcase then
local modPrefix = args.modid or default.modid
id = mw.ustring.lower( id ):gsub( '[%s%+]', '-' )
end
if link ~= "" and not isExternalLink(link) then
local mod = id:match( '([^:%]]+):' ) or "Minecraft"
link = modPrefix .. ":" .. link
 
local nameStart = ( id:find( ':' ) or id:find( '%]' ) or 0 ) + 1
if nameStart - 1 == #id then
nameStart = 1
end
end
local nameId = id:sub( nameStart, ( id:find( '[,%[]', nameStart ) or 0 ) - 1 )
-- if mw.ustring.lower( link ) == 'none' then
local link = ( args.link or '' )
-- link = ''
if mw.ustring.lower( link ) == 'none' then
-- elseif link ~= '' then
link = ''
-- local linkPrefix = ( not link:find( '//' ) and args.linkprefix ) or ''
elseif link ~= '' then
-- link = linkPrefix .. link
local linkPrefix = ( not link:find( '//' ) and args.linkprefix ) or ''
-- end
link = linkPrefix .. link
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
local name = args.name or ''
if name == 'InvSprite' then
name = 'Invicon'
end
local file = ''
if mod == 'Minecraft' or mod == 'minecraft' then
file = name .. ' ' .. nameId .. '.png'
else
file = name .. ' ' .. mod .. ' ' .. nameId .. '.png'
end
end
local altText = file .. ": Sprite image for " .. id
local altText = ''
if link ~= '' then altText = altText .. " linking to " .. link end
if link == '' then
if id == '' then altText = "Unknown sprite image" end
altText = file .. ': Sprite image for ' .. nameId .. ' in ' .. mod
 
end
local sprite = mw.html.create("span"):addClass("sprite-file")
if id == '' then
local img   = string.format("[[File:%s|%s|link=%s|alt=%s|class=pixel-image|%s]]",
file = 'Grid Unknown.png'
file, size, link, altText, args.title or '')
altText = 'Unknown sprite image'
sprite:node(img)
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] = "--vertical-align:" .. align end
if align ~= default.align then
if args.css then styles[#styles+1] = args.css end
styles[#styles + 1] = '--vertical-align:' .. align
sprite:cssText(table.concat(styles, ";"))
end
styles[#styles + 1] = args.css
local root = mw.html.create("")
sprite:cssText( table.concat( styles, ';' ) )
local haveText = args.text or args["wrap"] == nil
local spriteText = nil
if haveText then
local root
spriteText = mw.html.create("span"):addClass("sprite-text"):wikitext(args.text or "")
local spriteText
if args.title then spriteText:attr("title", args.title) end
if args.text then
if not args['wrap'] then
if link ~= "" then
root = mw.html.create( 'span' ):addClass( 'nowrap' )
if isExternalLink(link) then
end
spriteText = "[" .. link .. " " .. tostring(spriteText) .. "]"
spriteText = mw.html.create( 'span' ):addClass( 'sprite-text' ):wikitext( args.text )
if args.title then
spriteText:attr( 'title', args.title )
else
spriteText:attr( 'title', nameId )
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
 
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("Module:ProcessArgs").merge(true)
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 raw = args[1] or ""
raw = raw:gsub("\\[^\\]+$", "")
 
if not args.notext then
if not args.notext then
args.text = args.text or args[2] or raw
text = args.text or args[2] or ''
if text == '' then
local nameStart = (link:find( ':' ) or 0 ) + 1
if nameStart - 1 == #link then
nameStart = 1
end
text = link:sub( nameStart, ( link:find( '[,%[]', nameStart ) or 0 ) - 1 )
end
end
end
 
args[1] = args.id or raw
args[1] = args.id or args[1]
args.link = args.link or raw
args.link = args.link or link
return p.sprite(args)
args.text = text
return p.sprite( args )
end
end


return p
return p

Latest revision as of 22:21, 19 May 2025

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 mod = id:match( '([^:%]]+):' ) or "Minecraft"

	local nameStart = ( id:find( ':' ) or id:find( '%]' ) or 0 ) + 1
	if nameStart - 1 == #id then
		nameStart = 1
	end
	local nameId = id:sub( nameStart, ( id:find( '[,%[]', nameStart ) or 0 ) - 1 )
	
	local link = ( args.link or '' )
	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 name = args.name or ''
	if name == 'InvSprite' then
		name = 'Invicon'
	end
	local file = ''
	
	if mod == 'Minecraft' or mod == 'minecraft' then
		file = name .. ' ' .. nameId .. '.png'
	else
		file = name .. ' ' .. mod .. ' ' .. nameId .. '.png'
	end
	
	local altText = ''
	if link == '' then
		altText = file .. ': Sprite image for ' .. nameId .. ' in ' .. mod
	end
	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
	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 )
		else
			spriteText:attr( 'title', nameId )
		end
		if link ~= '' then
			-- External link
			if link:find( '//' ) then
				spriteText = '[' .. link .. ' ' .. tostring( spriteText ) .. ']'
			else
				spriteText = '[[' .. link .. '|' .. tostring( spriteText ) .. ']]'
			end
		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 ''
		
		if text == '' then
			local nameStart = (link:find( ':' ) or 0 ) + 1
			if nameStart - 1 == #link then
				nameStart = 1
			end
			text = link:sub( nameStart, ( link:find( '[,%[]', nameStart ) or 0 ) - 1 )
		end
	end
	
	args[1] = args.id or args[1]
	args.link = args.link or link
	args.text = text
	
	return p.sprite( args )
end

return p