Moduł:Sprite: Różnice pomiędzy wersjami

Z Minecraft Wiki Polska
Przejdź do nawigacji Przejdź do wyszukiwania
[wersja nieprzejrzana][wersja nieprzejrzana]
Przetłumaczenie kategorii
Część dalsza eksperymentowania. Przywrócenie starego kodu i sprawdzenie czy po zaaplikowaniu poprzednich zmian to coś da
Linia 4: Linia 4:
if f == mw.getCurrentFrame() then  
if f == mw.getCurrentFrame() then  
args = require( 'Moduł:ProcessArgs' ).merge( true )
args = require( 'Moduł:ProcessArgs' ).merge( true )
else
f = mw.getCurrentFrame()
end
end
Linia 14: Linia 12:
size = 16,
size = 16,
pos = 1,
pos = 1,
align = 'text-top'
link = '',
align = 'text-top',
class = '',
text = '',
title = ''
}
}
local defaultStyle = default
local defaultStyle = mw.clone( default )
if args.settings then
if args.settings then
local settings = mw.loadData( 'Moduł:' .. args.settings )
local settings = mw.loadData( 'Moduł:' .. 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 setting = function( arg )
local name = args.name or default.name
return args[arg] or default[arg]
local scale = args.scale or default.scale
end
local autoScale = args.autoscale or default.autoscale
local sheetWidth = args.sheetsize or default.sheetsize
local size = args.size or default.size
local pos = math.abs( args.pos or default.pos ) - 1
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 sprite = mw.html.create( 'span' ):addClass( 'sprite' )
sprite:tag( 'br' )
-- mw.html's css method performs very slow escaping, which doubles the time it takes
-- to run, so we'll construct the styles manually, and put them in the cssText
-- method, which only does html escaping (which isn't slow)
local styles = {}
local styles = {}
if args.stylesheet or default.stylesheet then
if setting( 'stylesheet' ) then
class = ( className or mw.ustring.lower( name:gsub( ' ', '-' ) ) .. '-sprite ' ) .. class
sprite:addClass(
setting( 'classname' ) or
mw.ustring.lower( setting( 'name' ):gsub( ' ', '-' ) ) .. '-sprite'
)
else
else
table.insert( styles, 'background-image:{{FileUrl|' .. (
table.insert( styles, 'background-image:{{FileUrl|' .. ( args.image or default.image or name .. 'Sprite.png' ) .. '}}' )
setting( 'image' ) or setting( 'name' ) .. 'Sprite.png'
) .. '}}' )
end
local class = setting( 'class' )
if class then
sprite:addClass( class )
end
end
local size = setting( 'size' )
local pos = math.abs( setting( 'pos' ) ) - 1
local sheetWidth = setting( 'sheetsize' )
local tiles = sheetWidth / size
local left = pos % tiles * size
local top = math.floor( pos / tiles ) * size
local scale = setting( 'scale' )
local autoScale = setting( 'autoscale' )
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' )
table.insert( styles, 'background-position:-' .. left * scale .. 'px -' .. top * scale .. 'px' )
Linia 72: Linia 61:
end
end
if size ~= defaultStyle.size or ( not autoScale and scale ~= defaultStyle.scale ) then
if size ~= defaultStyle.size or ( not autoScale and scale ~= defaultStyle.scale ) then
table.insert( styles, 'height:' .. size * scale .. 'px' )
table.insert( styles, 'height:' .. size * scale .. 'px;width:' .. size * scale .. 'px' )
table.insert( styles, 'width:' .. size * scale .. 'px' )
end
end
local align = setting( 'align' )
if align ~= defaultStyle.align then
if align ~= defaultStyle.align then
table.insert( styles, 'vertical-align:' .. align )
table.insert( styles, 'vertical-align:' .. align )
end
end
table.insert( styles, setting( 'css' ) )
if css then
sprite:cssText( table.concat( styles, ';' ) )
table.insert( styles, css )
end
local text = setting( 'text' )
if title ~= '' then
local root
title = ' title="' .. title .. '"'
local spriteText
if text then
root = mw.html.create( 'span' ):addClass( 'nowrap' )
spriteText = mw.html.create( 'span' ):addClass( 'sprite-text' ):wikitext( text )
end
end
local title = setting( 'title' )
local sprite = table.concat( {
if title then
'<span',
( root or sprite ):attr( 'title', title )
'class="sprite ' .. class .. '"',
end
'style="' .. table.concat( styles, ';' ) .. '"',
title,
'><br></span>'
}, ' ' )
sprite = sprite:gsub( '%s+([">])', '%1' )
if not root then
if text ~= '' then
root = mw.html.create( '' )
text = '<span class="sprite-text"' .. title .. '>' .. text .. '</span>'
end
root:node( sprite )
if spriteText then
root:node( spriteText )
end
end
local link = setting( 'link' ) or ''
local content = '<span class="nowrap">' .. sprite .. text .. '</span>'
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
return '[' .. link .. ' ' .. tostring( root ) .. ']'
-- External link
return '[' .. link .. ' ' .. content .. ']'
else
-- Internal link
local linkPrefix = args.linkprefix or default.linkprefix or ''
return '[[' .. linkPrefix .. link .. '|' .. content .. ']]'
end
end
else
-- Internal link
return content
local linkPrefix = setting( 'linkprefix' ) or ''
return '[[' .. linkPrefix .. link .. '|' .. tostring( root ) .. ']]'
end
end
return tostring( root )
end
end


Linia 124: Linia 107:
end
end
local categories = {}
local category = ''
local idData = args.iddata
if tonumber( args[1] ) then
if not idData then
args.pos = args[1]
else
local default = {}
local default = {}
if args.settings then
if args.settings then
Linia 133: Linia 117:
local name = args.name or default.name
local name = args.name or default.name
local ids = mw.loadData( 'Moduł:' .. ( args.ids or default.ids or name .. '/IDs' ) )
local ids = mw.loadData( 'Moduł:' .. ( args.ids or default.ids or 'Sprite/' .. name ) )
ids = ids.ids or ids
local id = mw.text.trim( args[1] or '' )
local id = mw.text.trim( tostring( args[1] or '' ) )
local pos = ids[id] or ids[mw.ustring.lower( id ):gsub( '[%s%+]', '-' )]
idData = ids[id] or ids[mw.ustring.lower( id ):gsub( '[%s%+]', '-' )]
if not pos and not mw.title.getCurrentTitle().isSubpage then
end
category = '[[Category:Strony z brakującymi spritami]]'
local title = mw.title.getCurrentTitle()
-- Remove categories on sub-pages, and User* and *Talk namespaces
local disallowCats = title.isSubpage or title.nsText:find( '^User' ) or title.nsText:find( '[Tt]alk$' )
if idData then
if type( idData ) == 'table' then
if idData.deprecated and not disallowCats then
table.insert( categories, '[[Kategoria:Strony posiadające przestarzałe sprity]]' )
end
args.pos = idData.pos
else
args.pos = idData
end
end
elseif not disallowCats then
args.pos = pos
table.insert( categories, '[[Kategoria:Strony z brakującymi spritami]]' )
end
end
return p.base( args ), table.concat( categories, '' )
return p.base( args ) .. category
end
end


Linia 179: Linia 149:


function p.doc( f )
function p.doc( f )
local args = f
local settings = mw.loadData( 'Moduł:' .. f.args[1] )
if f == mw.getCurrentFrame() then
local idTable = mw.title.new( 'Moduł:' .. ( settings.ids or 'Sprite/' .. settings.name ) ):getContent()
args = f.args
idTable = idTable:gsub( '(\n%s*%-%-%s*.-%s*%-%-%s*\n)', '%1,' ):gsub( '^return {', '' ):gsub( '}$', '' )
else
f = mw.getCurrentFrame()
end
local settingsPage = mw.text.trim( args[1] )
local settings = mw.loadData( 'Moduł:' .. settingsPage )
local idsPage = 'Moduł:' .. ( settings.ids or settings.name .. '/IDs' )
local getProtection = function( title, action, extra )
local html = {}
local protections = { 'edit' }
local ids = {}
if extra then
local posKeys = {}
table.insert( protections, extra )
local section = ''
end
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
local addProtection = function( protection )
if id ~= '' and pos ~= '' then
if protection == 'autoconfirmed' then
if ids[pos] then
protection = 'editsemiprotected'
if type( ids[pos].id ) == 'table' then
elseif protection == 'sysop' then
table.insert( ids[pos].id, id )
protection = 'editprotected'
else
ids[pos].id = { ids[pos].id, id }
end
else
ids[pos] = { id = id, section = section }
table.insert( posKeys, pos )
end
end
table.insert( protections, protection )
end
end
end
local list = {}
local listHead = '<ul class="spritedoc-multicolumn">'
local listFoot = '</ul>'
local lastSection = ''
for i, pos in ipairs( posKeys ) do
local id = ids[pos].id
local newSection = mw.text.trim( ids[pos].section )
local direct = title.protectionLevels[action]
if newSection ~= lastSection or i == 1 then
for _, protection in ipairs( direct ) do
if newSection ~= lastSection then
addProtection( protection )
if lastSection ~= '' then
table.insert( list, listFoot )
end
table.insert( list, '\n===' .. newSection .. '===\n' )
lastSection = newSection
end
table.insert( list, listHead )
end
end
if title.cascadingProtection then
table.insert( list, '<li><table><tr><td data-pos="' .. pos .. '">' )
local cascading = title.cascadingProtection.restrictions[action] or {}
if type( id ) == 'table' then
if #cascading > 0 then
for i, id2 in ipairs( id ) do
table.insert( protections, 'protect' )
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
end
for _, protection in ipairs( cascading ) do
else
addProtection( protection )
table.insert( list, p.sprite{ id, settings = f.args[1] } .. '</td><td><div class="sprite-id"><code>' .. id .. '</code></div>' )
end
elseif f:callParserFunction( 'CASCADINGSOURCES', title.prefixedText ) ~= '' then
-- Just adding the requirement for the protect right should be good enough until MW 1.25
table.insert( protections, 'protect' )
end
end
table.insert( list, '</td></tr></table></li>' )
return table.concat( protections, ',' )
if i == #posKeys then
table.insert( list, listFoot )
end
end
end
local body
local out = table.concat( list )
if args.refresh then
if not f.args.refresh then
body = mw.html.create( '' )
out = f:preprocess( '{{#widget:stylesheet|page=Sprite doc}}' ) .. '<div id="sprite-doc" data-settings="' .. f.args[1] .. '">' .. out .. '</div>'
else
local idsTitle = mw.title.new( idsPage )
local spritesheet = settings.image or settings.name .. 'Sprite.png'
local spriteTitle = mw.title.new( 'File:' .. spritesheet )
local idsProtection = getProtection( idsTitle, 'edit' )
local spriteProtection = getProtection( spriteTitle, 'upload', 'upload,reupload' )
body = mw.html.create( 'div' ):attr( {
id = 'spritedoc',
['data-idspage'] = idsTitle.id,
['data-idsprotection'] = idsProtection,
['data-idstimestamp'] = f:callParserFunction( 'REVISIONTIMESTAMP', idsPage ),
['data-spritesheet'] = spritesheet,
['data-spriteprotection'] = spriteProtection,
['data-pos'] = settings.pos or 1,
['data-refreshtext'] = mw.text.nowiki( '{{#invoke:sprite|doc|' .. settingsPage .. '|refresh=1}}' )
} )
end
end
local data = mw.loadData( idsPage )
return out
local sections = {}
for _, sectionData in ipairs( data.sections or { 'Uncategorized' } ) do
local sectionTag = body:tag( 'div' ):addClass( 'spritedoc-section' ):attr( 'data-section-id', sectionData.id )
-- https://phabricator.wikimedia.org/T73594
sectionTag:wikitext( '<h3>', sectionData[1], '</h3>' )
sections[sectionData.id] = { boxes = sectionTag:tag( 'ul' ):addClass( 'spritedoc-boxes' ) }
end
local keyedData = {}
for name, idData in pairs( data.ids ) do
table.insert( keyedData, {
sortKey = mw.ustring.lower( name ),
name = name,
data = idData
} )
end
table.sort( keyedData, function( a, b )
return a.sortKey < b.sortKey
end )
for _, data in ipairs( keyedData ) do
local idData = data.data
local pos = idData.pos
local section = sections[idData.section]
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.base{ pos = pos, settings = settingsPage } )
names = box:tag( 'ul' ):addClass( 'spritedoc-names' )
section[pos] = names
end
local nameElem = mw.html.create( 'li' ):addClass( 'spritedoc-name' )
local codeElem = nameElem:tag( 'code' ):wikitext( data.name )
if idData.deprecated then
codeElem:addClass( 'spritedoc-deprecated' )
end
names:wikitext( tostring( nameElem ) )
end
if args.refresh then
return '', tostring( body )
end
return f:callParserFunction( '#widget:Stylesheet', { page = 'SpriteDoc' } ), tostring( body )
end
end
return p
return p

Wersja z 02:01, 26 lut 2016

[ utwórz | historia | odśwież ]Dokumentacja
W tym module nie ma dokumentacji. Jeśli wiesz jak używać tego modułu, proszę, podaj odpowiednie informacje.
local p = {}
function p.base( f )
	local args = f
	if f == mw.getCurrentFrame() then 
		args = require( 'Moduł:ProcessArgs' ).merge( true )
	end
	
	-- Default settings
	local default = {
		scale = 1,
		sheetsize = 256,
		size = 16,
		pos = 1,
		link = '',
		align = 'text-top',
		class = '',
		text = '',
		title = ''
	}
	
	local defaultStyle = mw.clone( default )
	if args.settings then
		local settings = mw.loadData( 'Moduł:' .. args.settings )
		for k, v in pairs( settings ) do
			default[k] = v
			if settings.stylesheet then
				defaultStyle[k] = v
			end
		end
	end
	
	local name = args.name or default.name
	local scale = args.scale or default.scale
	local autoScale = args.autoscale or default.autoscale
	local sheetWidth = args.sheetsize or default.sheetsize
	local size = args.size or default.size
	local pos = math.abs( args.pos or default.pos ) - 1
	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 args.stylesheet or default.stylesheet then
		class = ( className or mw.ustring.lower( name:gsub( ' ', '-' ) ) .. '-sprite ' ) .. class
	else
		table.insert( styles, 'background-image:{{FileUrl|' .. ( args.image or default.image or name .. 'Sprite.png' ) .. '}}' )
	end
	if left > 0 or top > 0 then
		table.insert( styles, 'background-position:-' .. left * scale .. 'px -' .. top * scale .. 'px' )
	end
	if not autoScale and scale ~= defaultStyle.scale then
		table.insert( styles, 'background-size:' .. sheetWidth * scale .. 'px auto' )
	end
	if size ~= defaultStyle.size or ( not autoScale and scale ~= defaultStyle.scale ) then
		table.insert( styles, 'height:' .. size * scale .. 'px;width:' .. size * scale .. 'px' )
	end
	if align ~= defaultStyle.align then
		table.insert( styles, 'vertical-align:' .. align )
	end
	if css then
		table.insert( styles, css )
	end
	if title ~= '' then
		title = ' title="' .. title .. '"'
	end
	
	local sprite = table.concat( {
		'<span',
			'class="sprite ' .. class .. '"',
			'style="' .. table.concat( styles, ';' ) .. '"',
			title,
		'><br></span>'
	}, ' ' )
	sprite = sprite:gsub( '%s+([">])', '%1' )
	
	if text ~= '' then
		text = '<span class="sprite-text"' .. title .. '>' .. text .. '</span>'
	end
	
	local content = '<span class="nowrap">' .. sprite .. text .. '</span>'
	if link ~= '' and mw.ustring.lower( link ) ~= 'none' then
		if link:find( '//' ) then
			-- External link
			return '[' .. link .. ' ' .. content .. ']'
		else
			-- Internal link
			local linkPrefix = args.linkprefix or default.linkprefix or ''
			return '[[' .. linkPrefix .. link .. '|' .. content .. ']]'
		end
	else
		return content
	end
end

function p.sprite( f )
	local args = f
	if f == mw.getCurrentFrame() then
		args = require( 'Moduł: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( 'Moduł:' .. args.settings )
		end
		
		local name = args.name or default.name
		local ids = mw.loadData( 'Moduł:' .. ( args.ids or default.ids or 'Sprite/' .. name ) )
		local id = mw.text.trim( args[1] or '' )
		local pos = ids[id] or ids[mw.ustring.lower( id ):gsub( '[%s%+]', '-' )]
		if not pos and not mw.title.getCurrentTitle().isSubpage then
			category = '[[Category:Strony z brakującymi spritami]]'
		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( 'Moduł: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 settings = mw.loadData( 'Moduł:' .. f.args[1] )
	local idTable = mw.title.new( 'Moduł:' .. ( settings.ids or 'Sprite/' .. settings.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 = '<ul class="spritedoc-multicolumn">'
	local listFoot = '</ul>'
	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, '<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
	
	local out = table.concat( list )
	if not f.args.refresh then
		out = f:preprocess( '{{#widget:stylesheet|page=Sprite doc}}' ) .. '<div id="sprite-doc" data-settings="' .. f.args[1] .. '">' .. out .. '</div>'
	end
	
	return out
end
return p