Module:Sprite: Difference between revisions

Allow template-based sprites to use stylesheets
See if unused code changes memory usage
Line 144: Line 144:
return p.sprite( args )
return p.sprite( args )
end
function p.doc( f )
local args = require( 'Module:ProcessArgs' ).norm( f.args )
local idTable = mw.title.new( 'Module:Sprite/' .. args.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
args[1] = id2
table.insert( list, p.sprite( args ) .. '</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
args[1] = id
table.insert( list, p.sprite( args ) .. '</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 args.refresh then
out = f:preprocess( '{{#widget:stylesheet|page=Sprite doc}}' ) .. '<div id="sprite-doc" data-details=\'{"name":"' .. args.name .. '","size":' .. ( args.size or 16 ) .. '}\'>' .. out .. '</div>'
end
return out
end
end
return p
return p