meatloaf 0.1.0 → 0.1.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.
Files changed (5) hide show
  1. data/Manifest +1 -0
  2. data/Rakefile +2 -2
  3. data/lib/meatloaf.rb +129 -4
  4. data/meatloaf.gemspec +5 -5
  5. metadata +5 -5
data/Manifest CHANGED
@@ -1,4 +1,5 @@
1
1
  README.rdoc
2
2
  Rakefile
3
3
  lib/meatloaf.rb
4
+ meatloaf.gemspec
4
5
  Manifest
data/Rakefile CHANGED
@@ -2,8 +2,8 @@ require 'rubygems'
2
2
  require 'rake'
3
3
  require 'echoe'
4
4
 
5
- Echoe.new('meatloaf', '0.1.0') do |p|
6
- p.description = "Compiles similar bg images into one, using SASS directives."
5
+ Echoe.new('meatloaf', '0.1.1') do |p|
6
+ p.description = "Sass-based background-image Sprite generator"
7
7
  p.url = "https://github.com/machty/meatloaf"
8
8
  p.author = "Alex Matchneer"
9
9
  p.email = "machty@gmail.com"
@@ -1,10 +1,135 @@
1
+ require 'sass'
2
+
1
3
  module Meatloaf
2
- def to_meatloaf
3
- "IT MEATS"
4
+
5
+ # An Ingredient is simply one of the images to be compiled into the Sprite.
6
+ class Ingredient
7
+
8
+ attr_accessor :width, :height, :x_offset, :y_offset, :image_path
9
+
10
+ def initialize(image_css_url, x_offset = 0, y_offset = 0)
11
+ @x_offset = x_offset
12
+ @y_offset = y_offset
13
+
14
+ # Find the asset associated with image_css_url.
15
+ asset = Rails.application.assets.find_asset(image_css_url)
16
+ raise ArgumentError.new("Can't find asset: #{image_css_url}") unless asset
17
+ @image_path = asset.pathname.to_s
18
+
19
+ # Get the dimensions of the image.
20
+ @width, @height = `identify -format "%w %h" '#{@image_path}'`.chomp.split(' ').collect { |i| i.to_i }
21
+ end
22
+ end
23
+
24
+ class Sprite
25
+
26
+ def initialize(group, css_name)
27
+ @group = group
28
+ @css_name = css_name
29
+ @ingredients = {}
30
+ @height = 0
31
+ end
32
+
33
+ # Add an image to this sprite. Returns CSS for 'background: '
34
+ def add_image(image_css_url)
35
+ return get_css_for(@ingredients[image_css_url]) if @ingredients[image_css_url]
36
+
37
+ # For now, just place the image at the bottom of the sprite.
38
+ # TODO: improve positioning algorithm
39
+ ingredient = Ingredient.new(image_css_url, 0, @height)
40
+ @height += ingredient.height
41
+ @ingredients[image_css_url] = ingredient
42
+ get_css_for(ingredient)
43
+ end
44
+
45
+ def finalize!
46
+ # Create ImageMagick command.
47
+ final_image_path = "#{Rails.root}/app/assets/images/#{filename}"
48
+ input_files_string = @ingredients.values.collect{ |ing| "'#{ing.image_path}'" }.join(' ')
49
+ command = "convert #{input_files_string} -append '#{final_image_path}'"
50
+ `#{command}`
51
+ end
52
+
53
+ private
54
+
55
+ def filename
56
+ "#{@css_name}-#{@group}-sprite.png"
57
+ end
58
+
59
+ def offset_string(offset)
60
+ offset == 0 ? "0" : "-#{offset}px"
61
+ end
62
+
63
+ def full_offset_string(ingredient)
64
+ "#{offset_string(ingredient.x_offset)} #{offset_string(ingredient.y_offset)}"
65
+ end
66
+
67
+ # Returns the CSS that follows "background: "
68
+ def get_css_for(ingredient)
69
+ Sass::Script::String.new("url(#{filename}) no-repeat #{full_offset_string(ingredient)}")
70
+ end
71
+ end
72
+
73
+ class Base
74
+
75
+ def initialize(css_filename)
76
+ # Convert 'NameOfcss.css.scss' to just 'nameofcss'
77
+ @css_name = css_filename.gsub(/\..*/, '').downcase
78
+ @sprites = {}
79
+ end
80
+
81
+ # Add image to sprite. Returns CSS for 'background: '
82
+ def add_image(image_css_url, group = "base")
83
+ image_css_url.strip!
84
+ group = group.strip.downcase
85
+ (@sprites[group] ||= Sprite.new(group, @css_name)).add_image(image_css_url)
86
+ end
87
+
88
+ # Generate all the final Sprite images.
89
+ def finalize!
90
+ @sprites.values.each { |sprite| sprite.finalize! }
91
+ end
92
+ end
93
+
94
+ # Module for adding some monkey-patch processing to the Sass Engine.
95
+ module Engine
96
+ def self.included(base)
97
+ base.class_eval do
98
+ alias_method_chain :render, :meatloaf
99
+ end
100
+ end
101
+
102
+ def render_with_meatloaf
103
+ rendered = render_without_meatloaf
104
+ options[:meatloaf].finalize! if options[:meatloaf]
105
+ rendered
106
+ end
107
+ end
108
+ end
109
+
110
+ module Sass::Script::Functions
111
+ def meatloaf(exp)
112
+ assert_type exp, :String
113
+
114
+ # Initialize Meatloaf for this stylesheet.
115
+ ml = (options[:meatloaf] ||=
116
+ Meatloaf::Base.new(options[:importer].context.pathname.basename.to_s))
117
+
118
+ exp_array = exp.value.split(':')
119
+ if exp_array.size == 1
120
+ # No group supplied.
121
+ ml.add_image(exp_array[0])
122
+ elsif exp_array.size == 2
123
+ # Group and URL supplied.
124
+ ml.add_image(exp_array[1], exp_array[0])
125
+ else
126
+ raise ArgumentError.new("Expected [group:]url")
127
+ end
4
128
  end
