Skip to content

Commit 43af5ca

Browse files
committed
Separating out generators
1 parent 54a8efc commit 43af5ca

File tree

7 files changed

+103
-20
lines changed

7 files changed

+103
-20
lines changed

lib/sprite.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ def self.root
2020

2121
require 'sprite/builder'
2222
require 'sprite/image_combiner'
23+
require 'sprite/styles'

lib/sprite/builder.rb

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ class Builder
55

66
attr_reader :config
77
attr_reader :images
8-
attr_reader :output
98

109
def self.from_config(path = nil)
1110
results = {}
@@ -63,7 +62,7 @@ def output_image(image)
6362
combiner = ImageCombiner.new
6463

6564
dest_image = combiner.get_image(sources.shift)
66-
results << combiner.image_properties(dest_image).merge(:x => 0, :y => 0)
65+
results << combiner.image_properties(dest_image).merge(:x => 0, :y => 0, :group => name)
6766
sources.each do |source|
6867
source_image = combiner.get_image(source)
6968
if image['align'].to_s == 'horizontal'
@@ -87,25 +86,16 @@ def output_image(image)
8786
end
8887

8988
def output_file
89+
style = Styles.get(config["style"]).new(self)
90+
9091
# set up path
91-
path = style_output_path("css")
92+
path = style_output_path(style.extension)
9293
FileUtils.mkdir_p(File.dirname(path))
9394

94-
# set up class_name to append to each rule
95-
sprites_class = config['sprites_class'] ? ".#{config['sprites_class']}" : ""
96-
97-
# write stylesheet file to disk
95+
# write styles to disk
9896
File.open(path, 'w') do |f|
99-
@output.each do |dest, results|
100-
results.each do |result|
101-
f.puts "#{sprites_class}.#{result[:group]}#{config['class_separator']}#{result[:name]} {"
102-
f.puts " background: url(http://github.com/'/%3Cspan%20class=pl-s1%3E%3Cspan%20class=pl-kos%3E#{%3C/span%3E%3Cspan%20class=pl-en%3Econfig%3C/span%3E%3Cspan%20class=pl-kos%3E[%3C/span%3E%3Cspan%20class=pl-s%3E'image_output_path'%3C/span%3E%3Cspan%20class=pl-kos%3E]%3C/span%3E%3Cspan%20class=pl-kos%3E}%3C/span%3E%3C/span%3E%3Cspan%20class=pl-s1%3E%3Cspan%20class=pl-kos%3E#{%3C/span%3E%3Cspan%20class=pl-s1%3Edest%3C/span%3E%3Cspan%20class=pl-kos%3E}%3C/span%3E%3C/span%3E') no-repeat #{result[:x]}px #{result[:y]}px;"
103-
f.puts " width: #{result[:width]}px;"
104-
f.puts " height: #{result[:height]}px;"
105-
f.puts "}"
106-
end
107-
end
108-
end
97+
f << style.generate(@output)
98+
end
10999
end
110100

111101
# get the disk path for the style output file
@@ -144,9 +134,9 @@ def chop_trailing_slash(path)
144134
# sets all the default values on the config
145135
def set_config_defaults
146136
@config['style'] ||= 'css'
147-
@config['style_output_path'] ||= 'stylesheets/sprites'
137+
@config['style_output_path'] ||= 'stylesheets/sprites'
148138
@config['image_output_path'] ||= 'images/sprites/'
149-
@config['image_source_path'] ||= 'images/'
139+
@config['image_source_path'] ||= 'images/'
150140
@config['public_path'] ||= 'public/'
151141
@config['default_format'] ||= 'png'
152142
@config['class_separator'] ||= '-'

lib/sprite/image_combiner.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ def composite_images(dest_image, src_image, x, y)
1616
image
1717
end
1818

19-
2019
# Image Utility Methods
2120
def get_image(image_filename)
2221
image = Magick::Image::read(image_filename).first

