Module:Sprite: Difference between revisions

From Modded Wiki
Jump to navigation Jump to search
Move nowrap span inside links
Created page with "local p = {} function p.base( f ) local args = f if f == mw.getCurrentFrame() then args = require( 'Module:ProcessArgs' ).merge( true ) else f = mw.getCurrentFrame()..."
Line 4: Line 4:
if f == mw.getCurrentFrame() then  
if f == mw.getCurrentFrame() then  
args = require( 'Module:ProcessArgs' ).merge( true )
args = require( 'Module:ProcessArgs' ).merge( true )
else
f = mw.getCurrentFrame()
end
end
Line 12: Line 14:
size = 16,
size = 16,
pos = 1,
pos = 1,
link = '',
align = 'text-top'
align = 'text-top',
class = '',
text = '',
title = ''
}
}
local defaultStyle = mw.clone( default )
local defaultStyle = default
if args.settings then
if args.settings then
local settings = mw.loadData( 'Module:' .. args.settings )
local settings = mw.loadData( 'Module:' .. args.settings )
if not settings.stylesheet then
-- Make a separate clone of the current default settings
defaultStyle = mw.clone( default )
end
for k, v in pairs( settings ) do
for k, v in pairs( settings ) do
default[k] = v
default[k] = v
if settings.stylesheet then
defaultStyle[k] = v
end
end
end
end
end
local name = args.name or default.name
local setting = function( arg )
local scale = args.scale or default.scale
return args[arg] or default[arg]
local autoScale = args.autoscale or default.autoscale
end
local sheetWidth = args.sheetsize or default.sheetsize
local size = args.size or default.size
local sprite = mw.html.create( 'span' ):addClass( 'sprite' )
local pos = math.abs( args.pos or default.pos ) - 1
sprite:tag( 'br' )
local link = args.link or default.link
local align = args.align or default.align
local class = args.class or default.class
local text = args.text or default.text
local title = args.title or default.title
local css = args.css or default.css
local className = args.classname or default.classname
 
local tiles = sheetWidth / size
local left = pos % tiles * size
local top = math.floor( pos / tiles ) * size
local styles = {}
if setting( 'stylesheet' ) then
if args.stylesheet or default.stylesheet then
sprite:addClass(
class = ( className or mw.ustring.lower( name:gsub( ' ', '-' ) ) .. '-sprite ' ) .. class
setting( 'classname' ) or
mw.ustring.lower( setting( 'name' ):gsub( ' ', '-' ) ) .. '-sprite'
)
else
else
table.insert( styles, 'background-image:{{FileUrl|' .. ( args.image or default.image or name .. 'Sprite.png' ) .. '}}' )
sprite:css(
'background-image',
'{{FileUrl|' .. ( setting( 'image' ) or setting( 'name' ) .. 'Sprite.png' ) .. '}}'
)
end
if setting( 'class' ) then
sprite:addClass( setting( 'class' ) )
end
end
local size = setting( 'size' )
local pos = math.abs( setting( 'pos' ) ) - 1
local tiles = setting( 'sheetsize' ) / size
local left = pos % tiles * size
local top = math.floor( pos / tiles ) * size
local scale = setting( 'scale' )
if left > 0 or top > 0 then
if left > 0 or top > 0 then
table.insert( styles, 'background-position:-' .. left * scale .. 'px -' .. top * scale .. 'px' )
sprite:css( 'background-position', '-' .. left * scale .. 'px -' .. top * scale .. 'px' )
end
end
if not autoScale and scale ~= defaultStyle.scale then
if not setting( 'autoscale' ) and scale ~= defaultStyle.scale then
table.insert( styles, 'background-size:' .. sheetWidth * scale .. 'px auto' )
sprite:css( 'background-size', setting( 'sheetsize' ) * scale .. 'px auto' )
end
end
if size ~= defaultStyle.size or ( not autoScale and scale ~= defaultStyle.scale ) then
if size ~= defaultStyle.size or ( not setting( 'autoscale' ) and scale ~= defaultStyle.scale ) then
table.insert( styles, 'height:' .. size * scale .. 'px;width:' .. size * scale .. 'px' )
sprite:css( 'height', size * scale .. 'px;width:' .. size * scale .. 'px' )
end
end
if align ~= defaultStyle.align then
if setting( 'align' ) ~= defaultStyle.align then
table.insert( styles, 'vertical-align:' .. align )
sprite:css( 'vertical-align', setting( 'align' ) )
end
end
if css then
if css then
table.insert( styles, css )
sprite:cssText( css )
end
end
if title ~= '' then
title = ' title="' .. title .. '"'
local root
local spriteText
if setting( 'text' ) then
root = mw.html.create( 'span' ):addClass( 'nowrap' )
spriteText = mw.html.create( 'span' ):addClass( 'sprite-text' ):wikitext( setting( 'text' ) )
end
end
local sprite = table.concat( {
if setting( 'title' ) then
'<span',
( root or sprite ):attr( 'title', setting( 'title' ) )
'class="sprite ' .. class .. '"',
end
'style="' .. table.concat( styles, ';' ) .. '"',
title,
'><br></span>'
}, ' ' )
sprite = sprite:gsub( '%s+([">])', '%1' )
if text ~= '' then
if not root then
text = '<span class="sprite-text"' .. title .. '>' .. text .. '</span>'
root = mw.html.create( '' )
end
root:node( sprite )
if spriteText then
root:node( spriteText )
end
end
local content = '<span class="nowrap">' .. sprite .. text .. '</span>'
local link = setting( 'link' )
if link ~= '' and mw.ustring.lower( link ) ~= 'none' then
if link and mw.ustring.lower( link ) ~= 'none' then
-- External link
if link:find( '//' ) then
if link:find( '//' ) then
-- External link
return '[' .. link .. ' ' .. tostring( root ) .. ']'
return '[' .. link .. ' ' .. content .. ']'
else
-- Internal link
local linkPrefix = args.linkprefix or default.linkprefix or ''
return '[[' .. linkPrefix .. link .. '|' .. content .. ']]'
end
end
else
return content
-- Internal link
local linkPrefix = setting( 'linkprefix' ) or ''
return '[[' .. linkPrefix .. link .. '|' .. tostring( root ) .. ']]'
end
end
return tostring( root )
end
end


