Skip to content

Commit a06d1f1

Browse files
committed
Reorganizing the configuration so its relative to a public folder (overridable)
1 parent 73fbc7a commit a06d1f1

File tree

7 files changed

+157
-93
lines changed

7 files changed

+157
-93
lines changed

README.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,10 @@ Configuration of `sprite` is done via `config/sprite.yml`. It allows you to set
9090

9191
* `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
9292
- `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/`)
9697
- `sprites_class:` defines the class name that gets added to all sprite stylesheet rules (defaults to `sprites`)
9798
- `default_format:` defines the default file image format of the generated files. (defaults to `png`)
9899
- `class_separator:` used to generated the class name by separating the image name and sprite name (defaults to `-`)
@@ -104,17 +105,18 @@ Configuration of `sprite` is done via `config/sprite.yml`. It allows you to set
104105
- `spaced_by:` spacing (in pixels) between the combined images. (defaults to `0`)
105106
- `format:` define what image file format gets created (optional, uses `default_format` setting if not set)
106107

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).
108109

109110
### Sample Configuration `config/sprite.yml` ###
110111

111112
# defines the base configuration options (file paths, etc, default style, etc)
112113

113114
config:
114115
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/
118120
sprites_class: 'sprites'
119121
class_separator: '-'
120122
default_format: png

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.1.1
1+
0.1.2

lib/sprite/builder.rb

Lines changed: 79 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,19 @@ class Builder
88
attr_reader :output
99

1010
def self.from_config(path = nil)
11-
1211
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)
2013

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+
2124
new(results["config"], results["images"])
2225
end
2326

@@ -26,12 +29,9 @@ def initialize(config = nil, images = nil)
2629
set_config_defaults
2730

2831
@images = images || []
32+
set_image_defaults
2933
expand_image_paths
3034

31-
# freeze hashes
32-
@config = @config.dup.freeze
33-
@images = @images.dup.freeze
34-
3535
# initialize output
3636
@output = {}
3737
end
@@ -49,7 +49,8 @@ def build
4949
output_file
5050
end
5151
end
52-
52+
53+
protected
5354
def output_image(image)
5455
results = []
5556
sources = image['sources'].to_a
@@ -74,19 +75,19 @@ def output_image(image)
7475
results << combiner.image_properties(source_image).merge(:x => x, :y => y, :group => name)
7576
dest_image = combiner.composite_images(dest_image, source_image, x, y)
7677
end
77-
@output[name] = results
7878

7979
# set up path
80-
path = image_path(name, image['format'])
80+
path = image_output_path(name, image['format'] || config["default_format"])
8181
FileUtils.mkdir_p(File.dirname(path))
8282

8383
# write sprite image file to disk
8484
dest_image.write(path)
85+
@output[name] = results
8586
end
86-
87+
8788
def output_file
8889
# set up path
89-
path = output_path("css")
90+
path = style_output_path("css")
9091
FileUtils.mkdir_p(File.dirname(path))
9192

9293
# set up class_name to append to each rule
@@ -97,7 +98,7 @@ def output_file
9798
@output.each do |dest, results|
9899
results.each do |result|
99100
f.puts "#{sprites_class}.#{result[:group]}#{config['class_separator']}#{result[:name]} {"
100-
f.puts " background: url('http://github.com/%3Cspan%20class=%22x%20x-first%20x-last%22%3Eimages/%3C/span%3E%3Cspan%20class=%22pl-s1%22%3E%3Cspan%20class=%22pl-kos%22%3E#{%3C/span%3E%3Cspan%20class=%22pl-s1%22%3Edest%3C/span%3E%3Cspan%20class=%22pl-kos%22%3E}%3C/span%3E%3C/span%3E') no-repeat #{result[:x]}px #{result[:y]}px;"
101+
f.puts " background: url('http://github.com/%3Cspan%20class=%22pl-s1%22%3E%3Cspan%20class=%22pl-kos%20x%20x-first%22%3E#{%3C/span%3E%3Cspan%20class=%22pl-en%20x%22%3Econfig%3C/span%3E%3Cspan%20class=%22pl-kos%20x%22%3E[%3C/span%3E%3Cspan%20class=%22pl-s%20x%22%3E'image_output_path'%3C/span%3E%3Cspan%20class=%22pl-kos%20x%22%3E]%3C/span%3E%3Cspan%20class=%22pl-kos%20x%20x-last%22%3E}%3C/span%3E%3C/span%3E%3Cspan%20class=%22pl-s1%22%3E%3Cspan%20class=%22pl-kos%22%3E#{%3C/span%3E%3Cspan%20class=%22pl-s1%22%3Edest%3C/span%3E%3Cspan%20class=%22pl-kos%22%3E}%3C/span%3E%3C/span%3E') no-repeat #{result[:x]}px #{result[:y]}px;"
101102
f.puts " width: #{result[:width]}px;"
102103
f.puts " height: #{result[:height]}px;"
103104
f.puts "}"
@@ -106,25 +107,75 @@ def output_file
106107
end
107108
end
108109

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
110118

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))
113125
end
114126

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
117141
end
118142

119143
# sets all the default values on the config
120144
def set_config_defaults
121145
@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/'
125150
@config['default_format'] ||= 'png'
126151
@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+
128179
end
129180

130181
# expands out sources, taking the Glob paths and turning them into separate entries in the array
@@ -133,7 +184,7 @@ def expand_image_paths
133184
@images.each do |image|
134185
# expand out all the globs
135186
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))
137188
}.flatten.compact
138189
end
139190
end

spec/resources/configs/android-icons.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
config:
22
style: css
3-
output_path: output/stylesheets/android-icons
3+
style_output_path: output/stylesheets/android-icons
44
image_output_path: output/images/sprites/
5-
source_path: resources/images/
5+
image_source_path: resources/images/
6+
public_path: ''
67
class_separator: '-'
78
default_format: png
89

spec/resources/configs/config-test.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
config:
22
style: css
3-
output_path: output/stylesheets/sprites
3+
style_output_path: output/stylesheets/sprites
44
image_output_path: output/images/sprites/
5-
source_path: resources/images/
5+
image_source_path: resources/images/
6+
public_path: ''
67
class_separator: '-'
78
default_format: png
89

spec/sprite/builder_spec.rb

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,6 @@
11
require File.dirname(__FILE__) + '/../spec_helper.rb'
22

33
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 "should load the settings keys from file" do
11-
@sprite.config.keys.size.should == 7
12-
end
13-
14-
it "should load the image keys from file" do
15-
@sprite.images.size.should == 2
16-
end
17-
18-
it "should expand any globs within the source paths" do
19-
@sprite.images.first["sources"].size.should == 30
20-
end
21-
22-
end
23-
24-
context "default settings" do
25-
before(:all) do
26-
@sprite = Sprite::Builder.new
27-
end
28-
29-
it "'style:' setting should default to 'css'" do
30-
@sprite.config['style'].should == "css"
31-
end
32-
33-
it "'output_path:' setting should default to 'public/stylesheets/sprites'" do
34-
@sprite.config['output_path'].should == "public/stylesheets/sprites"
35-
end
36-
37-
it "'image_output_path:' setting should default to 'public/images/sprites/'" do
38-
@sprite.config['image_output_path'].should == "public/images/sprites/"
39-
end
40-
41-
it "'source_path:' setting should default to 'public/images/'" do
42-
@sprite.config['source_path'].should == "public/images/"
43-
end
44-
45-
it "'default_format:' setting should default to 'png'" do
46-
@sprite.config['default_format'].should == "png"
47-
end
48-
49-
it "'sprites_class:' setting should default to 'sprites'" do
50-
@sprite.config['sprites_class'].should == "sprites"
51-
end
52-
53-
it "'class_separator:' setting should default to '-'" do
54-
@sprite.config['class_separator'].should == "-"
55-
end
56-
end
574

585
context "generate android icon sprites" do
596
before(:all) do

spec/sprite/config_spec.rb

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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 "should load the settings keys from file" do
11+
@sprite.config.keys.size.should == 8
12+
end
13+
14+
it "should load the image keys from file" do
15+
@sprite.images.size.should == 2
16+
end
17+
18+
it "should expand any globs within the source paths" do
19+
@sprite.images.first["sources"].size.should == 30
20+
end
21+
22+
end
23+
24+
context "default settings" do
25+
before(:all) do
26+
@sprite = Sprite::Builder.new
27+
end
28+
29+
it "'style:' setting should default to 'css'" do
30+
@sprite.config['style'].should == "css"
31+
end
32+
33+
it "'style_output_path:' setting should default to 'stylesheets/sprites'" do
34+
@sprite.config['style_output_path'].should == "stylesheets/sprites"
35+
end
36+
37+
it "'image_output_path:' setting should default to 'images/sprites/'" do
38+
@sprite.config['image_output_path'].should == "images/sprites/"
39+
end
40+
41+
it "'image_source_path:' setting should default to 'images/'" do
42+
@sprite.config['image_source_path'].should == "images/"
43+
end
44+
45+
it "'public_path:' setting should default to 'public/'" do
46+
@sprite.config['public_path'].should == "public/"
47+
end
48+
49+
it "'default_format:' setting should default to 'png'" do
50+
@sprite.config['default_format'].should == "png"
51+
end
52+
53+
it "'sprites_class:' setting should default to 'sprites'" do
54+
@sprite.config['sprites_class'].should == "sprites"
55+
end
56+
57+
it "'class_separator:' setting should default to '-'" do
58+
@sprite.config['class_separator'].should == "-"
59+
end
60+
end
61+
62+
end

0 commit comments

Comments
 (0)