@@ -4,26 +4,43 @@ class Builder
4
4
DEFAULT_IMAGE_PATH = 'public/images/'
5
5
DEFAULT_FILE_PATH = 'tmp/sprite.css'
6
6
7
- def initialize ( settings = nil )
8
- @image_path = DEFAULT_IMAGE_PATH
9
-
10
- if settings . is_a? ( Hash )
11
- @sprite_config = settings
7
+ attr_reader :config
8
+ attr_reader :images
9
+
10
+ def initialize ( config = nil , images = nil )
11
+ results = config_results
12
+
13
+ # use the override
14
+ if config . is_a? ( Hash )
15
+ @config = config
16
+ else
17
+ @config = config [ "config" ] || { }
18
+ end
19
+
20
+ # set defaults
21
+ set_config_defaults
22
+
23
+ # default image list
24
+ if images . is_a? ( Array )
25
+ @images = images
12
26
else
13
- @sprite_config = read_sprite_config ( settings )
27
+ @images = config [ "images" ] || [ ]
14
28
end
29
+
30
+ # process images
31
+ process_image_settings
15
32
end
16
33
17
34
def build
18
35
@output = { }
19
36
20
- # build up settings
37
+ # build up config
21
38
@image_path = sprite_config [ 'config' ] [ 'base_image_path' ] ? Sprite . root +"/" +sprite_config [ 'config' ] [ 'base_image_path' ] +"/" : DEFAULT_IMAGE_PATH
22
39
@file_path = sprite_config [ 'config' ] [ 'output_file' ] ? Sprite . root +"/" +sprite_config [ 'config' ] [ 'output_file' ] : DEFAULT_FILE_PATH
23
40
24
41
# create images
25
42
sprite_config [ 'images' ] . each do |configuration |
26
- output_image ( configuration )
43
+ output_image ( configuration )
27
44
end
28
45
29
46
# write css
@@ -32,24 +49,26 @@ def build
32
49
33
50
def output_image ( configuration )
34
51
results = [ ]
35
- sources = configuration [ 'sources' ] . collect { |source | Dir . glob ( @image_path +source ) } . flatten
52
+ sources = configuration [ 'sources' ] . to_a
53
+
36
54
dest = configuration [ 'target' ] || sources [ 0 ] . gsub ( /\. / , "_sprite." )
37
55
spaced_by = configuration [ 'spaced_by' ] || 0
38
- dest_image = ImageUtil . get_image ( sources . shift )
39
- results << ImageUtil . image_properties ( dest_image ) . merge ( :x => 0 , :y => 0 )
56
+
57
+ combiner = ImageCombiner . new
58
+
59
+ dest_image = combiner . get_image ( sources . shift )
60
+ results << combiner . image_properties ( dest_image ) . merge ( :x => 0 , :y => 0 )
40
61
sources . each do |source |
41
- source_image = ImageUtil . get_image ( source )
42
- if configuration [ 'align' ] == 'horizontal'
43
- gravity = Magick ::EastGravity
62
+ source_image = combiner . get_image ( source )
63
+ if configuration [ 'align' ] . to_s == 'horizontal'
44
64
x = dest_image . columns + spaced_by
45
65
y = 0
46
66
else
47
- gravity = Magick ::SouthGravity
48
67
x = 0
49
68
y = dest_image . rows + spaced_by
50
69
end
51
- results << ImageUtil . image_properties ( source_image ) . merge ( :x => x , :y => y )
52
- dest_image = ImageUtil . composite_images ( dest_image , source_image , x , y )
70
+ results << combiner . image_properties ( source_image ) . merge ( :x => x , :y => y )
71
+ dest_image = combiner . composite_images ( dest_image , source_image , x , y )
53
72
end
54
73
@output [ dest ] = results
55
74
dest_image . write ( @image_path + dest )
@@ -71,17 +90,50 @@ def output_file(configuration)
71
90
72
91
protected
73
92
74
- # reads config settings from the given path
75
- def read_sprite_config ( path = nil )
93
+ # reads config config from the given path
94
+ def read_config ( path = nil )
95
+ config_results = { }
76
96
config_path = File . join ( Sprite . root , path || DEFAULT_CONFIG_PATH )
77
97
begin
78
- config = File . open ( config_path ) { |f | YAML ::load ( f ) }
98
+ config_results = File . open ( config_path ) { |f | YAML ::load ( f ) }
79
99
rescue => e
80
100
puts "Unable to read sprite config: #{ Sprite . root +"/" +config_path } "
81
101
puts e . to_s
82
102
end
83
- config
84
- rescue
103
+ config_results
85
104
end
105
+
106
+ # sets all the default values on the config
107
+ def set_config_defaults
108
+ @config [ 'style' ] ||= ''
109
+ @config [ 'output_path' ] ||= 'public/sass/mixins/sprites'
110
+ @config [ 'image_output_path' ] ||= 'public/images/sprite/'
111
+ @config [ 'source_path' ] ||= ''
112
+ @config [ 'default_format' ] ||= 'png'
113
+ @config [ 'class_separator' ] ||= '_'
114
+ end
115
+
116
+ # expands out sources
117
+ def process_image_config
118
+ # cycle through image sources and expand out globs
119
+ @images . each do |image |
120
+ # find all the files
121
+ image [ 'sources' ] = image [ 'sources' ] . to_a . map do |source |
122
+ Dir . glob ( File . join ( Sprite . root , @config [ 'source_path' ] , source ) )
123
+ end
124
+
125
+ # remove the prefix on them
126
+ new_sources = new_sources . flatten . map do |source |
127
+ source . gsub! ( Sprite . root , "" )
128
+ end
129
+
130
+ image_config [ 'sources' ] = new_sources
131
+ end
132
+
133
+ rescue => e
134
+ puts "Invalid sprite configuration syntax:"
135
+ puts e . to_s
136
+ end
137
+
86
138
end
87
139
end
0 commit comments