sprite 0.1.7 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (71) hide show
  1. data/Gemfile +9 -0
  2. data/Gemfile.lock +50 -0
  3. data/README.md +27 -23
  4. data/Rakefile +37 -20
  5. data/lib/sprite.rb +5 -0
  6. data/lib/sprite/builder.rb +90 -101
  7. data/lib/sprite/config.rb +48 -0
  8. data/lib/sprite/image_combiner.rb +17 -10
  9. data/lib/sprite/image_config.rb +40 -0
  10. data/lib/sprite/image_reader.rb +14 -0
  11. data/lib/sprite/image_resizer.rb +19 -0
  12. data/lib/sprite/image_writer.rb +29 -0
  13. data/lib/sprite/runner.rb +6 -4
  14. data/lib/sprite/sass_extensions.rb +93 -48
  15. data/lib/sprite/styles.rb +3 -1
  16. data/lib/sprite/styles/css_generator.rb +19 -15
  17. data/lib/sprite/styles/sass_generator.rb +5 -4
  18. data/lib/sprite/styles/sass_mixin_generator.rb +7 -7
  19. data/lib/sprite/styles/sass_yml_generator.rb +15 -16
  20. data/lib/sprite/styles/templated_css_generator.rb +41 -0
  21. data/spec/output/android_horizontal/images/sprites/android-icons.png +0 -0
  22. data/spec/output/android_horizontal/stylesheets/android-icons.css +1 -0
  23. data/spec/output/android_vertical/images/sprites/android-icons.png +0 -0
  24. data/spec/output/android_vertical/stylesheets/android-icons.css +1 -0
  25. data/spec/resources/android_css.erb +5 -0
  26. data/spec/resources/configs/android-icons.yml +19 -0
  27. data/spec/resources/configs/config-test.yml +26 -0
  28. data/spec/resources/images/android-icons/LICENSE +1 -0
  29. data/spec/resources/images/android-icons/barcode.png +0 -0
  30. data/spec/resources/images/android-icons/cards.png +0 -0
  31. data/spec/resources/images/android-icons/chart.png +0 -0
  32. data/spec/resources/images/android-icons/clock.png +0 -0
  33. data/spec/resources/images/android-icons/cloud.png +0 -0
  34. data/spec/resources/images/android-icons/colour-picker.png +0 -0
  35. data/spec/resources/images/android-icons/dialog.png +0 -0
  36. data/spec/resources/images/android-icons/dice.png +0 -0
  37. data/spec/resources/images/android-icons/disc.png +0 -0
  38. data/spec/resources/images/android-icons/equalizer.png +0 -0
  39. data/spec/resources/images/android-icons/filter.png +0 -0
  40. data/spec/resources/images/android-icons/flag.png +0 -0
  41. data/spec/resources/images/android-icons/flash.png +0 -0
  42. data/spec/resources/images/android-icons/globe.png +0 -0
  43. data/spec/resources/images/android-icons/happy.png +0 -0
  44. data/spec/resources/images/android-icons/large-tiles.png +0 -0
  45. data/spec/resources/images/android-icons/light.png +0 -0
  46. data/spec/resources/images/android-icons/love.png +0 -0
  47. data/spec/resources/images/android-icons/magnet.png +0 -0
  48. data/spec/resources/images/android-icons/monitor.png +0 -0
  49. data/spec/resources/images/android-icons/music.png +0 -0
  50. data/spec/resources/images/android-icons/pie-chart.png +0 -0
  51. data/spec/resources/images/android-icons/ruler.png +0 -0
  52. data/spec/resources/images/android-icons/sad.png +0 -0
  53. data/spec/resources/images/android-icons/seal.png +0 -0
  54. data/spec/resources/images/android-icons/shopping.png +0 -0
  55. data/spec/resources/images/android-icons/small-tiles.png +0 -0
  56. data/spec/resources/images/android-icons/sun.png +0 -0
  57. data/spec/resources/images/android-icons/tag.png +0 -0
  58. data/spec/resources/images/android-icons/umbrella.png +0 -0
  59. data/spec/resources/images/topics/good-topic.gif +0 -0
  60. data/spec/resources/images/topics/mid-topic.gif +0 -0
  61. data/spec/spec_helper.rb +32 -0
  62. data/spec/sprite/builder_spec.rb +76 -0
  63. data/spec/sprite/config_spec.rb +64 -0
  64. data/spec/sprite/image_combiner_spec.rb +40 -0
  65. data/spec/sprite/image_reader_spec.rb +16 -0
  66. data/spec/sprite/styles/css_spec.rb +0 -0
  67. data/spec/sprite/styles/sass_mixin_spec.rb +0 -0
  68. data/spec/sprite/styles/sass_spec.rb +0 -0
  69. data/sprite.gemspec +30 -49
  70. metadata +142 -21
  71. data/VERSION +0 -1