lib/sprite/styles.rb

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
module Sprite::Styles
2+
GENERATORS = {
3+
"css" => "CssGenerator",
4+
"sass" => "SassGenerator",
5+
'sass_mixin' => "SassMixinGenerator"
6+
}
7+
8+
def self.get(config)
9+
const_get(GENERATORS[config])
10+
rescue
11+
CssGenerator
12+
end
13+
14+
# renders standard css style rules
15+
class CssGenerator
16+
def initialize(builder)
17+
@builder = builder
18+
end
19+
20+
def generate(sprite_files)
21+
output = ""
22+
23+
# set up class_name to append to each rule
24+
sprites_class = @builder.config['sprites_class'] ? ".#{@builder.config['sprites_class']}" : ""
25+
26+
# write stylesheet file to disk
27+
sprite_files.each do |sprite_file, sprites|
28+
sprites.each do |sprite|
29+
output << "#{sprites_class}.#{sprite[:group]}#{@builder.config['class_separator']}#{sprite[:name]} {\n"
30+
output << " background: url(http://github.com/'/%3Cspan%20class=pl-s1%3E%3Cspan%20class=pl-kos%3E#{%3C/span%3E%3Cspan%20class=pl-c1%3E@builder%3C/span%3E%3Cspan%20class=pl-kos%3E.%3C/span%3E%3Cspan%20class=pl-en%3Econfig%3C/span%3E%3Cspan%20class=pl-kos%3E[%3C/span%3E%3Cspan%20class=pl-s%3E'image_output_path'%3C/span%3E%3Cspan%20class=pl-kos%3E]%3C/span%3E%3Cspan%20class=pl-kos%3E}%3C/span%3E%3C/span%3E%3Cspan%20class=pl-s1%3E%3Cspan%20class=pl-kos%3E#{%3C/span%3E%3Cspan%20class=pl-s1%3Esprite_file%3C/span%3E%3Cspan%20class=pl-kos%3E}%3C/span%3E%3C/span%3E') no-repeat #{sprite[:x]}px #{sprite[:y]}px;\n"
31+
output << " width: #{sprite[:width]}px;\n"
32+
output << " height: #{sprite[:height]}px;\n"
33+
output << "}\n"
34+
end
35+
end
36+
37+
output
38+
end
39+
40+
def extension
41+
"css"
42+
end
43+
end
44+
45+
# renders standard sass rules
46+
class SassGenerator
47+
def initialize(builder)
48+
@builder = builder
49+
end
50+
51+
def generate(sprite_files)
52+
output = ""
53+
54+
# set up class_name to append to each rule
55+
sprites_class = @builder.config['sprites_class'] ? ".#{@builder.config['sprites_class']}" : ""
56+
57+
output << ".#{sprites_class}"
58+
sprite_files.each do |sprite_file, sprites|
59+
sprites.each do |sprite|
60+
output << " &.#{sprite[:group]}#{@builder.config['class_separator']}#{sprite[:name]}\n"
61+
output << " background: url(http://github.com/'/%3Cspan%20class=pl-s1%3E%3Cspan%20class=pl-kos%3E#{%3C/span%3E%3Cspan%20class=pl-c1%3E@builder%3C/span%3E%3Cspan%20class=pl-kos%3E.%3C/span%3E%3Cspan%20class=pl-en%3Econfig%3C/span%3E%3Cspan%20class=pl-kos%3E[%3C/span%3E%3Cspan%20class=pl-s%3E'image_output_path'%3C/span%3E%3Cspan%20class=pl-kos%3E]%3C/span%3E%3Cspan%20class=pl-kos%3E}%3C/span%3E%3C/span%3E%3Cspan%20class=pl-s1%3E%3Cspan%20class=pl-kos%3E#{%3C/span%3E%3Cspan%20class=pl-s1%3Esprite_file%3C/span%3E%3Cspan%20class=pl-kos%3E}%3C/span%3E%3C/span%3E') no-repeat #{sprite[:x]}px #{sprite[:y]}px\n"
62+
output << " width: #{sprite[:width]}px\n"
63+
output << " height: #{sprite[:height]}px\n"
64+
output << "\n"
65+
end
66+
end
67+
68+
output
69+
end
70+
71+
def extension
72+
"sass"
73+
end
74+
end
75+
76+
# renders a yml file that is parsed by a sass extension
77+
class SassMixinGenerator
78+
def initialize(builder)
79+
@builder = builder
80+
end
81+
82+
def generate(output)
83+
""
84+
end
85+
86+
def extension
87+
"yml"
88+
end
89+
90+
end
91+
92+
93+
end

spec/sprite/styles/css_spec.rb

Whitespace-only changes.

spec/sprite/styles/sass_mixin_spec.rb

Whitespace-only changes.

spec/sprite/styles/sass_spec.rb

Whitespace-only changes.

0 commit comments

Comments
 (0)