lemonade 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  Lemonade—On the fly sprite generator for Sass/Compass
2
2
  =====================================================
3
3
 
4
- Usage (SCSS or Sass):
4
+ Usage ([SCSS or Sass](http://sass-lang.com/docs/yardoc/file.SASS_REFERENCE.html)):
5
5
 
6
6
  .fanta {
7
7
  background: sprite-image("bottles/fanta.png");
@@ -40,10 +40,16 @@ Background
40
40
  Installation
41
41
  ------------
42
42
 
43
- gem install haml
44
- gem install compass
45
- gem install rmagick
46
43
  gem install lemonade
44
+
45
+
46
+ Current State
47
+ -------------
48
+
49
+ * Compass standalone finished
50
+ * Rails Sass integration finished
51
+ * Staticmatic integration finished
52
+ * Haml integration (with “:sass” filter): work in progress
47
53
 
48
54
 
49
55
  Options
@@ -62,6 +68,28 @@ Output (assuming the calculated position would be “0 -50px” as shown above):
62
68
  background: url('/images/bottles.png') 12px -47px;
63
69
  }
64
70
 
71
+ If you need empty space around the current image, this will add 20px transparent space above and below.
72
+
73
+ .seven-up {
74
+ background: sprite-image("bottles/seven-up.png", 0, 0, 20px);
75
+ }
76
+
77
+ This one adds 20px above, 30px below:
78
+
79
+ .seven-up {
80
+ background: sprite-image("bottles/seven-up.png", 0, 0, 20px, 30px);
81
+ }
82
+
83
+ Right aligned images are possible:
84
+
85
+ .seven-up {
86
+ background: sprite-image("bottles/seven-up.png", 100%, 4px);
87
+ }
88
+
89
+ The original image will be placed on the right side of the sprite image.
90
+ Use this, if you have a link with an arrow on the right side (like Apple).
91
+
92
+
65
93
 
66
94
  Note on Patches/Pull Requests
67
95
  -----------------------------
data/Rakefile CHANGED
@@ -10,6 +10,9 @@ begin
10
10
  gem.email = "gems@hagenburger.net"
11
11
  gem.homepage = "http://github.com/hagenburger/lemonade"
12
12
  gem.authors = ["Nico Hagenburger"]
13
+ gem.add_dependency "haml", ">= 3.0.0"
14
+ gem.add_dependency "compass", ">= 0.10.0"
15
+ gem.add_dependency "rmagick"
13
16
  gem.add_development_dependency "rspec", ">= 1.2.9"
14
17
  end
15
18
  Jeweler::GemcutterTasks.new
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.2.0
data/lemonade.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{lemonade}
8
- s.version = "0.1.0"
8
+ s.version = "0.2.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Nico Hagenburger"]
12
- s.date = %q{2010-05-11}
12
+ s.date = %q{2010-05-21}
13
13
  s.description = %q{Generates sprites on the fly by using `background: sprite-image("sprites/logo.png")`.}
14
14
  s.email = %q{gems@hagenburger.net}
15
15
  s.extra_rdoc_files = [
@@ -26,9 +26,11 @@ Gem::Specification.new do |s|
26
26
  "lib/lemonade.rb",
27
27
  "lib/lemonade/lemonade.rb",
28
28
  "lib/lemonade/sass_extensions/functions/lemonade.rb",
29
- "spec/images/other_images/more-images/sprites/test-2.png",
30
- "spec/images/sprites/test-1.png",
31
- "spec/images/sprites/test-2.png",
29
+ "spec/images/other_images/more-images/sprites/test.png",
30
+ "spec/images/sprites/10x10.png",
31
+ "spec/images/sprites/150x10.png",
32
+ "spec/images/sprites/20x20.png",
33
+ "spec/images/sprites/30x30.png",
32
34
  "spec/lemonade_spec.rb",
33
35
  "spec/spec.opts",
34
36
  "spec/spec_helper.rb"
@@ -48,11 +50,20 @@ Gem::Specification.new do |s|
48
50
  s.specification_version = 3
49
51
 
50
52
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
53
+ s.add_runtime_dependency(%q<haml>, [">= 3.0.0"])
54
+ s.add_runtime_dependency(%q<compass>, [">= 0.10.0"])
55
+ s.add_runtime_dependency(%q<rmagick>, [">= 0"])
51
56
  s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
52
57
  else
58
+ s.add_dependency(%q<haml>, [">= 3.0.0"])
59
+ s.add_dependency(%q<compass>, [">= 0.10.0"])
60
+ s.add_dependency(%q<rmagick>, [">= 0"])
53
61
  s.add_dependency(%q<rspec>, [">= 1.2.9"])
54
62
  end
55
63
  else
64
+ s.add_dependency(%q<haml>, [">= 3.0.0"])
65
+ s.add_dependency(%q<compass>, [">= 0.10.0"])
66
+ s.add_dependency(%q<rmagick>, [">= 0"])
56
67
  s.add_dependency(%q<rspec>, [">= 1.2.9"])
57
68
  end
58
69
  end
data/lib/lemonade.rb CHANGED
@@ -7,6 +7,7 @@ end
7
7
 
8
8
  require 'rubygems'
9
9
  require 'compass'
10
+ require 'sass/plugin'
10
11
  require 'rmagick'
11
12
  require File.dirname(__FILE__) + '/lemonade/sass_extensions/functions/lemonade'
12
13
  require File.dirname(__FILE__) + '/lemonade/lemonade'
@@ -17,10 +18,34 @@ end
17
18
 
18
19
  module Compass
19
20
  class Compiler
20
- alias_method :complile_without_comprass, :compile
21
+ alias_method :compile_without_lemonade, :compile
21
22
  def compile(sass_filename, css_filename)
22
- complile_without_comprass sass_filename, css_filename
23
+ compile_without_lemonade sass_filename, css_filename
23
24
  Lemonade::generate_sprites
24
25
  end
25
26
  end
26
- end
27
+ end
28
+
29
+ module Sass
30
+ module Plugin
31
+ alias_method :update_stylesheets_without_lemonade, :update_stylesheets
32
+ def update_stylesheets
33
+ if update_stylesheets_without_lemonade
34
+ Lemonade::generate_sprites
35
+ end
36
+ end
37
+ end
38
+ end
39
+
40
+ module Sass
41
+ class Engine
42
+ alias_method :render_without_lemonade, :render
43
+ def render
44
+ if result = render_without_lemonade
45
+ Lemonade::generate_sprites
46
+ result
47
+ end
48
+ end
49
+ alias_method :to_css, :render
50
+ end
51
+ end
@@ -9,12 +9,14 @@ module Lemonade
9
9
  sprite[:images].each do |image|
10
10
  file = File.join(Compass.configuration.images_path, image[:file])
11
11
  single_image = Magick::Image::read(file).first
12
- sprite_image.composite!(single_image, image[:x], image[:y], Magick::OverCompositeOp)
12
+ x = (sprite[:width] - image[:width]) * image[:x]
13
+ sprite_image.composite!(single_image, x, image[:y], Magick::OverCompositeOp)
13
14
  end
14
15
  file = File.join(Compass.configuration.images_path, "#{ sprite_name }.png")
15
16
  sprite_image.write file
16
17
  end
17
18
  $lemonade_sprites = nil
19
+ $lemonade_space_bottom = 0
18
20
  end
19
21
  end
20
22
  end
@@ -1,6 +1,6 @@
1
1
  module Lemonade::SassExtensions::Functions::Lemonade
2
2
 
3
- def sprite_image(file, add_x = nil, add_y = nil)
3
+ def sprite_image(file, add_x = nil, add_y = nil, margin_top_or_both = nil, margin_bottom = nil)
4
4
  assert_type file, :String
5
5
  unless (file.to_s =~ %r(^"(.+/)?(.+?)/(.+?)\.(png|gif|jpg)"$)) == 0
6
6
  raise Sass::SyntaxError, 'Please provide a file in a folder: e.g. sprites/button.png'
@@ -8,15 +8,17 @@ module Lemonade::SassExtensions::Functions::Lemonade
8
8
  dir, name, filename = $1, $2, $3
9
9
  height = image_height(file).value
10
10
  width = image_width(file).value
11
+ margin_top = calculate_margin_top(margin_top_or_both, margin_bottom)
11
12
 
12
13
  $lemonade_sprites ||= {}
13
14
  sprite = $lemonade_sprites["#{ dir }/#{ name }"] ||= { :height => 0, :width => 0, :images => [] }
14
- x, y = 0, sprite[:height]
15
- sprite[:height] += height
15
+ x = (add_x and add_x.numerator_units == %w(%)) ? add_x.value / 100 : 0
16
+ y = sprite[:height] + margin_top
17
+ sprite[:height] += height + margin_top
16
18
  sprite[:width] = width if width > sprite[:width]
17
- position = background_position(x, y, add_x, add_y)
18
19
  sprite[:images] << { :file => file.to_s.gsub('"', ''), :height => height, :width => width, :x => x, :y => y }
19
20
 
21
+ position = background_position(0, y, add_x, add_y)
20
22
  file = image_url(Sass::Script::String.new("#{ dir }#{ name }.png"))
21
23
  Sass::Script::String.new("#{ file }#{ position }")
22
24
  end
@@ -25,11 +27,18 @@ private
25
27
 
26
28
  def background_position(x, y, add_x, add_y)
27
29
  y = -y
28
- x += add_x.value if add_x
30
+ x = add_x ? add_x.to_s : 0
29
31
  y += add_y.value if add_y
30
- unless x == 0 and y == 0
31
- " #{ x }#{ 'px' unless x == 0 } #{ y }#{ 'px' unless y == 0 }"
32
+ unless (add_x.nil? or add_x.value == 0) and y == 0
33
+ " #{ x } #{ y }#{ 'px' unless y == 0 }"
32
34
  end
33
35
  end
36
+
37
+ def calculate_margin_top(margin_top_or_both, margin_bottom)
38
+ margin_top_or_both = margin_top_or_both ? margin_top_or_both.value : 0
39
+ margin_top = ($lemonade_margin_bottom ||= 0) > margin_top_or_both ? $lemonade_margin_bottom : margin_top_or_both
40
+ $lemonade_margin_bottom = margin_bottom ? margin_bottom.value : margin_top_or_both
41
+ margin_top
42
+ end
34
43
 
35
44
  end
Binary file
File without changes
Binary file
Binary file
@@ -3,12 +3,18 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
3
3
  describe Lemonade::SassExtensions::Functions::Lemonade do
4
4
  before :each do
5
5
  @sass = Sass::Environment.new
6
+ $lemonade_sprites = nil
7
+ $lemonade_margin_bottom = nil
6
8
  FileUtils.cp_r File.dirname(__FILE__) + '/images', IMAGES_TMP_PATH
7
9
  end
8
10
 
9
11
  after :each do
10
12
  FileUtils.rm_r IMAGES_TMP_PATH
11
- $lemonade_sprites = nil
13
+ end
14
+
15
+ def image_size(file)
16
+ Lemonade::generate_sprites
17
+ IO.read(IMAGES_TMP_PATH + '/' + file)[0x10..0x18].unpack('NN')
12
18
  end
13
19
 
14
20
  def evaluate(value)
@@ -16,11 +22,11 @@ describe Lemonade::SassExtensions::Functions::Lemonade do
16
22
  end
17
23
 
18
24
  it "should return the sprite file name" do
19
- evaluate('sprite-image("sprites/test-1.png")').should == "url('/sprites.png')"
25
+ evaluate('sprite-image("sprites/30x30.png")').should == "url('/sprites.png')"
20
26
  end
21
27
 
22
28
  it "should work in folders with dashes and underscores" do
23
- evaluate('sprite-image("other_images/more-images/sprites/test-2.png")').should ==
29
+ evaluate('sprite-image("other_images/more-images/sprites/test.png")').should ==
24
30
  "url('/other_images/more-images/sprites.png')"
25
31
  end
26
32
 
@@ -29,14 +35,68 @@ describe Lemonade::SassExtensions::Functions::Lemonade do
29
35
  end
30
36
 
31
37
  it "should set the background position" do
32
- evaluate('sprite-image("sprites/test-1.png")').should == "url('/sprites.png')"
33
- evaluate('sprite-image("sprites/test-2.png")').should == "url('/sprites.png') 0 -30px"
34
- Lemonade::generate_sprites
35
- File.exists?(IMAGES_TMP_PATH + '/sprites.png').should be_true
38
+ evaluate('sprite-image("sprites/30x30.png")').should == "url('/sprites.png')"
39
+ evaluate('sprite-image("sprites/150x10.png")').should == "url('/sprites.png') 0 -30px"
40
+ image_size('sprites.png').should == [150, 40]
36
41
  end
37
42
 
38
43
  it "should use the X position" do
39
- evaluate('sprite-image("sprites/test-1.png", 5px, 0)').should == "url('/sprites.png') 5px 0"
44
+ evaluate('sprite-image("sprites/30x30.png", 5px, 0)').should == "url('/sprites.png') 5px 0"
45
+ image_size('sprites.png').should == [30, 30]
46
+ end
47
+
48
+ it "should calculate 20px empty space between sprites" do
49
+ # Resulting sprite should look like (1 line = 10px height, X = placed image):
50
+
51
+ # X
52
+ #
53
+ #
54
+ # XX
55
+ # XX
56
+ #
57
+ #
58
+ # XXX
59
+ # XXX
60
+ # XXX
61
+
62
+ evaluate('sprite-image("sprites/10x10.png")').should == "url('/sprites.png')"
63
+ # space before #2: 20px
64
+ evaluate('sprite-image("sprites/20x20.png", 0, 0, 20px)').should == "url('/sprites.png') 0 -30px"
65
+ # space after #2: 20px
66
+ evaluate('sprite-image("sprites/30x30.png")').should == "url('/sprites.png') 0 -70px"
67
+ image_size('sprites.png').should == [30, 100]
68
+ end
69
+
70
+ it "should calculate empty space between sprites and combine space like CSS margins" do
71
+ # Resulting sprite should look like (1 line = 10px height, X = placed image):
72
+
73
+ # X
74
+ #
75
+ #
76
+ #
77
+ # XX
78
+ # XX
79
+ #
80
+ # XXX
81
+ # XXX
82
+ # XXX
83
+
84
+ evaluate('sprite-image("sprites/10x10.png", 0, 0, 0, 30px)').should == "url('/sprites.png')"
85
+ # space between #1 and #2: 30px (=> 30px > 20px)
86
+ evaluate('sprite-image("sprites/20x20.png", 0, 0, 20px, 5px)').should == "url('/sprites.png') 0 -40px"
87
+ # space between #2 and #3: 10px (=> 5px < 10px)
88
+ evaluate('sprite-image("sprites/30x30.png", 0, 0, 10px)').should == "url('/sprites.png') 0 -70px"
89
+ image_size('sprites.png').should == [30, 100]
90
+ end
91
+
92
+ it "should allow % for x positions" do
93
+ # Resulting sprite should look like (1 line = 10px height, X = placed image):
94
+
95
+ # XXXXXXXXXXXXXXX
96
+ # X
97
+
98
+ evaluate('sprite-image("sprites/150x10.png")')
99
+ evaluate('sprite-image("sprites/10x10.png", 100%)').should == "url('/sprites.png') 100% -10px"
40
100
  end
41
101
 
42
102
  end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 1
7
+ - 2
8
8
  - 0
9
- version: 0.1.0
9
+ version: 0.2.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Nico Hagenburger
@@ -14,13 +14,56 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-05-11 00:00:00 +02:00
17
+ date: 2010-05-21 00:00:00 +02:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
- name: rspec
21
+ name: haml
22
22
  prerelease: false
23
23
  requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 3
30
+ - 0
31
+ - 0
32
+ version: 3.0.0
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: compass
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ segments:
44
+ - 0
45
+ - 10
46
+ - 0
47
+ version: 0.10.0
48
+ type: :runtime
49
+ version_requirements: *id002
50
+ - !ruby/object:Gem::Dependency
51
+ name: rmagick
52
+ prerelease: false
53
+ requirement: &id003 !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ segments:
59
+ - 0
60
+ version: "0"
61
+ type: :runtime
62
+ version_requirements: *id003
63
+ - !ruby/object:Gem::Dependency
64
+ name: rspec
65
+ prerelease: false
66
+ requirement: &id004 !ruby/object:Gem::Requirement
24
67
  none: false
25
68
  requirements:
26
69
  - - ">="
@@ -31,7 +74,7 @@ dependencies:
31
74
  - 9
32
75
  version: 1.2.9
33
76
  type: :development
34
- version_requirements: *id001
77
+ version_requirements: *id004
35
78
  description: "Generates sprites on the fly by using `background: sprite-image(\"sprites/logo.png\")`."
36
79
  email: gems@hagenburger.net
37
80
  executables: []
@@ -51,9 +94,11 @@ files:
51
94
  - lib/lemonade.rb
52
95
  - lib/lemonade/lemonade.rb
53
96
  - lib/lemonade/sass_extensions/functions/lemonade.rb
54
- - spec/images/other_images/more-images/sprites/test-2.png
55
- - spec/images/sprites/test-1.png
56
- - spec/images/sprites/test-2.png
97
+ - spec/images/other_images/more-images/sprites/test.png
98
+ - spec/images/sprites/10x10.png
99
+ - spec/images/sprites/150x10.png
100
+ - spec/images/sprites/20x20.png
101
+ - spec/images/sprites/30x30.png
57
102
  - spec/lemonade_spec.rb
58
103
  - spec/spec.opts
59
104
  - spec/spec_helper.rb
Binary file