sprite 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/README.md +9 -7
  2. data/VERSION +1 -1
  3. data/lib/sprite/builder.rb +79 -28
  4. data/sprite.gemspec +1 -1
  5. metadata +1 -1
data/README.md CHANGED
@@ -90,9 +90,10 @@ Configuration of `sprite` is done via `config/sprite.yml`. It allows you to set
90
90
 
91
91
  * `config:` section defines all the global properties for sprite generation. Such as how it generates the styles, where it looks for images, where it writes it output file to, and what image file format it uses by default
92
92
  - `style:` defines how the style rules are outputted. built in options are `css`, `sass`, and `sass_mixin`. (defaults to `css`)
93
- - `output_path:` defines the file path where your style settings get written (defaults to `public/stylesheets/sprites`). the file extension not needed as it will be set based on the `style:` setting
94
- - `image_output_path:` defines the folder path where the combined sprite images files are written (defaults to `public/images/sprites/`)
95
- - `source_path:` defines the folder where source image files are read from (defaults to `public/images/`)
93
+ - `style_output_path:` defines the file path where your style settings get written (defaults to `stylesheets/sprites`). the file extension not needed as it will be set based on the `style:` setting
94
+ - `image_output_path:` defines the folder path where the combined sprite images files are written (defaults to `images/sprites/`)
95
+ - `image_source_path:` defines the folder where source image files are read from (defaults to `images/`)
96
+ - `public_path:` defines the root folder where static assets live (defaults to `public/`)
96
97
  - `sprites_class:` defines the class name that gets added to all sprite stylesheet rules (defaults to `sprites`)
97
98
  - `default_format:` defines the default file image format of the generated files. (defaults to `png`)
98
99
  - `class_separator:` used to generated the class name by separating the image name and sprite name (defaults to `-`)
@@ -104,7 +105,7 @@ Configuration of `sprite` is done via `config/sprite.yml`. It allows you to set
104
105
  - `spaced_by:` spacing (in pixels) between the combined images. (defaults to `0`)
105
106
  - `format:` define what image file format gets created (optional, uses `default_format` setting if not set)
106
107
 
107
- you can define any number of destination image files.
108
+ All image and style paths should be set relative to the public folder (which is configurable via public_path setting).
108
109
 
109
110
  ### Sample Configuration `config/sprite.yml` ###
110
111
 
@@ -112,9 +113,10 @@ you can define any number of destination image files.
112
113
 
113
114
  config:
114
115
  style: css
115
- output_path: public/sass/mixins/sprites.sass
116
- image_output_path: public/images/sprites/
117
- source_path: public/images/
116
+ style_output_path: sass/mixins/sprites.sass
117
+ image_output_path: images/sprites/
118
+ image_source_path: images/
119
+ public_path: public/
118
120
  sprites_class: 'sprites'
119
121
  class_separator: '-'
120
122
  default_format: png
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.1.2
@@ -8,16 +8,19 @@ module Sprite
8
8
  attr_reader :output
9
9
 
10
10
  def self.from_config(path = nil)
11
-
12
11
  results = {}
13
- config_path = File.join(Sprite.root, path || DEFAULT_CONFIG_PATH)
14
- begin
15
- results = File.open(config_path) {|f| YAML::load(f)} || {}
16
- rescue => e
17
- puts "Unable to read sprite config: #{Sprite.root+"/"+config_path}"
18
- puts e.to_s
19
- end
12
+ config_path = File.join(Sprite.root, path || DEFAULT_CONFIG_PATH)
20
13
 
14
+ # read configuration
15
+ if File.exists?(config_path)
16
+ begin
17
+ results = File.open(config_path) {|f| YAML::load(f)} || {}
18
+ rescue => e
19
+ puts "Error reading sprite config: #{config_path}"
20
+ puts e.to_s
21
+ end
22
+ end
23
+
21
24
  new(results["config"], results["images"])
22
25
  end
23
26
 
@@ -26,12 +29,9 @@ module Sprite
26
29
  set_config_defaults
27
30
 
28
31
  @images = images || []
32
+ set_image_defaults
29
33
  expand_image_paths
30
34
 
31
- # freeze hashes
32
- @config = @config.dup.freeze
33
- @images = @images.dup.freeze
34
-
35
35
  # initialize output
36
36
  @output = {}
37
37
  end
@@ -49,7 +49,8 @@ module Sprite
49
49
  output_file
50
50
  end
51
51
  end
52
-
52
+
53
+ protected
53
54
  def output_image(image)
54
55
  results = []
55
56
  sources = image['sources'].to_a
@@ -74,19 +75,19 @@ module Sprite
74
75
  results << combiner.image_properties(source_image).merge(:x => x, :y => y, :group => name)
75
76
  dest_image = combiner.composite_images(dest_image, source_image, x, y)
76
77
  end
77
- @output[name] = results
78
78
 
79
79
  # set up path
80
- path = image_path(name, image['format'])
80
+ path = image_output_path(name, image['format'] || config["default_format"])
81
81
  FileUtils.mkdir_p(File.dirname(path))
82
82
 
83
83
  # write sprite image file to disk