@@ -15,12 +15,13 @@ module Sprite
15
15
  f.puts ".#{@builder.config['sprites_class']}"
16
16
  @level += 1
17
17
  end
18
-
18
+
19
19
  sprite_files.each do |sprite_file, sprites|
20
+ background_url = @builder.background_url(sprite_file)
20
21
  sprites.each do |sprite|
21
22
  f.puts sass_line("&.#{sprite[:group]}#{@builder.config['class_separator']}#{sprite[:name]}")
22
23
  @level += 1
23
- f.puts sass_line("background: url('/#{@builder.config['image_output_path']}#{sprite_file}') no-repeat #{sprite[:x]}px #{sprite[:y]}px")
24
+ f.puts sass_line("background: #{background_url} no-repeat #{sprite[:x]}px #{sprite[:y]}px")
24
25
  f.puts sass_line("width: #{sprite[:width]}px")
25
26
  f.puts sass_line("height: #{sprite[:height]}px")
26
27
  f.puts sass_line("")
@@ -29,12 +30,12 @@ module Sprite
29
30
  end
30
31
  end
31
32
  end
32
-
33
+
33
34
  # write sass output with correct tab spaces prepended
34
35
  def sass_line(sass)
35
36
  "#{' '*@level}#{sass}"
36
37
  end
37
-
38
+
38
39
  def extension
39
40
  "sass"
40
41
  end
@@ -5,31 +5,31 @@ module Sprite
5
5
  def initialize(builder)
6
6
  @builder = builder
7
7
  end
8
-
9
- def write(path, sprite_files)
8
+
9
+ def write(path, sprite_files)
10
10
  # write the sass mixins to disk
11
11
  File.open(File.join(Sprite.root, path), 'w') do |f|
12
12
  add_else = false
13
13
 
14
14
  f.puts "= sprite(!group_name, !image_name, !offset=0)"
15
15
  sprite_files.each do |sprite_file, sprites|
16
+ background_url = @builder.background_url(sprite_file)
16
17
  sprites.each do |sprite|
17
-
18
18
  f << " @"
19
19
  if add_else
20
20
  f << "else "
21
21
  end
22
22
  add_else = true
23
23
  #{sprite[:x]}px #{sprite[:y]}px
24
-
24
+
25
25
  if sprite[:align] == 'horizontal'
26
26
  background_offset = "\#{#{sprite[:x]}+!offset}px #{sprite[:y]}px"
27
27
  else
28
28
  background_offset = "#{sprite[:x]}px \#{#{sprite[:y]}+!offset}px"
29
29
  end
30
-
30
+
31
31
  f.puts %{if !group_name == "#{sprite[:group]}" and !image_name == "#{sprite[:name]}"}
32
- f.puts " background: url('/#{@builder.config['image_output_path']}#{sprite_file}') no-repeat #{background_offset}"
32
+ f.puts " background: #{background_url} repeat #{background_offset}"
33
33
  f.puts " width: #{sprite[:width]}px"
34
34
  f.puts " height: #{sprite[:height]}px"
