sprite-factory 1.3.0 → 1.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -1,5 +1,5 @@
1
- Sprite Factory
2
- ==============
1
+ Sprite Factory (v1.4.1)
2
+ =======================
3
3
 
4
4
  The sprite factory is a ruby library that can be used to generate
5
5
  [CSS sprites](http://www.alistapart.com/articles/sprites). It combines
@@ -14,6 +14,7 @@ The library provides:
14
14
  * support for any stylesheet syntax, including [CSS](http://www.w3.org/Style/CSS/) and [Sass](http://sass-lang.com/).
15
15
  * support for any image library, including [RMagick](http://rmagick.rubyforge.org/) and [ChunkyPNG](https://github.com/wvanbergen/chunky_png).
16
16
  * support for pngcrush'n the generated image file
17
+ * compatible with Rails 3.1 asset pipeline
17
18
 
18
19
 
19
20
  Installation
@@ -73,19 +74,20 @@ to keep the markup semantic it is common to use an `img` tag with a dummy `src=s
73
74
  Customization
74
75
  =============
75
76
 
76
- Much of the behavior can be customized by overriding the following options:
77
-
78
- - `:output` - specify output location for generated files
79
- - `:layout` - specify layout algorithm (horizontal, vertical or packed)
80
- - `:style` - specify output style (css or sass)
81
- - `:library` - specify image library to use (rmagick or chunkypng)
82
- - `:selector` - specify custom css selector (see below)
83
- - `:csspath` - specify custom path for css image url (see below)
84
- - `:padding` - add padding to each sprite
85
- - `:width` - fix width of each sprite to a specific size
86
- - `:height` - fix height of each sprite to a specific size
87
- - `:pngcrush` - pngcrush the generated output image (if pngcrush is available)
88
- - `:nocss` - suppress generation of output css file (`run!` returns css content as a string instead)
77
+ Much of the behavior can be customized by overriding the following options:
78
+
79
+ - `:layout` - specify layout algorithm (horizontal, vertical or packed)
80
+ - `:style` - specify stylesheet syntax (css, scss or sass)
81
+ - `:library` - specify image library to use (rmagick or chunkypng)
82
+ - `:selector` - specify custom css selector (see below)
83
+ - `:csspath` - specify custom path for css image url (see below)
84
+ - `:output_image` - specify output location for generated image (default: <input folder>.png)
85
+ - `:output_style` - specify output location for generated stylesheet (default: <input folder>.<style>)
86
+ - `:pngcrush` - pngcrush the generated output image (if pngcrush is available)
87
+ - `:padding` - add padding to each sprite
88
+ - `:width` - fix width of each sprite to a specific size
89
+ - `:height` - fix height of each sprite to a specific size
90
+ - `:nocss` - suppress generation of output style file (`run!` returns css content as a string instead)
89
91
 
90
92
  Options can be passed as command line arguments to the `sf` script:
91
93
 
@@ -155,7 +157,17 @@ For most CDN's, you can prepend a simple string to the image name:
155
157
 
156
158
  # generates: url(http://s3.amazonaws.com/icons.png)
157
159
 
158
- For more control, you can provide a lambda function and generate your own paths:
160
+ For more control, a simple token replacement can be performed using the $IMAGE token. For example, to embed ERB
161
+ into the generated style file and let Rails generate the paths you can use:
162
+
163
+ SpriteFactory.run('images/icons',
164
+ :csspath => "<%= image_path('$IMAGE') %>")
165
+
166
+ # generates: url(<%= image_path('icons.png') %>)
167
+
168
+ >> _this assumes Rails will post-process the css file with ERB (e.g. using sprockets in the Rails 3.1 asset pipeline)_
169
+
170
+ For full control, you can provide a lambda function and generate your own paths:
159
171
 
160
172
  SpriteFactory.run('images/icons',
161
173
  :csspath => lambda{|image| image_path(image)})
@@ -192,10 +204,56 @@ value is a hash of image metadata that includes the following:
192
204
 
193
205
  (*NOTE*: the image coords can differ form the css sprite coords when padding or fixed width/height options are specified)
194
206
 
207
+ Using sprite-factory with the Rails 3.1 asset pipeline
208
+ ======================================================
209
+
210
+ The sprite-factory gem (>= v1.4.0) plays nice with the upcoming Rails 3.1 asset pipeline with a few simple steps:
211
+
212
+ Add the sprite-factory to your Gemfile, including your chosen image library dependency:
213
+
214
+ group :assets do
215
+ gem 'sprite-factory', '>= 1.4.0'
216
+ gem 'chunky_png'
217
+ end
218
+
219
+ Store your images in Rails 3.1 `app/assets/images` sub-folders, e.g
220
+
221
+ app/assets/images/avatars/*.png
222
+ app/assets/images/icons/*.png
223
+ ...
224
+
225
+ Create a Rake task for regenerating your sprites, e.g. in `lib/tasks/assets.rake`
226
+
227
+ require 'sprite_factory'
228
+
229
+ namespace :assets do
230
+ desc 'recreate sprite images and css'
231
+ task :resprite => :environment do
232
+ SpriteFactory.library = :chunkypng # use chunkypng as underlying image library
233
+ SpriteFactory.csspath = "<%= asset_path '$IMAGE' %>" # embed ERB into css file to be evaluated by asset pipeline
234
+ SpriteFactory.run!('app/assets/images/avatars', :output_style => 'app/assets/stylesheets/avatars.css.erb')
235
+ SpriteFactory.run!('app/assets/images/icons', :output_style => 'app/assets/stylesheets/icons.css.erb')
236
+ # ... etc ...
237
+ end
238
+ end
239
+
240
+ Run the rake task
241
+
242
+ bundle exec rake assets:resprite
243
+
244
+ Generates
245
+
246
+ * sprite images in `app/assets/images`
247
+ * sprite styles in `app/assets/stylesheets` - automatically picked up by the asset pipeline and included in your generated application.css
248
+
249
+ You can find out more here:
250
+
251
+ * [Sprite Factory 1.4.1 and the Rails Asset Pipeline](http://codeincomplete.com/posts/2011/8/6/sprite_factory_1_4_1/)
252
+
195
253
  Extending the Library
196
254
  =====================
197
255
 
198
- The sprite factory library can also be extended in a number of other ways.
256
+ The sprite factory library can be extended in a number of other ways.
199
257
 
200
258
  * provide a custom layout algorithm in the `SpriteFactory::Layout` module.
201
259
  * provide a custom style generator in the `SpriteFactory::Style` module.
data/RELEASE_NOTES.md CHANGED
@@ -1,4 +1,14 @@
1
1
 
2
+ August 5th 2011 - v1.4.1
3
+ ------------------------
4
+ * added support for `:style => :scss` to generate .scss file (even though content will be almost exactly same as .css style)
5
+ * deprecated `:output` option and replaced it with 2 new explicit `:output_image` and `:output_style` options
6
+ * updated RELEASE NOTES to include setup for use with Rails 3.1 asset pipeline
7
+
8
+ Auguest 5th 2011 - v1.4.0
9
+ -------------------------
10
+ * (not available)
11
+
2
12
  July 9th 2011 - v1.3.0
3
13
  ----------------------
4
14
 
data/bin/sf CHANGED
@@ -19,21 +19,23 @@ op.on("-v", "--version") do
19
19
  exit
20
20
  end
21
21
 
22
- output_help = "specify output location, without any extension"
23
- layout_help = "specify layout orientation ( horizontal, vertical, packed )"
24
- style_help = "specify output style format ( css, sass )"
25
- library_help = "specify image library to use ( rmagic, chunkypng )"
26
- selector_help = "specify custom selector to use for each css rule ( default: 'img.' )"
27
- csspath_help = "specify custom path to use for css image urls ( default: output file's basename )"
28
- pngcrush_help = "use pngcrush to optimize generated image"
29
-
30
- op.on("--output [PATH]", output_help) {|value| options[:output] = value }
31
- op.on("--layout [ORIENTATION]", layout_help) {|value| options[:layout] = value }
32
- op.on("--style [STYLE]", style_help) {|value| options[:style] = value }
33
- op.on("--library [LIBRARY]", library_help) {|value| options[:library] = value }
34
- op.on("--selector [SELECTOR]", selector_help) {|value| options[:selector] = value }
35
- op.on("--csspath [CSSPATH]", csspath_help) {|value| options[:csspath] = value }
36
- op.on("--pngcrush", pngcrush_help) {|value| options[:pngcrush] = value }
22
+ layout_help = "specify layout orientation ( horizontal, vertical, packed )"
23
+ style_help = "specify stylesheet syntax ( css, scss, sass )"
24
+ library_help = "specify image library to use ( rmagic, chunkypng )"
25
+ selector_help = "specify custom selector to use for each css rule ( default: 'img.' )"
26
+ csspath_help = "specify custom path to use for css image urls ( default: output image basename )"
27
+ output_image_help = "specify output location for generated image ( default: <input folder>.png )"
28
+ output_style_help = "specify output location for generated stylesheet ( default: <input folder>.<style>)"
29
+ pngcrush_help = "use pngcrush to optimize generated image"
30
+
31
+ op.on("--layout [ORIENTATION]", layout_help) {|value| options[:layout] = value }
32
+ op.on("--style [STYLE]", style_help) {|value| options[:style] = value }
33
+ op.on("--library [LIBRARY]", library_help) {|value| options[:library] = value }
34
+ op.on("--selector [SELECTOR]", selector_help) {|value| options[:selector] = value }
35
+ op.on("--csspath [CSSPATH]", csspath_help) {|value| options[:csspath] = value }
36
+ op.on("--output-image [PATH]", output_image_help) {|value| options[:output_image] = value }
37
+ op.on("--output-style [PATH]", output_style_help) {|value| options[:output_style] = value }
38
+ op.on("--pngcrush", pngcrush_help) {|value| options[:pngcrush] = value }
37
39
 
38
40
  begin
39
41
  op.parse!(ARGV)
@@ -2,7 +2,7 @@ module SpriteFactory
2
2
 
3
3
  #----------------------------------------------------------------------------
4
4
 
5
- VERSION = "1.3.0"
5
+ VERSION = "1.4.1"
6
6
  SUMMARY = "Automatic CSS sprite generator"
7
7
  DESCRIPTION = "Combines individual images from a directory into a single sprite image file and creates an appropriate CSS stylesheet"
8
8
  LIB = File.dirname(__FILE__)
@@ -30,8 +30,10 @@ module SpriteFactory
30
30
  raise RuntimeError, "unknown library #{library_name}" if !Library.respond_to?(library_name)
31
31
 
32
32
  raise RuntimeError, "input must be a single directory" if input.nil? || input.to_s.empty? || !File.directory?(input)
33
- raise RuntimeError, "no output file specified" if output.nil? || output.to_s.empty?
34
33
  raise RuntimeError, "no image files found" if image_files.empty?
34
+ raise RuntimeError, "no output file specified" if output.to_s.empty?
35
+ raise RuntimeError, "no output image file specified" if output_image_file.to_s.empty?
36
+ raise RuntimeError, "no output style file specified" if output_style_file.to_s.empty?
35
37
 
36
38
  raise RuntimeError, "set :width for fixed width, or :hpadding for horizontal padding, but not both." if width && !hpadding.zero?
37
39
  raise RuntimeError, "set :height for fixed height, or :vpadding for vertical padding, but not both." if height && !vpadding.zero?
@@ -64,10 +66,6 @@ module SpriteFactory
64
66
 
65
67
  private
66
68
 
67
- def output
68
- config[:output] || input
69
- end
70
-
71
69
  def selector
72
70
  config[:selector]
73
71
  end
@@ -100,16 +98,20 @@ module SpriteFactory
100
98
  config[:height]
101
99
  end
102
100
 
101
+ def output
102
+ config[:output] || input
103
+ end
104
+
103
105
  def output_image_file
104
- "#{output}.png" if output
106
+ config[:output_image] || "#{output}.png"
105
107
  end
106
108
 
107
109
  def output_style_file
108
- "#{output}.#{style_name}" if output and style_name
110
+ config[:output_style] || "#{output}.#{style_name}"
109
111
  end
110
112
 
111
113
  def nocss?
112
- config[:nocss] # set true if you dont want an output css file generated (e.g. you will take the #run! output and store it yourself)
114
+ config[:nocss] # set true if you dont want an output style file generated (e.g. you will take the #run! output and store it yourself)
113
115
  end
114
116
 
115
117
  def custom_style_file
@@ -128,7 +130,7 @@ module SpriteFactory
128
130
  File.join(custom, base) # allow custom path with simple prepend
129
131
  end
130
132
  else
131
- base # otherwise, just default to basename of the output image
133
+ base # otherwise, just default to basename of the output image file
132
134
  end
133
135
  end
134
136
 
@@ -17,6 +17,20 @@ module SpriteFactory
17
17
 
18
18
  #----------------------------------------------------------------------------
19
19
 
20
+ def self.scss(selector, name, attributes)
21
+ css(selector, name, attributes) # scss is a superset of css, but we dont actually need any of the extra bits, so just defer to the css generator instead
22
+ end
23
+
24
+ def self.scss_style(attributes)
25
+ css_style(attributes)
26
+ end
27
+
28
+ def self.scss_comment(comment)
29
+ css_comment(attributes)
30
+ end
31
+
32
+ #----------------------------------------------------------------------------
33
+
20
34
  def self.sass(selector, name, attributes)
21
35
  "#{selector}#{name}\n" + sass_style(attributes)
22
36
  end
@@ -19,10 +19,9 @@ Gem::Specification.new do |s|
19
19
  s.has_rdoc = false
20
20
  s.extra_rdoc_files = ["README.md"]
21
21
  s.rdoc_options = ["--charset=UTF-8"]
22
-
23
- s.files = `git ls-files `.split("\n")
24
- s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
25
- s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
26
- s.require_paths = ["lib"]
22
+ s.files = `git ls-files `.split("\n")
23
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
24
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
25
+ s.require_paths = ["lib"]
27
26
 
28
27
  end
data/test/runner_test.rb CHANGED
@@ -39,6 +39,16 @@ module SpriteFactory
39
39
  assert_equal(:css, r.style_name)
40
40
  assert_equal(:rmagick, r.library_name)
41
41
 
42
+ r = Runner.new(REGULAR_PATH, :output_image => "foo.png", :output_style => "bar.css.sass.erb")
43
+ assert_equal(REGULAR_PATH, r.input)
44
+ assert_equal(REGULAR_PATH, r.output)
45
+ assert_equal("foo.png", r.output_image_file)
46
+ assert_equal("bar.css.sass.erb", r.output_style_file)
47
+ assert_equal(REGULAR, r.image_files)
48
+ assert_equal(:horizontal, r.layout_name)
49
+ assert_equal(:css, r.style_name)
50
+ assert_equal(:rmagick, r.library_name)
51
+
42
52
  r = Runner.new(REGULAR_PATH, :layout => :vertical, :library => :chunkypng, :style => :sass)
43
53
  assert_equal(REGULAR_PATH, r.input)
44
54
  assert_equal(REGULAR_PATH, r.output)
data/test/style_test.rb CHANGED
@@ -19,6 +19,14 @@ module SpriteFactory
19
19
 
20
20
  #--------------------------------------------------------------------------
21
21
 
22
+ def test_scss
23
+ expected = "img.foo { width: 30px; height: 40px; background: url(path/to/image.png) -10px -20px no-repeat; }"
24
+ actual = Style.scss("img.", "foo", TEST_ATTRIBUTES)
25
+ assert_equal(expected, actual)
26
+ end
27
+
28
+ #--------------------------------------------------------------------------
29
+
22
30
  def test_sass
23
31
  expected = "img.foo\n width: 30px\n height: 40px\n background: url(path/to/image.png) -10px -20px no-repeat\n"
24
32
  actual = Style.sass("img.", "foo", TEST_ATTRIBUTES)
metadata CHANGED
@@ -1,58 +1,48 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: sprite-factory
3
- version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 1
7
- - 3
8
- - 0
9
- version: 1.3.0
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.4.1
5
+ prerelease:
10
6
  platform: ruby
11
- authors:
7
+ authors:
12
8
  - Jake Gordon
13
9
  autorequire:
14
10
  bindir: bin
15
11
  cert_chain: []
16
-
17
- date: 2011-07-09 00:00:00 -07:00
18
- default_executable:
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
12
+ date: 2011-08-06 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
21
15
  name: rmagick
22
- prerelease: false
23
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: &12869300 !ruby/object:Gem::Requirement
24
17
  none: false
25
- requirements:
26
- - - ">="
27
- - !ruby/object:Gem::Version
28
- segments:
29
- - 0
30
- version: "0"
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
31
22
  type: :development
32
- version_requirements: *id001
33
- - !ruby/object:Gem::Dependency
34
- name: chunky_png
35
23
  prerelease: false
36
- requirement: &id002 !ruby/object:Gem::Requirement
24
+ version_requirements: *12869300
25
+ - !ruby/object:Gem::Dependency
26
+ name: chunky_png
27
+ requirement: &12901920 !ruby/object:Gem::Requirement
37
28
  none: false
38
- requirements:
39
- - - ">="
40
- - !ruby/object:Gem::Version
41
- segments:
42
- - 0
43
- version: "0"
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
44
33
  type: :development
45
- version_requirements: *id002
46
- description: Combines individual images from a directory into a single sprite image file and creates an appropriate CSS stylesheet
47
- email:
34
+ prerelease: false
35
+ version_requirements: *12901920
36
+ description: Combines individual images from a directory into a single sprite image
37
+ file and creates an appropriate CSS stylesheet
38
+ email:
48
39
  - jake@codeincomplete.com
49
- executables:
40
+ executables:
50
41
  - sf
51
42
  extensions: []
52
-
53
- extra_rdoc_files:
43
+ extra_rdoc_files:
54
44
  - README.md
55
- files:
45
+ files:
56
46
  - .gitignore
57
47
  - LICENSE
58
48
  - README.md
@@ -134,37 +124,29 @@ files:
134
124
  - test/runner_test.rb
135
125
  - test/style_test.rb
136
126
  - test/test_case.rb
137
- has_rdoc: true
138
127
  homepage: https://github.com/jakesgordon/sprite-factory
139
128
  licenses: []
140
-
141
129
  post_install_message:
142
- rdoc_options:
130
+ rdoc_options:
143
131
  - --charset=UTF-8
144
- require_paths:
132
+ require_paths:
145
133
  - lib
146
- required_ruby_version: !ruby/object:Gem::Requirement
134
+ required_ruby_version: !ruby/object:Gem::Requirement
147
135
  none: false
148
- requirements:
149
- - - ">="
150
- - !ruby/object:Gem::Version
151
- segments:
152
- - 0
153
- version: "0"
154
- required_rubygems_version: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - ! '>='
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ required_rubygems_version: !ruby/object:Gem::Requirement
155
141
  none: false
156
- requirements:
157
- - - ">="
158
- - !ruby/object:Gem::Version
159
- segments:
160
- - 0
161
- version: "0"
142
+ requirements:
143
+ - - ! '>='
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
162
146
  requirements: []
163
-
164
147
  rubyforge_project:
165
- rubygems_version: 1.3.7
148
+ rubygems_version: 1.8.7
166
149
  signing_key:
167
150
  specification_version: 3
168
151
  summary: Automatic CSS sprite generator
169
152
  test_files: []
170
-