Line 117: Line 123:
local name = args.name or default.name
local name = args.name or default.name
local ids = mw.loadData( 'Module:' .. ( args.ids or default.ids or 'Sprite/' .. name ) )
local ids = mw.loadData( 'Module:' .. ( args.ids or default.ids or name .. '/IDs' ) ).ids
local id = mw.text.trim( args[1] or '' )
local id = mw.text.trim( args[1] or '' )
local pos = ids[id] or ids[mw.ustring.lower( id ):gsub( '[%s%+]', '-' )]
local pos = ( ids[id] or ids[mw.ustring.lower( id ):gsub( '[%s%+]', '-' )] or {} ).pos
if not pos and not mw.title.getCurrentTitle().isSubpage then
if not pos and not mw.title.getCurrentTitle().isSubpage then
category = '[[Category:Pages with missing sprites]]'
category = '[[Category:Pages with missing sprites]]'
Line 149: Line 155:


function p.doc( f )
function p.doc( f )
local settings = mw.loadData( 'Module:' .. f.args[1] )
local args = f
local idTable = mw.title.new( 'Module:' .. ( settings.ids or 'Sprite/' .. settings.name ) ):getContent()
if f == mw.getCurrentFrame() then
idTable = idTable:gsub( '(\n%s*%-%-%s*.-%s*%-%-%s*\n)', '%1,' ):gsub( '^return {', '' ):gsub( '}$', '' )
args = f.args
else
f = mw.getCurrentFrame()
end
local settingsPage = mw.text.trim( args[1] )
local settings = mw.loadData( 'Module:' .. settingsPage )
local idsPage = 'Module:' .. ( settings.ids or settings.name .. '/IDs' )
local body
if args.refresh then
body = mw.html.create()
else
local spriteSheet = settings.image or settings.name .. 'Sprite.png'
body = mw.html.create( 'div' ):attr( {
id = 'spritedoc',
['data-idspage'] = '{{PAGEID:' .. idsPage .. '}}',
['data-idstimestamp'] = '{{REVISIONTIMESTAMP:' .. idsPage .. '}}',
['data-spritesheet'] = spriteSheet,
['data-pos'] = settings.pos or 1,
['data-refreshtext'] = mw.text.nowiki( '{{#invoke:sprite2|doc|' .. settingsPage .. '|refresh=1}}' )
} )
end
local data = mw.loadData( idsPage )
local sections = {}
for _, sectionName in ipairs( data.sections or { 'Uncategorized' } ) do
local sectionTag = body:tag( 'div' ):addClass( 'spritedoc-section' )
-- https://phabricator.wikimedia.org/T73594
sectionTag:wikitext( '<h3>', sectionName, '</h3>' )
sections[sectionName] = { boxes = sectionTag:tag( 'ul' ):addClass( 'spritedoc-boxes' ) }
end
local html = {}
local nameKeys = {}
local ids = {}
for name in pairs( data.ids ) do
local posKeys = {}
table.insert( nameKeys, name )
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
end
table.sort( nameKeys, function( a, b )
local list = {}
return mw.ustring.lower( a ) < mw.ustring.lower( b )
local listHead = '<ul class="spritedoc-multicolumn">'
end )
local listFoot = '</ul>'
local lastSection = ''
for _, name in ipairs( nameKeys ) do
for i, pos in ipairs( posKeys ) do
local val = data.ids[name]
local id = ids[pos].id
local pos = val.pos
local newSection = mw.text.trim( ids[pos].section )
local sectionName = data.sections[val.section]
local section = sections[sectionName]
if newSection ~= lastSection or i == 1 then
local names = section[pos]
if newSection ~= lastSection then
if not names then
if lastSection ~= '' then
local box = section.boxes:tag( 'li' ):addClass( 'spritedoc-box' ):attr( 'data-pos', pos )
table.insert( list, listFoot )
box:tag( 'div' ):addClass( 'spritedoc-image' )
end
:wikitext( p.sprite{ pos, settings = settingsPage } )
table.insert( list, '\n===' .. newSection .. '===\n' )
names = box:tag( 'ul' ):addClass( 'spritedoc-names' )
lastSection = newSection
section[pos] = names
end
table.insert( list, listHead )
end
table.insert( list, '<li><table><tr><td data-pos="' .. pos .. '">' )
if type( id ) == 'table' then
for i, id2 in ipairs( id ) do
if i == 1 then
table.insert( list, p.sprite{ id2, settings = f.args[1] } .. '</td><td><div class="sprite-id"><code>' .. id2 .. '</code></div>' )
else
table.insert( list, '<div class="sprite-id"><code>' .. id2 .. '</code></div>' )
end
end
else
table.insert( list, p.sprite{ id, settings = f.args[1] } .. '</td><td><div class="sprite-id"><code>' .. id .. '</code></div>' )
end
table.insert( list, '</td></tr></table></li>' )
if i == #posKeys then
table.insert( list, listFoot )
end
end
names:tag( 'li' ):addClass( 'spritedoc-name' ):tag( 'code' ):wikitext( name )
end
end
local out = table.concat( list )
if args.refresh then
if not f.args.refresh then
return tostring( body )
out = f:preprocess( '{{#widget:stylesheet|page=Sprite doc}}' ) .. '<div id="sprite-doc" data-settings="' .. f.args[1] .. '">' .. out .. '</div>'
end
end
return f:callParserFunction( '#widget:Stylesheet', { page = 'SpriteDoc' } ) .. tostring( body )
return out
end
end
return p
return p

