From eec6759649b7af21bf3937c19f330e0b8575a473 Mon Sep 17 00:00:00 2001 From: Scott Davis Date: Thu, 12 May 2011 23:36:59 -0400 Subject: [PATCH 1/3] refactored the sass importer for sprites --- lib/compass/configuration/adapters.rb | 4 +- lib/compass/sass_extensions/sprites/base.rb | 2 +- .../sass_extensions/sprites/sprite_map.rb | 75 ++++++++++++------- .../sass_extensions/sprites/sprites.rb | 62 --------------- test/units/sprites/base_test.rb | 2 +- test/units/sprites/image_test.rb | 2 +- 6 files changed, 53 insertions(+), 94 deletions(-) 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/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..54c54f4b 100644 --- a/lib/compass/sass_extensions/sprites/sprite_map.rb +++ b/lib/compass/sass_extensions/sprites/sprite_map.rb @@ -1,14 +1,54 @@ 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(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 initialize(uri, options) + + 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 +90,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 index 7431983f..e69de29b 100644 --- a/lib/compass/sass_extensions/sprites/sprites.rb +++ b/lib/compass/sass_extensions/sprites/sprites.rb @@ -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 From 2d797ba95662aab4d7a2175d5f44553aa63de8ed Mon Sep 17 00:00:00 2001 From: Scott Davis Date: Thu, 12 May 2011 23:38:57 -0400 Subject: [PATCH 2/3] removed unneeded file --- lib/compass/sass_extensions/sprites.rb | 1 - lib/compass/sass_extensions/sprites/sprites.rb | 0 2 files changed, 1 deletion(-) delete mode 100644 lib/compass/sass_extensions/sprites/sprites.rb 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/sprites.rb b/lib/compass/sass_extensions/sprites/sprites.rb deleted file mode 100644 index e69de29b..00000000 From 6ba72b953348f5d188cb01ab12739b3f7f86848b Mon Sep 17 00:00:00 2001 From: Scott Davis Date: Thu, 12 May 2011 23:44:21 -0400 Subject: [PATCH 3/3] move hashing and comparison methods into SpriteMap class --- lib/compass/sass_extensions/sprites/sprite_map.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/compass/sass_extensions/sprites/sprite_map.rb b/lib/compass/sass_extensions/sprites/sprite_map.rb index 54c54f4b..b89b072f 100644 --- a/lib/compass/sass_extensions/sprites/sprite_map.rb +++ b/lib/compass/sass_extensions/sprites/sprite_map.rb @@ -34,6 +34,15 @@ module Compass 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={})