diff --git a/lib/compass/configuration/adapters.rb b/lib/compass/configuration/adapters.rb index 9897f321..6a35c91a 100644 --- a/lib/compass/configuration/adapters.rb +++ b/lib/compass/configuration/adapters.rb @@ -23,7 +23,7 @@ module Compass plugin_opts[:cache_location] = cache_path unless cache_path.nil? plugin_opts.merge!(sass_options || {}) plugin_opts[:load_paths] ||= [] - plugin_opts[:load_paths] << Compass::Sprites.new + plugin_opts[:load_paths] << Compass::SpriteMap.new plugin_opts end @@ -63,7 +63,7 @@ module Compass next p if p.respond_to?(:find_relative) Sass::Importers::Filesystem.new(p.to_s) end - load_paths << Compass::Sprites.new + load_paths << Compass::SpriteMap.new load_paths end end diff --git a/lib/compass/sass_extensions/sprites.rb b/lib/compass/sass_extensions/sprites.rb index 85765179..12e821ed 100644 --- a/lib/compass/sass_extensions/sprites.rb +++ b/lib/compass/sass_extensions/sprites.rb @@ -1,5 +1,4 @@ require 'digest/md5' -require 'compass/sass_extensions/sprites/sprites' require 'compass/sass_extensions/sprites/sprite_map' require 'compass/sass_extensions/sprites/image' require 'compass/sass_extensions/sprites/base' diff --git a/lib/compass/sass_extensions/sprites/base.rb b/lib/compass/sass_extensions/sprites/base.rb index 4bdea36b..3051248a 100644 --- a/lib/compass/sass_extensions/sprites/base.rb +++ b/lib/compass/sass_extensions/sprites/base.rb @@ -7,7 +7,7 @@ module Compass # Initialize a new aprite object from a relative file path # the path is relative to the images_path confguration option def self.from_uri(uri, context, kwargs) - sprite_map = ::Compass::SpriteMap.new(uri.value, {}) + sprite_map = ::Compass::SpriteMap.new(:uri => uri.value, :options => {}) sprites = sprite_map.files.map do |sprite| sprite.gsub(Compass.configuration.images_path+"/", "") end diff --git a/lib/compass/sass_extensions/sprites/sprite_map.rb b/lib/compass/sass_extensions/sprites/sprite_map.rb index 121c4e12..b89b072f 100644 --- a/lib/compass/sass_extensions/sprites/sprite_map.rb +++ b/lib/compass/sass_extensions/sprites/sprite_map.rb @@ -1,14 +1,63 @@ module Compass - class SpriteMap - attr_reader :uri, :options + class SpriteMap < Sass::Importers::Base + attr_accessor :uri, :options VAILD_FILE_NAME = /\A#{Sass::SCSS::RX::IDENT}\Z/ - - def find_relative(*args) - nil + SPRITE_IMPORTER_REGEX = %r{((.+/)?([^\*.]+))/(.+?)\.png} + + def self.load(uri, options) + Compass.quick_cache "Sprite_map:#{uri}#{options.inspect}", 5 do + klass = Compass::SpriteMap.new + klass.uri, klass.options = uri, options + klass + end end - - def initialize(uri, options) + + def initialize(options ={}) + @uri, @options = '', {} + options.each do |key, value| + send("#{key}=", value) + end + end + + def find(uri, options) @uri, @options = uri, options + if uri =~ SPRITE_IMPORTER_REGEX + return sass_engine + end + end + + def find_relative(uri, base, options) + @uri, @options = uri, options + find(File.join(base, uri), options) + end + + def to_s + content_for_images + end + + def hash + self.class.name.hash + end + + def eql?(other) + other.class == self.class + end + + + + def key(uri, options={}) + @uri, @options = uri, options + [self.class.name + ":" + File.dirname(File.expand_path(uri)), File.basename(uri)] + end + + def self.path_and_name(uri) + Compass.quick_cache "Sprite_map_name:#{uri}", 5 do + if uri =~ SPRITE_IMPORTER_REGEX + [$1, $3] + else + [nil, nil] + end + end end # Name of this spite @@ -50,27 +99,8 @@ module Compass end def ensure_path_and_name! - @path ||= get_path - @name ||= get_name - end - - def get_name - _, name = Compass::Sprites.path_and_name(uri) - name - end - - def get_path - path, _ = Compass::Sprites.path_and_name(uri) - path - end - - def key(uri, options) - Compass::Sprites.key(uri) - end - - def mtime(uri, options) - Compass::Sprites.mtime(uri, options) - end + @path, @name = self.class.path_and_name(uri) + end # Generates the Sass for this sprite file def content_for_images(skip_overrides = false) diff --git a/lib/compass/sass_extensions/sprites/sprites.rb b/lib/compass/sass_extensions/sprites/sprites.rb deleted file mode 100644 index 7431983f..00000000 --- a/lib/compass/sass_extensions/sprites/sprites.rb +++ /dev/null @@ -1,62 +0,0 @@ -module Compass - class Sprites < Sass::Importers::Base - attr_accessor :name, :path - - def self.path_and_name(uri) - if uri =~ %r{((.+/)?([^\*.]+))/(.+?)\.png} - [$1, $3] - end - end - - def self.discover_sprites(uri) - self.load_map(uri, {}).files - end - - def self.sprite_name(file) - File.basename(file, '.png') - end - - def self.load_map(uri, options) - Compass.quick_cache("spritemap:#{uri}", 5) do - SpriteMap.new(uri, options) - end - end - - - # Called by the sass engine to build a new SpriteMap - def find(uri, options) - if uri =~ /\.png$/ - map = Compass::Sprites.load_map(uri, options) - self.path, self.name = map.path, map.name - return map.sass_engine - end - end - - # Called by the sass engine to identify the SpriteMap - def self.key(uri, options={}) - [self.class.name + ":" + File.dirname(File.expand_path(uri)), File.basename(uri)] - end - - def self.mtime(uri, options) - Compass.quick_cache("mtime:#{uri}") do - map = Compass::Sprites.load_map(uri, options) - map.files.inject(Time.at(0)) do |max_time, file| - (t = File.mtime(file)) > max_time ? t : max_time - end - end - end - - def to_s - "" - end - - def hash - self.class.name.hash - end - - def eql?(other) - other.class == self.class - end - - end -end diff --git a/test/units/sprites/base_test.rb b/test/units/sprites/base_test.rb index e79a11c3..b557bf29 100644 --- a/test/units/sprites/base_test.rb +++ b/test/units/sprites/base_test.rb @@ -16,7 +16,7 @@ class SpritesBaseTest < Test::Unit::TestCase end def setup_map - @map = Compass::SpriteMap.new("selectors/*.png", @options) + @map = Compass::SpriteMap.new(:uri => "selectors/*.png", :options => @options) @base = Compass::SassExtensions::Sprites::Base.new(@map.sprite_names.map{|n| "selectors/#{n}.png"}, @map, @map.sass_engine, @map.options) end diff --git a/test/units/sprites/image_test.rb b/test/units/sprites/image_test.rb index 01609932..f5a11a50 100644 --- a/test/units/sprites/image_test.rb +++ b/test/units/sprites/image_test.rb @@ -19,7 +19,7 @@ class SpritesImageTest < Test::Unit::TestCase let(:sprite_name) { File.basename(sprite_filename, '.png') } def parent - map = Compass::SpriteMap.new("selectors/*.png", options) + map = Compass::SpriteMap.new(:uri => "selectors/*.png", :options => options) @parent ||= Compass::SassExtensions::Sprites::Base.new(map.sprite_names.map{|n| "selectors/#{n}.png"}, map, map.sass_engine, map.options) end