35
35
  end
@@ -40,7 +40,7 @@ module Sprite
40
40
  def extension
41
41
  "sass"
42
42
  end
43
-
43
+
44
44
  end
45
45
  end
46
46
  end
@@ -6,14 +6,17 @@ module Sprite
6
6
  def initialize(builder)
7
7
  @builder = builder
8
8
  end
9
-
10
- def write(path, sprite_files)
9
+
10
+ def write(path, sprite_files)
11
11
  # build the yml file
12
- config_location = write_config(path, sprite_files)
13
-
12
+ write_config(path, sprite_files)
13
+
14
+ # Where to put the sass mixin file
15
+ sass_path = path.gsub(".yml", ".sass")
16
+
14
17
  # write the sass mixins to disk
15
- File.open(File.join(Sprite.root, path), 'w') do |f|
16
- f.puts "!sprite_data = '#{config_location}'"
18
+ File.open(File.join(Sprite.root, sass_path), 'w') do |f|
19
+ f.puts "!sprite_data = '#{path}'"
17
20
  f.puts ""
18
21
  f.puts "= sprite(!group_name, !image_name)"
19
22
  f.puts " background= sprite_background(!group_name, !image_name)"
@@ -22,7 +25,7 @@ module Sprite
22
25
  f.puts ""
23
26
  end
24
27
  end
25
-
28
+
26
29
  # write the sprite configuration file (used by the yml extension)
27
30
  def write_config(path, sprite_files)
28
31
  # build a grouped hash with all the sprites in it
@@ -35,20 +38,16 @@ module Sprite
35
38
  end
36
39
  end
37
40
  end
38
-
41
+
39
42
  # write the config yml to disk
40
- config_path = path.gsub(".sass", ".yml")
41
- File.open(File.join(Sprite.root, config_path), 'w') do |f|
43
+ File.open(File.join(Sprite.root, path), 'w') do |f|
42
44
  YAML.dump(result, f)
43
45
  end
44
-
45
- config_path
46
46
  end
47
-
47
+
48
48
  def extension
49
- "sass"
49
+ "yml"
50
50
  end
51
-
52
51
  end
53
52
  end