84
84
  dest_image.write(path)
85
+ @output[name] = results
85
86
  end
86
-
87
+
87
88
  def output_file
88
89
  # set up path
89
- path = output_path("css")
90
+ path = style_output_path("css")
90
91
  FileUtils.mkdir_p(File.dirname(path))
91
92
 
92
93
  # set up class_name to append to each rule
@@ -97,7 +98,7 @@ module Sprite
97
98
  @output.each do |dest, results|
98
99
  results.each do |result|
99
100
  f.puts "#{sprites_class}.#{result[:group]}#{config['class_separator']}#{result[:name]} {"
100
- f.puts " background: url('/images/#{dest}') no-repeat #{result[:x]}px #{result[:y]}px;"
101
+ f.puts " background: url('/#{config['image_output_path']}#{dest}') no-repeat #{result[:x]}px #{result[:y]}px;"
101
102
  f.puts " width: #{result[:width]}px;"
102
103
  f.puts " height: #{result[:height]}px;"
103
104
  f.puts "}"
@@ -106,25 +107,75 @@ module Sprite
106
107
  end
107
108
  end
108
109
 
109
- protected
110
+ # get the disk path for the style output file
111
+ def style_output_path(file_ext)
112
+ path = config['style_output_path']
113
+ unless path.include?(".#{file_ext}")
114
+ path = "#{path}.#{file_ext}"
115
+ end
116
+ public_path(path)
117
+ end
110
118
 
111
- def output_path(file_ext)
112
- "#{Sprite.root}/#{config['output_path']}.#{file_ext}"
119
+ # get the disk path for a location within the image output folder
120
+ def image_output_path(name, format)
121
+ path_parts = []
122
+ path_parts << chop_trailing_slash(config['image_output_path'])
123
+ path_parts << "#{name}.#{format}"
124
+ public_path(File.join(*path_parts))
113
125
  end
114
126
 
115
- def image_path(name, format)
116
- "#{Sprite.root}/#{config['image_output_path']}#{name}.#{format}"
127
+ # get the disk path for a location within the public folder (if set)
128
+ def public_path(location)
129
+ path_parts = []
130
+ path_parts << Sprite.root
131
+ path_parts << chop_trailing_slash(config['public_path']) if config['public_path'] and config['public_path'].length > 0
132
+ path_parts << location
133
+
134
+ File.join(*path_parts)
135
+ end
136
+
137
+ # chop off the trailing slash on a directory path (if it exists)
138
+ def chop_trailing_slash(path)
139
+ path = path[0...-1] if path[-1] == File::SEPARATOR
140
+ path
117
141
  end
118
142
 
119
143
  # sets all the default values on the config
120
144
  def set_config_defaults
121
145
  @config['style'] ||= 'css'
122
- @config['output_path'] ||= 'public/stylesheets/sprites'
123
- @config['image_output_path'] ||= 'public/images/sprites/'
124
- @config['source_path'] ||= 'public/images/'
146
+ @config['style_output_path'] ||= 'stylesheets/sprites'
147
+ @config['image_output_path'] ||= 'images/sprites/'
148
+ @config['image_source_path'] ||= 'images/'
149
+ @config['public_path'] ||= 'public/'
125
150
  @config['default_format'] ||= 'png'
126
151
  @config['class_separator'] ||= '-'
127
- @config["sprites_class"] ||= 'sprites'
152
+ @config["sprites_class"] ||= 'sprites'
153
+ end
154
+
155
+ # if no image configs are detected, set some intelligent defaults
156
+ def set_image_defaults
157
+ return unless @images.size == 0
158
+
159
+ sprites_path = File.join(Sprite.root, config['public_path'], config['image_source_path'], "sprites")
160
+
161
+ if File.exists?(sprites_path)
162
+ Dir.glob(File.join(sprites_path, "*")) do |dir|
163
+ next unless File.directory?(dir)
164
+ source_name = File.basename(dir)
165
+
166
+ # default to finding all png, gif, jpg, and jpegs within the directory
167
+ images << {
168
+ "name" => source_name,
169
+ "sources" => [
170
+ File.join("sprites", source_name, "*.png"),
171
+ File.join("sprites", source_name, "*.gif"),
172
+ File.join("sprites", source_name, "*.jpg"),
173
+ File.join("sprites", source_name, "*.jpeg"),
174
+ ]
175
+ }
176
+ end
177
+ end
178
+
128
179
  end
129
180
 
130
181
  # expands out sources, taking the Glob paths and turning them into separate entries in the array
@@ -133,7 +184,7 @@ module Sprite
133
184
  @images.each do |image|
134
185
  # expand out all the globs
135
186
  image['sources'] = image['sources'].to_a.map{ |source|
136
- Dir.glob(File.join(Sprite.root, @config['source_path'], source))
187
+ Dir.glob(File.join(Sprite.root, config['public_path'], @config['image_source_path'], source))
137
188
  }.flatten.compact
138
189
  end
139
190
  end
data/sprite.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{sprite}
8
- s.version = "0.1.1"
8
+ s.version = "0.1.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Jacques Crocker", "Richard Huang"]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sprite
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jacques Crocker