css_sprite 1.2.0 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.textile +63 -43
- data/Rakefile +1 -0
- data/VERSION +1 -1
- data/css_sprite.gemspec +8 -1
- data/lib/automatic.rb +15 -0
- data/lib/css_sprite/sprite.rb +164 -42
- data/lib/css_sprite.rb +2 -1
- data/spec/css_sprite/sprite_spec.rb +89 -12
- data/spec/public/images/css_sprite/.DS_Store +0 -0
- data/spec/public/images/css_sprite/gmail_logo.png +0 -0
- data/spec/public/images/css_sprite/hotmail_logo.png +0 -0
- data/spec/public/images/css_sprite/icons/facebook_icon.png +0 -0
- data/spec/public/images/css_sprite/icons/twitter_icon.png +0 -0
- data/spec/public/images/css_sprite/not_image.txt +0 -0
- data/spec/spec_helper.rb +8 -1
- data/tasks/css_sprite_tasks.rake +50 -1
- metadata +9 -2
data/README.textile
CHANGED
@@ -2,29 +2,22 @@ h1. css_sprite
|
|
2
2
|
|
3
3
|
A rails plugin/gem to generate css sprite images automatically.
|
4
4
|
|
5
|
-
|
5
|
+
*************************************************************************
|
6
6
|
|
7
|
-
h2.
|
7
|
+
h2. Notice
|
8
8
|
|
9
|
-
|
10
|
-
<pre><code>
|
11
|
-
sudo gem install rmagick
|
12
|
-
</code></pre>
|
9
|
+
I have rewritten the plugin. Please check out the gem version >= 1.3.0
|
13
10
|
|
14
|
-
|
11
|
+
**************************************************************************
|
15
12
|
|
16
|
-
|
17
|
-
sudo port install libxml2
|
18
|
-
sudo port install ImageMagick
|
19
|
-
</code></pre>
|
13
|
+
h2. Install
|
20
14
|
|
21
|
-
|
15
|
+
css_sprite is dependent on the RMagick gem, please install it first.
|
22
16
|
|
23
17
|
|
24
18
|
install it as a gem:
|
25
19
|
|
26
20
|
<pre><code>
|
27
|
-
sudo gem sources -a http://gemcutter.org
|
28
21
|
sudo gem install css_sprite
|
29
22
|
</code></pre>
|
30
23
|
|
@@ -37,52 +30,79 @@ script/plugin install git://github.com/flyerhzm/css_sprite.git
|
|
37
30
|
|
38
31
|
h2. Configuration
|
39
32
|
|
40
|
-
|
33
|
+
There is no need to create a configuration if you do not use sass and you do not do any customization.
|
34
|
+
|
35
|
+
|
36
|
+
You can create <code>config/css_sprite.yml</code> to define the customization.
|
37
|
+
|
38
|
+
For css
|
41
39
|
<pre><code>
|
42
|
-
|
43
|
-
|
44
|
-
-
|
45
|
-
|
46
|
-
|
47
|
-
-
|
48
|
-
|
49
|
-
|
50
|
-
|
40
|
+
suffix:
|
41
|
+
button: |
|
42
|
+
text-indent: -9999px;
|
43
|
+
display: block;
|
44
|
+
cursor: pointer;
|
45
|
+
font-size: 0;
|
46
|
+
line-height: 15px;
|
47
|
+
border: 0;
|
48
|
+
outline: 0;
|
49
|
+
|
50
|
+
icon: |
|
51
|
+
text-indent: -9999px;
|
52
|
+
cursor: pointer;
|
51
53
|
</code></pre>
|
52
54
|
|
53
|
-
|
54
|
-
<code>
|
55
|
-
|
56
|
-
|
57
|
-
|
55
|
+
For sass
|
56
|
+
<pre><code>
|
57
|
+
engine: sass
|
58
|
+
suffix:
|
59
|
+
button: |
|
60
|
+
text-indent: -9999px
|
61
|
+
display: block
|
62
|
+
cursor: pointer
|
63
|
+
font-size: 0
|
64
|
+
line-height: 15px
|
65
|
+
border: 0
|
66
|
+
outline: 0
|
67
|
+
|
68
|
+
icon: |
|
69
|
+
text-indent: -9999px
|
70
|
+
cursor: pointer
|
71
|
+
</code></pre>
|
58
72
|
|
59
|
-
|
73
|
+
<code>engine</code> defines css (default) or sass to generate.
|
74
|
+
<code>suffix</code> defines the customization styles for specified images.
|
75
|
+
The customization above means if your image's suffix is button (e.g. post_button.png), it contains the additional style with (outline: 0 and so on),
|
76
|
+
if your image's suffix is icon (e.g. twitter_icon.png), it contains the additional style with (text-indent: -9999px and so on)
|
60
77
|
|
61
78
|
**************************************************************************
|
62
79
|
|
63
80
|
h2. Usage
|
64
81
|
|
65
|
-
|
82
|
+
1. create a <code>css_sprite</code> directory or css_sprite suffixed directory (e.g. widget_css_sprite) under <code>public/images</code>
|
83
|
+
|
84
|
+
2. define <code>config/css_sprite.yml</code> or not
|
85
|
+
|
86
|
+
3. start css_sprite server <code>rake css_sprite:start</code>
|
87
|
+
|
88
|
+
4. put any images you want to do css sprite into the css_sprite directory
|
89
|
+
|
90
|
+
5. then css_sprite image will be automatically generated and css_sprite.css or css_sprite.sass are generated too.
|
91
|
+
|
92
|
+
****************************************************************************
|
93
|
+
|
94
|
+
h2. Task
|
95
|
+
|
66
96
|
<pre><code>
|
67
97
|
require 'css_sprite'
|
68
98
|
</code></pre>
|
69
99
|
if you use it as a plugin, ignore the step above.
|
70
100
|
|
71
101
|
then just run rake task:
|
72
|
-
<
|
73
|
-
rake css_sprite:
|
74
|
-
</code
|
102
|
+
<code>rake css_sprite:start</code>, start css_sprite server
|
103
|
+
<code>rake css_sprite:stop</code>, stop css_sprite server
|
104
|
+
<code>rake css_sprite:build</code>, build css_sprite once
|
75
105
|
|
76
|
-
the result css is generated at <code>public/stylesheets/css_sprite.css</code>
|
77
|
-
<pre><code>
|
78
|
-
/* do not touch - generated through 'rake css_sprite:build' */
|
79
|
-
.v-forum_icon_vertical { background: url('/images/forum_icon_vertical.gif?1265358559') no-repeat; }
|
80
|
-
.v-good_topic { background-position: 0px 0px; width: 20px; height: 19px; }
|
81
|
-
.v-mid_topic { background-position: 0px -29px; width: 20px; height: 19px; }
|
82
|
-
.v-sticky_topic { background-position: 0px -58px; width: 19px; height: 18px; }
|
83
|
-
.v-unread_topic { background-position: 0px -86px; width: 19px; height: 18px; }
|
84
|
-
.v-plectix_logo { background-position: 0px -114px; width: 125px; height: 47px; }
|
85
|
-
</code></pre>
|
86
106
|
|
87
107
|
**************************************************************************
|
88
108
|
|
data/Rakefile
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.
|
1
|
+
1.3.0
|
data/css_sprite.gemspec
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{css_sprite}
|
8
|
-
s.version = "1.
|
8
|
+
s.version = "1.3.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Richard Huang"]
|
@@ -21,9 +21,16 @@ Gem::Specification.new do |s|
|
|
21
21
|
"Rakefile",
|
22
22
|
"VERSION",
|
23
23
|
"css_sprite.gemspec",
|
24
|
+
"lib/automatic.rb",
|
24
25
|
"lib/css_sprite.rb",
|
25
26
|
"lib/css_sprite/sprite.rb",
|
26
27
|
"spec/css_sprite/sprite_spec.rb",
|
28
|
+
"spec/public/images/css_sprite/.DS_Store",
|
29
|
+
"spec/public/images/css_sprite/gmail_logo.png",
|
30
|
+
"spec/public/images/css_sprite/hotmail_logo.png",
|
31
|
+
"spec/public/images/css_sprite/icons/facebook_icon.png",
|
32
|
+
"spec/public/images/css_sprite/icons/twitter_icon.png",
|
33
|
+
"spec/public/images/css_sprite/not_image.txt",
|
27
34
|
"spec/spec.opts",
|
28
35
|
"spec/spec_helper.rb",
|
29
36
|
"tasks/css_sprite_tasks.rake"
|
data/lib/automatic.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require File.join(File.dirname(__FILE__), 'css_sprite/sprite.rb')
|
3
|
+
|
4
|
+
# We need Rails.root, but not require 'rails', so hack it
|
5
|
+
class Rails
|
6
|
+
def self.root
|
7
|
+
Dir.pwd
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
loop do
|
12
|
+
sleep 1
|
13
|
+
|
14
|
+
Sprite.new.check
|
15
|
+
end
|
data/lib/css_sprite/sprite.rb
CHANGED
@@ -1,64 +1,186 @@
|
|
1
|
+
require 'find'
|
1
2
|
require 'RMagick'
|
3
|
+
require 'yaml'
|
2
4
|
|
3
|
-
class Sprite
|
4
|
-
CONFIG_PATH = RAILS_ROOT + '/config/'
|
5
|
-
IMAGE_PATH = RAILS_ROOT + '/public/images/'
|
6
|
-
PUBLIC_PATH = RAILS_ROOT + '/public/stylesheets/'
|
5
|
+
class Sprite
|
7
6
|
|
8
|
-
def initialize
|
9
|
-
@
|
7
|
+
def initialize(options={})
|
8
|
+
@image_path = File.join(Rails.root, 'public/images')
|
9
|
+
@stylesheet_path = File.join(Rails.root, 'public/stylesheets')
|
10
|
+
@todo = {}
|
11
|
+
|
12
|
+
if File.exist?(File.join(Rails.root, 'config/css_sprite.yml'))
|
13
|
+
@config = YAML::load_file(File.join(Rails.root, 'config/css_sprite.yml'))
|
14
|
+
else
|
15
|
+
@config = options
|
16
|
+
end
|
10
17
|
end
|
11
18
|
|
12
19
|
def build
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
20
|
+
directories = css_sprite_directories
|
21
|
+
directories.each { |directory| output_image(directory) }
|
22
|
+
output_stylesheet
|
23
|
+
end
|
24
|
+
|
25
|
+
def check
|
26
|
+
directories = css_sprite_directories
|
27
|
+
directories.each do |directory|
|
28
|
+
if expire?(directory)
|
29
|
+
output_image(directory)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
output_stylesheet
|
33
|
+
end
|
34
|
+
|
35
|
+
def expire?(directory)
|
36
|
+
if sass?
|
37
|
+
stylesheet_path = dest_sass_path(directory)
|
38
|
+
else
|
39
|
+
stylesheet_path = dest_css_path(directory)
|
40
|
+
end
|
41
|
+
!File.exist?(stylesheet_path) or File.new(directory).mtime > File.new(stylesheet_path).mtime
|
19
42
|
end
|
20
43
|
|
21
|
-
def
|
44
|
+
def output_stylesheet
|
45
|
+
if sass?
|
46
|
+
output_sass
|
47
|
+
else
|
48
|
+
output_css
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def sass?
|
53
|
+
@config['engine'] == 'sass'
|
54
|
+
end
|
55
|
+
|
56
|
+
def css_sprite_directories
|
57
|
+
Dir.entries(@image_path).collect do |d|
|
58
|
+
File.join(@image_path, d) if File.directory?(File.join(@image_path, d)) and d =~ /css_sprite$/
|
59
|
+
end.compact
|
60
|
+
end
|
61
|
+
|
62
|
+
def output_image(directory)
|
22
63
|
results = []
|
23
|
-
sources =
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
x = dest_image.columns + span
|
32
|
-
y = 0
|
33
|
-
else
|
64
|
+
sources = all_images(directory)
|
65
|
+
dest_image_path = dest_image_path(directory)
|
66
|
+
span = 5
|
67
|
+
unless sources.empty?
|
68
|
+
dest_image = get_image(sources.shift)
|
69
|
+
results << image_properties(dest_image).merge(:x => 0, :y => 0)
|
70
|
+
sources.each do |source|
|
71
|
+
source_image = get_image(source)
|
34
72
|
gravity = Magick::SouthGravity
|
35
73
|
x = 0
|
36
74
|
y = dest_image.rows + span
|
75
|
+
results << image_properties(source_image).merge(:x => x, :y => y)
|
76
|
+
dest_image = composite_images(dest_image, source_image, x, y)
|
37
77
|
end
|
38
|
-
|
39
|
-
dest_image = composite_images(dest_image, source_image, x, y)
|
78
|
+
dest_image.write(dest_image_path)
|
40
79
|
end
|
41
|
-
@
|
42
|
-
dest_image.write(IMAGE_PATH + dest)
|
80
|
+
@todo[directory] = results
|
43
81
|
end
|
44
|
-
|
82
|
+
|
45
83
|
def output_css
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
84
|
+
@todo.each do |directory, results|
|
85
|
+
unless results.empty?
|
86
|
+
dest_image_name = dest_image_name(directory)
|
87
|
+
dest_css_path = dest_css_path(directory)
|
88
|
+
File.open(dest_css_path, 'w') do |f|
|
89
|
+
if @config['suffix']
|
90
|
+
@config['suffix'].each do |key, value|
|
91
|
+
cns = class_names(results, :suffix => key)
|
92
|
+
unless cns.empty?
|
93
|
+
f.print cns.join(",\n")
|
94
|
+
f.print " \{\n"
|
95
|
+
f.print value.split("\n").collect { |text| " " + text }.join("\n")
|
96
|
+
f.print "\}\n"
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
f.print class_names(results).join(",\n")
|
102
|
+
f.print " \{\n background: url('/images/#{dest_image_name}?#{Time.now.to_i}') no-repeat;\n\}\n"
|
103
|
+
|
104
|
+
results.each do |result|
|
105
|
+
f.print ".#{result[:name]} \{"
|
106
|
+
f.print " background-position: #{-result[:x]}px #{-result[:y]}px;"
|
107
|
+
f.print " width: #{result[:width]}px;"
|
108
|
+
f.print " height: #{result[:height]}px;"
|
109
|
+
f.print " \}\n"
|
110
|
+
end
|
57
111
|
end
|
58
112
|
end
|
59
113
|
end
|
60
114
|
end
|
61
115
|
|
116
|
+
def output_sass
|
117
|
+
@todo.each do |directory, results|
|
118
|
+
unless results.empty?
|
119
|
+
dest_image_name = dest_image_name(directory)
|
120
|
+
dest_sass_path = dest_sass_path(directory)
|
121
|
+
File.open(dest_sass_path, 'w') do |f|
|
122
|
+
if @config['suffix']
|
123
|
+
@config['suffix'].each do |key, value|
|
124
|
+
cns = class_names(results, :suffix => key)
|
125
|
+
unless cns.empty?
|
126
|
+
f.print cns.join(",\n")
|
127
|
+
f.print "\n"
|
128
|
+
f.print value.split("\n").collect { |text| " " + text }.join("\n")
|
129
|
+
f.print "\n"
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
f.print class_names(results).join(",\n")
|
135
|
+
f.print " \n background: url('/images/#{dest_image_name}?#{Time.now.to_i}') no-repeat\n"
|
136
|
+
|
137
|
+
results.each do |result|
|
138
|
+
f.print ".#{result[:name]}\n"
|
139
|
+
f.print " background-position: #{-result[:x]}px #{-result[:y]}px\n"
|
140
|
+
f.print " width: #{result[:width]}px\n"
|
141
|
+
f.print " height: #{result[:height]}px\n"
|
142
|
+
end
|
143
|
+
end
|
144
|
+
end
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
def class_names(results, options={})
|
149
|
+
options = {:count_per_line => 5}.merge(options)
|
150
|
+
class_names = []
|
151
|
+
results = results.select { |result| result[:name] =~ %r|#{options[:suffix]}$| } if options[:suffix]
|
152
|
+
results.each_slice(options[:count_per_line]) do |batch_results|
|
153
|
+
class_names << batch_results.collect { |result| ".#{result[:name]}" }.join(', ')
|
154
|
+
end
|
155
|
+
class_names
|
156
|
+
end
|
157
|
+
|
158
|
+
def all_images(directory)
|
159
|
+
images = []
|
160
|
+
Find.find(directory) do |path|
|
161
|
+
if path =~ /\.(png|gif|jpg|jpeg)$/
|
162
|
+
images << path
|
163
|
+
end
|
164
|
+
end
|
165
|
+
images
|
166
|
+
end
|
167
|
+
|
168
|
+
def dest_image_path(directory)
|
169
|
+
directory + ".png"
|
170
|
+
end
|
171
|
+
|
172
|
+
def dest_image_name(directory)
|
173
|
+
File.basename(directory) + ".png"
|
174
|
+
end
|
175
|
+
|
176
|
+
def dest_css_path(directory)
|
177
|
+
File.join(@stylesheet_path, File.basename(directory) + '.css')
|
178
|
+
end
|
179
|
+
|
180
|
+
def dest_sass_path(directory)
|
181
|
+
File.join(@stylesheet_path, 'sass', File.basename(directory) + '.sass')
|
182
|
+
end
|
183
|
+
|
62
184
|
def composite_images(dest_image, src_image, x, y)
|
63
185
|
width = [src_image.columns + x, dest_image.columns].max
|
64
186
|
height = [src_image.rows + y, dest_image.rows].max
|
@@ -69,11 +191,11 @@ class Sprite
|
|
69
191
|
end
|
70
192
|
|
71
193
|
def get_image(image_filename)
|
72
|
-
|
194
|
+
Magick::Image::read(image_filename).first
|
73
195
|
end
|
74
196
|
|
75
197
|
def image_properties(image)
|
76
|
-
{:name => File.basename(image.filename
|
198
|
+
{:name => File.basename(image.filename, File.extname(image.filename)), :width => image.columns, :height => image.rows}
|
77
199
|
end
|
78
200
|
|
79
201
|
end
|
data/lib/css_sprite.rb
CHANGED
@@ -5,32 +5,109 @@ describe Sprite do
|
|
5
5
|
@sprite = Sprite.new
|
6
6
|
end
|
7
7
|
|
8
|
-
|
8
|
+
describe "build" do
|
9
|
+
it "should build css_sprite image and css" do
|
10
|
+
@sprite.build
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should build css_sprite image and sass" do
|
14
|
+
Sprite.new(:engine => :sass).build
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe "css_sprite_directories" do
|
19
|
+
it "should read two direcoties" do
|
20
|
+
expected_directories = [File.join(IMAGE_PATH, 'another_css_sprite'),
|
21
|
+
File.join(IMAGE_PATH, 'css_sprite')]
|
22
|
+
@sprite.css_sprite_directories.should == expected_directories
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
describe "output_image" do
|
27
|
+
it "should output a css_sprite image for a directory" do
|
28
|
+
@sprite.output_image(File.join(IMAGE_PATH, 'css_sprite'))
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
describe "all_images" do
|
33
|
+
it "should read all images from a directory" do
|
34
|
+
expected_images = [File.join(IMAGE_PATH, 'css_sprite/icons/twitter_icon.png'),
|
35
|
+
File.join(IMAGE_PATH, 'css_sprite/icons/facebook_icon.png'),
|
36
|
+
File.join(IMAGE_PATH, 'css_sprite/hotmail_logo.png'),
|
37
|
+
File.join(IMAGE_PATH, 'css_sprite/gmail_logo.png')]
|
38
|
+
@sprite.all_images(File.join(IMAGE_PATH, 'css_sprite')).should == expected_images
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
describe "class_names" do
|
43
|
+
before(:all) do
|
44
|
+
@results = [{:name => 'gmail_logo'}, {:name => 'hotmail_logo'}, {:name => 'yahoo_logo'},
|
45
|
+
{:name => 'gmail_button'}, {:name => 'hotmail_button'}, {:name => 'yahoo_button'}]
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should get class_names with default options" do
|
49
|
+
@sprite.class_names(@results).should == [".gmail_logo, .hotmail_logo, .yahoo_logo, .gmail_button, .hotmail_button", ".yahoo_button"]
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should get class_names with 3 count_per_line" do
|
53
|
+
@sprite.class_names(@results, :count_per_line => 3).should == [".gmail_logo, .hotmail_logo, .yahoo_logo", ".gmail_button, .hotmail_button, .yahoo_button"]
|
54
|
+
end
|
55
|
+
|
56
|
+
it "should get specified class_names with suffix" do
|
57
|
+
@sprite.class_names(@results, :suffix => 'logo').should == [".gmail_logo, .hotmail_logo, .yahoo_logo"]
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
describe "dest_image_path" do
|
62
|
+
it "should get css_sprite image path for a directory" do
|
63
|
+
@sprite.dest_image_path(File.join(IMAGE_PATH, 'css_sprite')).should == File.join(IMAGE_PATH, 'css_sprite.png')
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
describe "dest_image_name" do
|
68
|
+
it "should get css_sprite image name for a directory" do
|
69
|
+
@sprite.dest_image_name(File.join(IMAGE_PATH, 'css_sprite')).should == 'css_sprite.png'
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
describe "dest_css_path" do
|
74
|
+
it "should get css_sprite css path for a directory" do
|
75
|
+
@sprite.dest_css_path(File.join(IMAGE_PATH, 'css_sprite')).should == File.join(STYLESHEET_PATH, 'css_sprite.css')
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
describe "dest_sass_path" do
|
80
|
+
it "should get sass_sprite css path for a directory" do
|
81
|
+
@sprite.dest_sass_path(File.join(IMAGE_PATH, 'css_sprite')).should == File.join(STYLESHEET_PATH, 'sass', 'css_sprite.sass')
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
describe "get_image" do
|
9
86
|
it "should get a image" do
|
10
|
-
@sprite.get_image(File.join(
|
87
|
+
@sprite.get_image(File.join(IMAGE_PATH, 'css_sprite/gmail_logo.png')).class.should == Magick::Image
|
11
88
|
end
|
12
89
|
end
|
13
90
|
|
14
|
-
|
91
|
+
describe "image_properties" do
|
15
92
|
it "should get image properties" do
|
16
|
-
image = @sprite.get_image(File.join(
|
17
|
-
@sprite.image_properties(image).should == {:name => '
|
93
|
+
image = @sprite.get_image(File.join(IMAGE_PATH, 'css_sprite/gmail_logo.png'))
|
94
|
+
@sprite.image_properties(image).should == {:name => 'gmail_logo', :width => 103, :height => 36}
|
18
95
|
end
|
19
96
|
end
|
20
97
|
|
21
|
-
|
98
|
+
describe "composite_images" do
|
22
99
|
it "should composite two images into one horizontally" do
|
23
|
-
image1 = @sprite.get_image(File.join(
|
24
|
-
image2 = @sprite.get_image(File.join(
|
100
|
+
image1 = @sprite.get_image(File.join(IMAGE_PATH, 'css_sprite/gmail_logo.png'))
|
101
|
+
image2 = @sprite.get_image(File.join(IMAGE_PATH, 'css_sprite/hotmail_logo.png'))
|
25
102
|
image = @sprite.composite_images(image1, image2, image1.columns, 0)
|
26
|
-
@sprite.image_properties(image).should == {:name =>
|
103
|
+
@sprite.image_properties(image).should == {:name => "", :width => 206, :height => 36}
|
27
104
|
end
|
28
105
|
|
29
106
|
it "should composite two images into one verically" do
|
30
|
-
image1 = @sprite.get_image(File.join(
|
31
|
-
image2 = @sprite.get_image(File.join(
|
107
|
+
image1 = @sprite.get_image(File.join(IMAGE_PATH, 'css_sprite/gmail_logo.png'))
|
108
|
+
image2 = @sprite.get_image(File.join(IMAGE_PATH, 'css_sprite/hotmail_logo.png'))
|
32
109
|
image = @sprite.composite_images(image1, image2, 0, image1.rows)
|
33
|
-
@sprite.image_properties(image).should == {:name =>
|
110
|
+
@sprite.image_properties(image).should == {:name => "", :width => 103, :height => 72}
|
34
111
|
end
|
35
112
|
end
|
36
113
|
end
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
File without changes
|
data/spec/spec_helper.rb
CHANGED
@@ -2,5 +2,12 @@ require 'rubygems'
|
|
2
2
|
require 'spec/autorun'
|
3
3
|
require 'date'
|
4
4
|
|
5
|
-
|
5
|
+
class Rails
|
6
|
+
def self.root
|
7
|
+
File.dirname(__FILE__)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
IMAGE_PATH = File.join(Rails.root, 'public/images')
|
12
|
+
STYLESHEET_PATH = File.join(Rails.root, 'public/stylesheets')
|
6
13
|
require File.join(File.dirname(__FILE__), '/../lib/css_sprite.rb')
|
data/tasks/css_sprite_tasks.rake
CHANGED
@@ -1,7 +1,56 @@
|
|
1
1
|
namespace :css_sprite do
|
2
|
-
desc "
|
2
|
+
desc "build css sprite image once"
|
3
3
|
task :build do
|
4
4
|
require File.join(File.dirname(__FILE__), '../lib/css_sprite/sprite.rb')
|
5
5
|
Sprite.new.build
|
6
6
|
end
|
7
|
+
|
8
|
+
desc "start css sprite server"
|
9
|
+
task :start do
|
10
|
+
if RUBY_PLATFORM.include?('mswin32')
|
11
|
+
# exec "start "
|
12
|
+
# puts "css_sprite server started sucessfully."
|
13
|
+
puts "not support windows yet."
|
14
|
+
else
|
15
|
+
file_path = "#{Rails.root}/tmp/pids/css_sprite.pid"
|
16
|
+
if File.exists?(file_path)
|
17
|
+
puts "css_sprite server is started. I haven't done anything."
|
18
|
+
else
|
19
|
+
pid = fork do
|
20
|
+
automatic_script = File.join(File.dirname(__FILE__), '..', 'lib', 'automatic.rb')
|
21
|
+
exec "ruby #{automatic_script}"
|
22
|
+
end
|
23
|
+
|
24
|
+
sleep(1)
|
25
|
+
File.open("#{Rails.root}/tmp/pids/css_sprite.pid", "w") { |f| f << pid }
|
26
|
+
puts "css_sprite server started sucessfully."
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
desc "stop css sprite server"
|
32
|
+
task :stop do
|
33
|
+
if RUBY_PLATFORM.include?('mswin32')
|
34
|
+
# exec "taskkill "
|
35
|
+
# puts "css_sprite server shutdown sucessfully."
|
36
|
+
puts "not support windows yet."
|
37
|
+
else
|
38
|
+
file_path = "#{Rails.root}/tmp/pids/css_sprite.pid"
|
39
|
+
if File.exists?(file_path)
|
40
|
+
fork do
|
41
|
+
File.open(file_path, "r") do |f|
|
42
|
+
pid = f.readline
|
43
|
+
Process.kill('TERM', pid.to_i)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
Process.wait
|
48
|
+
File.unlink(file_path)
|
49
|
+
puts "css_sprite server shutdown sucessfully."
|
50
|
+
else
|
51
|
+
puts "css_sprite server is not running. I haven't done anything."
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
7
56
|
end
|
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 1
|
7
|
-
-
|
7
|
+
- 3
|
8
8
|
- 0
|
9
|
-
version: 1.
|
9
|
+
version: 1.3.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Richard Huang
|
@@ -43,9 +43,16 @@ files:
|
|
43
43
|
- Rakefile
|
44
44
|
- VERSION
|
45
45
|
- css_sprite.gemspec
|
46
|
+
- lib/automatic.rb
|
46
47
|
- lib/css_sprite.rb
|
47
48
|
- lib/css_sprite/sprite.rb
|
48
49
|
- spec/css_sprite/sprite_spec.rb
|
50
|
+
- spec/public/images/css_sprite/.DS_Store
|
51
|
+
- spec/public/images/css_sprite/gmail_logo.png
|
52
|
+
- spec/public/images/css_sprite/hotmail_logo.png
|
53
|
+
- spec/public/images/css_sprite/icons/facebook_icon.png
|
54
|
+
- spec/public/images/css_sprite/icons/twitter_icon.png
|
55
|
+
- spec/public/images/css_sprite/not_image.txt
|
49
56
|
- spec/spec.opts
|
50
57
|
- spec/spec_helper.rb
|
51
58
|
- tasks/css_sprite_tasks.rake
|