129
+ declare :meatloaf, [:exp]
5
130
  end
6
131
 
7
- class ActiveRecord::Base
8
- include Meatloaf
132
+ class Sass::Engine
133
+ include Meatloaf::Engine
9
134
  end
10
135
 
@@ -2,21 +2,21 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = "meatloaf"
5
- s.version = "0.1.0"
5
+ s.version = "0.1.1"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Alex Matchneer"]
9
- s.date = "2011-11-02"
10
- s.description = "Compiles similar bg images into one, using SASS directives."
9
+ s.date = "2011-11-04"
10
+ s.description = "Sass-based background-image Sprite generator"
11
11
  s.email = "machty@gmail.com"
12
12
  s.extra_rdoc_files = ["README.rdoc", "lib/meatloaf.rb"]
13
- s.files = ["README.rdoc", "Rakefile", "lib/meatloaf.rb", "Manifest", "meatloaf.gemspec"]
13
+ s.files = ["README.rdoc", "Rakefile", "lib/meatloaf.rb", "meatloaf.gemspec", "Manifest"]
14
14
  s.homepage = "https://github.com/machty/meatloaf"
15
15
  s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Meatloaf", "--main", "README.rdoc"]
16
16
  s.require_paths = ["lib"]
17
17
  s.rubyforge_project = "meatloaf"
18
18
  s.rubygems_version = "1.8.10"
19
- s.summary = "Compiles similar bg images into one, using SASS directives."
19
+ s.summary = "Sass-based background-image Sprite generator"
20
20
 
21
21
  if s.respond_to? :specification_version then
22
22
  s.specification_version = 3
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: meatloaf
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,9 +9,9 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-11-02 00:00:00.000000000Z
12
+ date: 2011-11-04 00:00:00.000000000Z
13
13
  dependencies: []
14
- description: Compiles similar bg images into one, using SASS directives.
14
+ description: Sass-based background-image Sprite generator
15
15
  email: machty@gmail.com
16
16
  executables: []
17
17
  extensions: []
@@ -22,8 +22,8 @@ files:
22
22
  - README.rdoc
23
23
  - Rakefile
24
24
  - lib/meatloaf.rb
25
- - Manifest
26
25
  - meatloaf.gemspec
26
+ - Manifest
27
27
  homepage: https://github.com/machty/meatloaf
28
28
  licenses: []
29
29
  post_install_message:
@@ -53,5 +53,5 @@ rubyforge_project: meatloaf
53
53
  rubygems_version: 1.8.10
54
54
  signing_key:
55
55
  specification_version: 3
56
- summary: Compiles similar bg images into one, using SASS directives.
56
+ summary: Sass-based background-image Sprite generator
57
57
  test_files: []