Move the sprite importer to the compass module and rename it to be more clear about what it does.

This commit is contained in:
Chris Eppstein 2011-06-05 18:06:37 -07:00
parent dc8d8cd765
commit c73d281e58
9 changed files with 21 additions and 22 deletions

View File

@ -39,7 +39,7 @@ module Compass
def perform def perform
relative_uri = options[:uri].gsub(/^#{Compass.configuration.images_dir}\//, '') relative_uri = options[:uri].gsub(/^#{Compass.configuration.images_dir}\//, '')
sprites = Compass::SpriteMap.new(relative_uri, Compass.sass_engine_options) sprites = Compass::SpriteImporter.new(relative_uri, Compass.sass_engine_options)
options[:output_file] ||= File.join(Compass.configuration.sass_path, "sprites", "_#{sprites.name}.#{Compass.configuration.preferred_syntax}") options[:output_file] ||= File.join(Compass.configuration.sass_path, "sprites", "_#{sprites.name}.#{Compass.configuration.preferred_syntax}")
options[:skip_overrides] ||= false options[:skip_overrides] ||= false
contents = sprites.content_for_images(options[:skip_overrides]) contents = sprites.content_for_images(options[:skip_overrides])

View File

@ -23,7 +23,7 @@ module Compass
plugin_opts[:cache_location] = cache_path unless cache_path.nil? plugin_opts[:cache_location] = cache_path unless cache_path.nil?
plugin_opts.merge!(sass_options || {}) plugin_opts.merge!(sass_options || {})
plugin_opts[:load_paths] ||= [] plugin_opts[:load_paths] ||= []
plugin_opts[:load_paths] << Compass::SpriteMap.new plugin_opts[:load_paths] << Compass::SpriteImporter.new
plugin_opts plugin_opts
end end
@ -63,7 +63,7 @@ module Compass
next p if p.respond_to?(:find_relative) next p if p.respond_to?(:find_relative)
Sass::Importers::Filesystem.new(p.to_s) Sass::Importers::Filesystem.new(p.to_s)
end end
load_paths << Compass::SpriteMap.new load_paths << Compass::SpriteImporter.new
load_paths load_paths
end end
end end

View File

@ -1,5 +1,5 @@
require 'digest/md5' require 'digest/md5'
require 'compass/sass_extensions/sprites/sprite_map' require 'compass/sprite_importer'
require 'compass/sass_extensions/sprites/image' require 'compass/sass_extensions/sprites/image'
require 'compass/sass_extensions/sprites/base' require 'compass/sass_extensions/sprites/base'
require 'compass/sass_extensions/sprites/engines' require 'compass/sass_extensions/sprites/engines'

View File

@ -4,14 +4,14 @@ module Compass
class Base < Sass::Script::Literal class Base < Sass::Script::Literal
# Initialize a new aprite object from a relative file path # Initialize a new sprite object from a relative file path
# the path is relative to the <tt>images_path</tt> confguration option # the path is relative to the <tt>images_path</tt> confguration option
def self.from_uri(uri, context, kwargs) def self.from_uri(uri, context, kwargs)
sprite_map = ::Compass::SpriteMap.new(:uri => uri.value, :options => {}) importer = ::Compass::SpriteImporter.new(:uri => uri.value, :options => {})
sprites = sprite_map.files.map do |sprite| sprites = importer.files.map do |sprite|
sprite.gsub(Compass.configuration.images_path+"/", "") sprite.gsub(Compass.configuration.images_path+"/", "")
end end
new(sprites, sprite_map, context, kwargs) new(sprites, importer.path, importer.name, context, kwargs)
end end
# Loads the sprite engine # Loads the sprite engine
@ -23,22 +23,21 @@ module Compass
# We should do so only when the packing algorithm changes # We should do so only when the packing algorithm changes
SPRITE_VERSION = "1" SPRITE_VERSION = "1"
attr_accessor :image_names, :path, :name, :map, :kwargs attr_accessor :image_names, :path, :name, :kwargs
attr_accessor :images, :width, :height attr_accessor :images, :width, :height
def initialize(sprites, sprite_map, context, kwargs) def initialize(sprites, path, name, context, kwargs)
require_engine! require_engine!
@image_names = sprites @image_names = sprites
@path = sprite_map.path @path = path
@name = sprite_map.name @name = name
@kwargs = kwargs @kwargs = kwargs
@kwargs['cleanup'] ||= Sass::Script::Bool.new(true) @kwargs['cleanup'] ||= Sass::Script::Bool.new(true)
@images = nil @images = nil
@width = nil @width = nil
@height = nil @height = nil
@evaluation_context = context @evaluation_context = context
@map = sprite_map
validate! validate!
compute_image_metadata! compute_image_metadata!
end end

View File

@ -1,12 +1,12 @@
module Compass module Compass
class SpriteMap < Sass::Importers::Base class SpriteImporter < Sass::Importers::Base
attr_accessor :uri, :options attr_accessor :uri, :options
VAILD_FILE_NAME = /\A#{Sass::SCSS::RX::IDENT}\Z/ VAILD_FILE_NAME = /\A#{Sass::SCSS::RX::IDENT}\Z/
SPRITE_IMPORTER_REGEX = %r{((.+/)?([^\*.]+))/(.+?)\.png} SPRITE_IMPORTER_REGEX = %r{((.+/)?([^\*.]+))/(.+?)\.png}
def self.load(uri, options) def self.load(uri, options)
Compass.quick_cache "Sprite_map:#{uri}#{options.inspect}", 5 do Compass.quick_cache "Sprite_map:#{uri}#{options.inspect}", 5 do
klass = Compass::SpriteMap.new klass = Compass::SpriteImporter.new
klass.uri, klass.options = uri, options klass.uri, klass.options = uri, options
klass klass
end end

View File

@ -11,7 +11,7 @@ describe Compass::SassExtensions::Sprites::Base do
Compass.configure_sass_plugin! Compass.configure_sass_plugin!
#fix this eww #fix this eww
options = Compass.sass_engine_options.extend Compass::SassExtensions::Functions::Sprites::VariableReader options = Compass.sass_engine_options.extend Compass::SassExtensions::Functions::Sprites::VariableReader
@map = Compass::SpriteMap.new("selectors/*.png", options) @map = Compass::SpriteImporter.new("selectors/*.png", options)
@base = Compass::SassExtensions::Sprites::Base.new(@map.sprite_names.map{|n| "selectors/#{n}.png"}, @map.path, 'selectors', @map.sass_engine, @map.options) @base = Compass::SassExtensions::Sprites::Base.new(@map.sprite_names.map{|n| "selectors/#{n}.png"}, @map.path, 'selectors', @map.sass_engine, @map.options)
end end

View File

@ -2,7 +2,7 @@ require 'spec_helper'
require 'fakefs/spec_helpers' require 'fakefs/spec_helpers'
require 'timecop' require 'timecop'
describe Compass::SpriteMap do describe Compass::SpriteImporter do
include FakeFS::SpecHelpers include FakeFS::SpecHelpers
let(:sprite_map) { self.class.describes.new(uri, options) } let(:sprite_map) { self.class.describes.new(uri, options) }

View File

@ -16,8 +16,8 @@ class SpritesBaseTest < Test::Unit::TestCase
end end
def setup_map def setup_map
@map = Compass::SpriteMap.new(:uri => "selectors/*.png", :options => @options) @importer = Compass::SpriteImporter.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) @base = Compass::SassExtensions::Sprites::Base.new(@importer.sprite_names.map{|n| "selectors/#{n}.png"}, @importer.path, @importer.name, @importer.sass_engine, @importer.options)
end end
def teardown def teardown
@ -29,7 +29,7 @@ class SpritesBaseTest < Test::Unit::TestCase
end end
it "should have the sprite names" do it "should have the sprite names" do
assert_equal @map.sprite_names, @base.sprite_names assert_equal @importer.sprite_names, @base.sprite_names
end end
it 'should have image filenames' do it 'should have image filenames' do

View File

@ -19,8 +19,8 @@ class SpritesImageTest < Test::Unit::TestCase
let(:sprite_name) { File.basename(sprite_filename, '.png') } let(:sprite_name) { File.basename(sprite_filename, '.png') }
def parent def parent
map = Compass::SpriteMap.new(:uri => "selectors/*.png", :options => options) importer = Compass::SpriteImporter.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) @parent ||= Compass::SassExtensions::Sprites::Base.new(importer.sprite_names.map{|n| "selectors/#{n}.png"}, importer.path, importer.name, importer.sass_engine, importer.options)
end end
let(:options) do let(:options) do