css_sprite 1.3.2 → 1.3.3
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/css_sprite.gemspec +1 -1
- data/lib/automatic.rb +2 -1
- data/lib/css_sprite/sprite.rb +15 -9
- data/spec/css_sprite/sprite_spec.rb +14 -8
- data/spec/spec_helper.rb +2 -2
- metadata +2 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.3.
|
1
|
+
1.3.3
|
data/css_sprite.gemspec
CHANGED
data/lib/automatic.rb
CHANGED
data/lib/css_sprite/sprite.rb
CHANGED
@@ -6,8 +6,8 @@ require 'enumerator'
|
|
6
6
|
class Sprite
|
7
7
|
|
8
8
|
def initialize(options={})
|
9
|
-
@image_path = File.join(Rails.root, 'public/images')
|
10
|
-
@stylesheet_path = File.join(Rails.root, 'public/stylesheets')
|
9
|
+
@image_path = File.expand_path(File.join(Rails.root, 'public/images'))
|
10
|
+
@stylesheet_path = File.expand_path(File.join(Rails.root, 'public/stylesheets'))
|
11
11
|
@todo = {}
|
12
12
|
|
13
13
|
if File.exist?(File.join(Rails.root, 'config/css_sprite.yml'))
|
@@ -67,13 +67,13 @@ class Sprite
|
|
67
67
|
span = 5
|
68
68
|
unless sources.empty?
|
69
69
|
dest_image = get_image(sources.shift)
|
70
|
-
results << image_properties(dest_image).merge(:x => 0, :y => 0)
|
70
|
+
results << image_properties(dest_image, directory).merge(:x => 0, :y => 0)
|
71
71
|
sources.each do |source|
|
72
72
|
source_image = get_image(source)
|
73
73
|
gravity = Magick::SouthGravity
|
74
74
|
x = 0
|
75
75
|
y = dest_image.rows + span
|
76
|
-
results << image_properties(source_image).merge(:x => x, :y => y)
|
76
|
+
results << image_properties(source_image, directory).merge(:x => x, :y => y)
|
77
77
|
dest_image = composite_images(dest_image, source_image, x, y)
|
78
78
|
end
|
79
79
|
dest_image.image_type = Magick::PaletteMatteType
|
@@ -104,7 +104,7 @@ class Sprite
|
|
104
104
|
f.print " \{\n background: url('/images/#{dest_image_name}?#{Time.now.to_i}') no-repeat;\n\}\n"
|
105
105
|
|
106
106
|
results.each do |result|
|
107
|
-
f.print "
|
107
|
+
f.print "#{class_name(result[:name])} \{"
|
108
108
|
f.print " background-position: #{-result[:x]}px #{-result[:y]}px;"
|
109
109
|
f.print " width: #{result[:width]}px;"
|
110
110
|
f.print " height: #{result[:height]}px;"
|
@@ -137,7 +137,7 @@ class Sprite
|
|
137
137
|
f.print " \n background: url('/images/#{dest_image_name}?#{Time.now.to_i}') no-repeat\n"
|
138
138
|
|
139
139
|
results.each do |result|
|
140
|
-
f.print "
|
140
|
+
f.print "#{class_name(result[:name])}\n"
|
141
141
|
f.print " background-position: #{-result[:x]}px #{-result[:y]}px\n"
|
142
142
|
f.print " width: #{result[:width]}px\n"
|
143
143
|
f.print " height: #{result[:height]}px\n"
|
@@ -152,11 +152,15 @@ class Sprite
|
|
152
152
|
class_names = []
|
153
153
|
results = results.select { |result| result[:name] =~ %r|#{options[:suffix]}$| } if options[:suffix]
|
154
154
|
results.each_slice(options[:count_per_line]) do |batch_results|
|
155
|
-
class_names << batch_results.collect { |result|
|
155
|
+
class_names << batch_results.collect { |result| class_name(result[:name]) }.join(', ')
|
156
156
|
end
|
157
157
|
class_names
|
158
158
|
end
|
159
159
|
|
160
|
+
def class_name(name)
|
161
|
+
".#{name.gsub('/', ' .')}"
|
162
|
+
end
|
163
|
+
|
160
164
|
def all_images(directory)
|
161
165
|
images = []
|
162
166
|
Find.find(directory) do |path|
|
@@ -196,8 +200,10 @@ class Sprite
|
|
196
200
|
Magick::Image::read(image_filename).first
|
197
201
|
end
|
198
202
|
|
199
|
-
def image_properties(image)
|
200
|
-
|
203
|
+
def image_properties(image, directory)
|
204
|
+
directory_length = directory.length + 1
|
205
|
+
extname_length = File.extname(image.filename).length
|
206
|
+
{:name => image.filename.slice(directory_length...-extname_length), :width => image.columns, :height => image.rows}
|
201
207
|
end
|
202
208
|
|
203
209
|
end
|
@@ -3,6 +3,7 @@ require File.dirname(__FILE__) + '/../spec_helper'
|
|
3
3
|
describe Sprite do
|
4
4
|
before(:all) do
|
5
5
|
@sprite = Sprite.new
|
6
|
+
@directory_path = File.join(IMAGE_PATH, 'css_sprite')
|
6
7
|
end
|
7
8
|
|
8
9
|
describe "build" do
|
@@ -94,24 +95,29 @@ describe Sprite do
|
|
94
95
|
|
95
96
|
describe "image_properties" do
|
96
97
|
it "should get image properties" do
|
97
|
-
image = @sprite.get_image(File.join(
|
98
|
-
@sprite.image_properties(image).should == {:name => 'gmail_logo', :width => 103, :height => 36}
|
98
|
+
image = @sprite.get_image(File.join(@directory_path, 'gmail_logo.png'))
|
99
|
+
@sprite.image_properties(image, @directory_path).should == {:name => 'gmail_logo', :width => 103, :height => 36}
|
100
|
+
end
|
101
|
+
|
102
|
+
it "should get a image with parent" do
|
103
|
+
image = @sprite.get_image(File.join(@directory_path, 'icons/twitter_icon.png'))
|
104
|
+
@sprite.image_properties(image, @directory_path).should == {:name => 'icons/twitter_icon', :width => 14, :height => 14}
|
99
105
|
end
|
100
106
|
end
|
101
107
|
|
102
108
|
describe "composite_images" do
|
103
109
|
it "should composite two images into one horizontally" do
|
104
|
-
image1 = @sprite.get_image(File.join(
|
105
|
-
image2 = @sprite.get_image(File.join(
|
110
|
+
image1 = @sprite.get_image(File.join(@directory_path, 'gmail_logo.png'))
|
111
|
+
image2 = @sprite.get_image(File.join(@directory_path, 'hotmail_logo.png'))
|
106
112
|
image = @sprite.composite_images(image1, image2, image1.columns, 0)
|
107
|
-
@sprite.image_properties(image).should == {:name =>
|
113
|
+
@sprite.image_properties(image, @directory_path).should == {:name => nil, :width => 206, :height => 36}
|
108
114
|
end
|
109
115
|
|
110
116
|
it "should composite two images into one verically" do
|
111
|
-
image1 = @sprite.get_image(File.join(
|
112
|
-
image2 = @sprite.get_image(File.join(
|
117
|
+
image1 = @sprite.get_image(File.join(@directory_path, 'gmail_logo.png'))
|
118
|
+
image2 = @sprite.get_image(File.join(@directory_path, 'hotmail_logo.png'))
|
113
119
|
image = @sprite.composite_images(image1, image2, 0, image1.rows)
|
114
|
-
@sprite.image_properties(image).should == {:name =>
|
120
|
+
@sprite.image_properties(image, @directory_path).should == {:name => nil, :width => 103, :height => 72}
|
115
121
|
end
|
116
122
|
end
|
117
123
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -8,6 +8,6 @@ class Rails
|
|
8
8
|
end
|
9
9
|
end
|
10
10
|
|
11
|
-
IMAGE_PATH = File.join(Rails.root, 'public/images')
|
12
|
-
STYLESHEET_PATH = File.join(Rails.root, 'public/stylesheets')
|
11
|
+
IMAGE_PATH = File.expand_path(File.join(Rails.root, 'public/images'))
|
12
|
+
STYLESHEET_PATH = File.expand_path(File.join(Rails.root, 'public/stylesheets'))
|
13
13
|
require File.join(File.dirname(__FILE__), '/../lib/css_sprite.rb')
|