Revision as of 16:04, 11 July 2015

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

local p = {}
function p.base( 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,
		sheetsize = 256,
		size = 16,
		pos = 1,
		align = 'text-top'
	}
	
	local defaultStyle = default
	if args.settings then
		local settings = mw.loadData( 'Module:' .. args.settings )
		if not settings.stylesheet then
			-- Make a separate clone of the current default settings
			defaultStyle = mw.clone( default )
		end
		for k, v in pairs( settings ) do
			default[k] = v
		end
	end
	
	local setting = function( arg )
		return args[arg] or default[arg]
	end
	
	local sprite = mw.html.create( 'span' ):addClass( 'sprite' )
	sprite:tag( 'br' )
	
	if setting( 'stylesheet' ) then
		sprite:addClass(
			setting( 'classname' ) or
			mw.ustring.lower( setting( 'name' ):gsub( ' ', '-' ) ) .. '-sprite'
		)
	else
		sprite:css(
			'background-image',
			'{{FileUrl|' .. ( setting( 'image' ) or setting( 'name' ) .. 'Sprite.png' ) .. '}}'
		)
	end
	if setting( 'class' ) then
		sprite:addClass( setting( 'class' ) )
	end
	
	local size = setting( 'size' )
	local pos = math.abs( setting( 'pos' ) ) - 1
	local tiles = setting( 'sheetsize' ) / size
	local left = pos % tiles * size
	local top = math.floor( pos / tiles ) * size
	local scale = setting( 'scale' )
	if left > 0 or top > 0 then
		sprite:css( 'background-position', '-' .. left * scale .. 'px -' .. top * scale .. 'px' )
	end
	if not setting( 'autoscale' ) and scale ~= defaultStyle.scale then
		sprite:css( 'background-size', setting( 'sheetsize' ) * scale .. 'px auto' )
	end
	if size ~= defaultStyle.size or ( not setting( 'autoscale' ) and scale ~= defaultStyle.scale ) then
		sprite:css( 'height', size * scale .. 'px;width:' .. size * scale .. 'px' )
	end
	if setting( 'align' ) ~= defaultStyle.align then
		sprite:css( 'vertical-align', setting( 'align' ) )
	end
	if css then
		sprite:cssText( css )
	end
	
	local root
	local spriteText
	if setting( 'text' ) then
		root = mw.html.create( 'span' ):addClass( 'nowrap' )
		spriteText = mw.html.create( 'span' ):addClass( 'sprite-text' ):wikitext( setting( 'text' ) )
	end
	
	if setting( 'title' ) then
		( root or sprite ):attr( 'title', setting( 'title' ) )
	end
	
	if not root then
		root = mw.html.create( '' )
	end
	root:node( sprite )
	if spriteText then
		root:node( spriteText )
	end
	
	local link = setting( 'link' )
	if link and mw.ustring.lower( link ) ~= 'none' then
		-- External link
		if link:find( '//' ) then
			return '[' .. link .. ' ' .. tostring( root ) .. ']'
		end
		
		-- Internal link
		local linkPrefix = setting( 'linkprefix' ) or ''
		return '[[' .. linkPrefix .. link .. '|' .. tostring( root ) .. ']]'
	end
	
	return tostring( root )
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 default = {}
		if args.settings then
			default = mw.loadData( 'Module:' .. args.settings )
		end
		
		local name = args.name or default.name
		local ids = mw.loadData( 'Module:' .. ( args.ids or default.ids or name .. '/IDs' ) ).ids
		local id = mw.text.trim( args[1] or '' )
		local pos = ( ids[id] or ids[mw.ustring.lower( id ):gsub( '[%s%+]', '-' )] or {} ).pos
		if not pos and not mw.title.getCurrentTitle().isSubpage then
			category = '[[Category:Pages with missing sprites]]'
		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 args[1] and not args.id then
		link = args[1]:match( '^(.-)%+' ) or args[1]
	end
	local text = args.text or args[2] or link
	
	args[1] = args.id or args[1]
	args.link = args.link or link
	args.text = text
	
	return p.sprite( args )