54
- end
53
+ end
@@ -0,0 +1,41 @@
1
+ require 'erb'
2
+
3
+ module Sprite
4
+ module Styles
5
+ # renders css rules from template
6
+ class TemplatedCssGenerator
7
+ def initialize(builder)
8
+ @builder = builder
9
+ end
10
+
11
+ def write(path, sprite_files)
12
+ # write styles to disk
13
+ File.open(File.join(Sprite.root, path), 'w') do |f|
14
+ f.puts "/* Generated by the sprite gem */"
15
+ sprite_files.each do |sprite_file|
16
+ @builder.images.each do |image|
17
+ if "#{image['name']}.#{image['format']}" == sprite_file[0]
18
+ erb_path = @builder.send :style_template_source_path, image
19
+ erb_template = ERB.new(File.read(erb_path))
20
+ sprites = sprite_file[1]
21
+ sprites.each do |sprite|
22
+ name = sprite[:name]
23
+ width = sprite[:width]
24
+ height = sprite[:height]
25
+ left = sprite[:x]
26
+ top = sprite[:y]
27
+ image_path = ImageWriter.new(@builder.config).image_output_path(image['name'], image['format'])
28
+ f.puts erb_template.result(binding)
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+
36
+ def extension
37
+ "css"
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1 @@
1
+ /* Generated by the sprite gem */
@@ -0,0 +1 @@
1
+ /* Generated by the sprite gem */
@@ -0,0 +1,5 @@
1
+ .sprite_<%= name %> {
2
+ background: url('<%= image_path %>') no-repeat <%= left %>px <%= top %>px;
3
+ width: <%= width %>px;
4
+ height: <%= height %>px;
5
+ }
@@ -0,0 +1,19 @@
1
+ config:
2
+ style: templated_css
3
+ style_output_path: output/stylesheets/android-icons
4
+ image_output_path: output/images/sprites/
5
+ image_source_path: resources/images/
6
+ public_path: ''
7
+ class_separator: '-'
8
+ default_format: png
9
+
10
+ # defines what sprite collections get created
11
+ images:
12
+ - name: android-icons
13
+ format: png
14
+ style_output_template: resources/android_css.erb
15
+ align: vertical
16
+ spaced_by: 50
17
+ resize_to: 48x48
18
+ sources:
19
+ - android-icons/*.png
@@ -0,0 +1,26 @@
1
+ config:
2
+ style: css
3
+ style_output_path: output/stylesheets/sprites
4
+ image_output_path: output/images/sprites/
5
+ image_source_path: resources/images/
6
+ css_image_path: ../images/sprites
7
+ public_path: ""
8
+ class_separator: '-'
9
+ default_format: png
10
+
11
+ # defines what sprite collections get created
12
+ images:
13
+ - name: android-icons
14
+ format: png
15
+ align: vertical
16
+ spaced_by: 50
17
+ sources:
18
+ - android-icons/*.png
19
+
20
+ - name: topics
21
+ format: gif
22
+ align: vertical
23
+ spaced_by: 50
24
+ sources:
25
+ - topics/good-topic.gif
26
+ - topics/mid-topic.gif
@@ -0,0 +1 @@
1
+ This work is licensed under the Creative Commons Attribution 3.0 Unported License. To view a copy of this license, visit http://creativecommons.org/licenses/by/3.0/ or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA.
@@ -0,0 +1,32 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "/../lib"))
2
+ require 'sprite'
3
+ require 'bundler/setup'
4
+
5
+ require 'date'
6
+ require 'fileutils'
7
+
8
+ # set Sprite.root to be this spec/ folder
9
+ Sprite.module_eval{ @root = File.dirname(__FILE__) }
10
+
11
+ FileUtils.rm_rf("#{Sprite.root}/output")
12
+
13
+ RSpec.configure do |c|
14
+
15
+ module SpriteSpecHelpers
16
+ def clear_output
17
+ FileUtils.rm_rf("#{Sprite.root}/output")
18
+ end
19
+ end
20
+
21
+ c.include(SpriteSpecHelpers)
22
+
23
+ # setup fixtures path
24
+ c.before(:all) do
25
+ end
26
+
27
+ # returns the file path of a fixture setting file
28
+ def config_path(filename)
29
+ @fixture_path.join(filename)
30
+ end
31
+
32
+ end
@@ -0,0 +1,76 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper.rb'
2
+
3
+ describe Sprite::Builder do
4
+
5
+ context "should generate vertical android icon sprites" do
6
+ before(:all) do
7
+ @sprite = Sprite::Builder.from_config("resources/configs/android-icons.yml")
8
+ @sprite.config["style_output_path"] = "output/android_vertical/stylesheets/android-icons"
9
+ @sprite.config["image_output_path"] = "output/android_vertical/images/sprites/"
10
+ @sprite.images.first["align"] = "vertical"
11
+ @sprite.build
12
+
13
+ @output_path = "#{Sprite.root}/output/android_vertical"
14
+ end
15
+
16
+ context "and the sprite result image" do
17
+ before(:all) do
18
+ combiner = Sprite::ImageCombiner.new(mock())
19
+ @result_image = Sprite::ImageReader.read("#{@output_path}/images/sprites/android-icons.png")
20
+ @result_properties = combiner.image_properties(@result_image)
21
+ end
22
+
23
+ it "should be 48x2890" do
24
+ "#{@result_properties[:width]}x#{@result_properties[:height]}".should == "48x2890"
25
+ end
26
+ end
27
+
28
+ context "and the sprite result styles" do
29
+ before(:all) do
30
+ @styles = File.read("#{@output_path}/stylesheets/android-icons.css")
31
+ end
32
+
33
+ it "should have some styles in it" do
34
+ @styles.should_not be_nil
35
+ @styles.strip.should_not == ""
36
+ end
37
+ end
38
+ end
39
+
40
+ context "should generate horizontal android icon sprites" do
41
+ before(:all) do
42
+ @sprite = Sprite::Builder.from_config("resources/configs/android-icons.yml")
43
+ @sprite.config["style_output_path"] = "output/android_horizontal/stylesheets/android-icons"
44
+ @sprite.config["image_output_path"] = "output/android_horizontal/images/sprites/"
45
+ @sprite.images.first["align"] = "horizontal"
46
+ @sprite.build
47
+
48
+ @output_path = "#{Sprite.root}/output/android_horizontal"
49
+ end
50
+
51
+ context "and the sprite result image" do
52
+ before(:all) do
53
+ combiner = Sprite::ImageCombiner.new(@sprite.config)
54
+ @result_image = Sprite::ImageReader.read("#{@output_path}/images/sprites/android-icons.png")
55
+ @result_properties = combiner.image_properties(@result_image)
56
+ end
57
+
58
+ it "should be 2890x48" do
59
+ "#{@result_properties[:width]}x#{@result_properties[:height]}".should == "2890x48"
60
+ end
61
+ end
62
+
63
+ context "and sprite result styles" do
64
+ before(:all) do
65
+ @styles = File.read("#{@output_path}/stylesheets/android-icons.css")
66
+ end
67
+
68
+ it "should have some styles in it" do
69
+ @styles.should_not be_nil
70
+ @styles.strip.should_not == ""
71
+ end
72
+ end
73
+ end
74
+
75
+
76
+ end
@@ -0,0 +1,64 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper.rb'
2
+
3
+ describe Sprite::Builder do
4
+
5
+ context "configuration parsing" do
6
+ before(:all) do
7
+ @sprite = Sprite::Builder.from_config("resources/configs/config-test.yml")
8
+ end
9
+
10
+ it "loads the image keys from file" do
11
+ @sprite.images.size.should == 2
12
+ end
13
+
14
+ it "expands any globs within the source paths" do
15
+ @sprite.images.first["sources"].size.should == 30
16
+ end
17
+
18
+ it "allows override of css_image_path value" do
19
+ @sprite.config['css_image_path'].should == '../images/sprites'
20
+ end
21
+ end
22
+
23
+ context "default settings" do
24
+ before(:all) do
25
+ @sprite = Sprite::Builder.new
26
+ end
27
+
28
+ it "'style:' setting defaults to 'css'" do
29
+ @sprite.config['style'].should == "css"
30
+ end
31
+
32
+ it "'style_output_path:' setting defaults to 'stylesheets/sprites'" do
33
+ @sprite.config['style_output_path'].should == "stylesheets/sprites"
34
+ end
35
+
36
+ it "'image_output_path:' setting defaults to 'images/sprites/'" do
37
+ @sprite.config['image_output_path'].should == "images/sprites/"
38
+ end
39
+
40
+ it "'css_image_path:' setting defaults to '/images/sprites/'" do
41
+ @sprite.config['css_image_path'].should == "/images/sprites/"
42
+ end
43
+
44
+ it "'image_source_path:' setting defaults to 'images/'" do
45
+ @sprite.config['image_source_path'].should == "images/"
46
+ end
47
+
48
+ it "'public_path:' setting defaults to 'public/'" do
49
+ @sprite.config['public_path'].should == "public/"
50
+ end
51
+
52
+ it "'default_format:' setting defaults to 'png'" do
53
+ @sprite.config['default_format'].should == "png"
54
+ end
55
+
56
+ it "'sprites_class:' setting defaults to 'sprites'" do
57
+ @sprite.config['sprites_class'].should == "sprites"
58
+ end
59
+
60
+ it "'class_separator:' setting defaults to '-'" do
61
+ @sprite.config['class_separator'].should == "-"
62
+ end
63
+ end
64
+ end