end

function p.doc( f )
	local args = f
	if f == mw.getCurrentFrame() then
		args = f.args
	else
		f = mw.getCurrentFrame()
	end
	local settingsPage = mw.text.trim( args[1] )
	local settings = mw.loadData( 'Module:' .. settingsPage )
	local idsPage = 'Module:' .. ( settings.ids or settings.name .. '/IDs' )
	
	local body
	if args.refresh then
		body = mw.html.create()
	else
		local spriteSheet = settings.image or settings.name .. 'Sprite.png'
		body = mw.html.create( 'div' ):attr( {
			id = 'spritedoc',
			['data-idspage'] = '{{PAGEID:' .. idsPage .. '}}',
			['data-idstimestamp'] = '{{REVISIONTIMESTAMP:' .. idsPage .. '}}',
			['data-spritesheet'] = spriteSheet,
			['data-pos'] = settings.pos or 1,
			['data-refreshtext'] = mw.text.nowiki( '{{#invoke:sprite2|doc|' .. settingsPage .. '|refresh=1}}' )
		} )
	end
	
	local data = mw.loadData( idsPage )
	
	local sections = {}
	for _, sectionName in ipairs( data.sections or { 'Uncategorized' } ) do
		local sectionTag = body:tag( 'div' ):addClass( 'spritedoc-section' )
		-- https://phabricator.wikimedia.org/T73594
		sectionTag:wikitext( '<h3>', sectionName, '</h3>' )
		sections[sectionName] = { boxes = sectionTag:tag( 'ul' ):addClass( 'spritedoc-boxes' ) }
	end
	
	local nameKeys = {}
	for name in pairs( data.ids ) do
		table.insert( nameKeys, name )
	end
	table.sort( nameKeys, function( a, b )
		return mw.ustring.lower( a ) < mw.ustring.lower( b )
	end )
	
	for _, name in ipairs( nameKeys ) do
		local val = data.ids[name]
		local pos = val.pos
		local sectionName = data.sections[val.section]
		local section = sections[sectionName]
		local names = section[pos]
		if not names then
			local box = section.boxes:tag( 'li' ):addClass( 'spritedoc-box' ):attr( 'data-pos', pos )
			box:tag( 'div' ):addClass( 'spritedoc-image' )
				:wikitext( p.sprite{ pos, settings = settingsPage } )
			
			names = box:tag( 'ul' ):addClass( 'spritedoc-names' )
			section[pos] = names
		end
		names:tag( 'li' ):addClass( 'spritedoc-name' ):tag( 'code' ):wikitext( name )
	end
	
	if args.refresh then
		return tostring( body )
	end
	return f:callParserFunction( '#widget:Stylesheet', { page = 'SpriteDoc' } ) .. tostring( body )
end
return p