Skip to content

Commit 7a40b77

Browse files
committed
Reorganizing style outputs
1 parent 631e354 commit 7a40b77

File tree

6 files changed

+192
-86
lines changed

6 files changed

+192
-86
lines changed

lib/sprite/builder.rb

Lines changed: 60 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -31,26 +31,28 @@ def initialize(config = nil, images = nil)
3131
set_image_defaults
3232
expand_image_paths
3333

34-
# initialize output
35-
@output = {}
34+
# initialize sprite files
35+
@sprite_files = {}
3636
end
3737

3838
def build
39-
@output = {}
39+
@sprite_files = {}
4040

4141
if images.size > 0
4242
# create images
4343
images.each do |image|
44-
output_image(image)
44+
write_image(image)
4545
end
4646

47-
# write css
48-
output_file
47+
if @sprite_files.values.length > 0
48+
# write css
49+
write_styles
50+
end
4951
end
5052
end
5153

5254
protected
53-
def output_image(image)
55+
def write_image(image)
5456
results = []
5557
sources = image['sources'].to_a
5658
return unless sources.length > 0
@@ -82,53 +84,17 @@ def output_image(image)
8284

8385
# write sprite image file to disk
8486
dest_image.write(path)
85-
@output["#{name}.#{format}"] = results
87+
@sprite_files["#{name}.#{format}"] = results
8688
end
87-
88-
def output_file
89+
90+
def write_styles
8991
style = Styles.get(config["style"]).new(self)
9092

9193
# set up path
9294
path = style_output_path(style.extension)
9395
FileUtils.mkdir_p(File.dirname(path))
9496

95-
# write styles to disk
96-
File.open(path, 'w') do |f|
97-
f << style.generate(@output)
98-
end
99-
end
100-
101-
# get the disk path for the style output file
102-
def style_output_path(file_ext)
103-
path = config['style_output_path']
104-
unless path.include?(".#{file_ext}")
105-
path = "#{path}.#{file_ext}"
106-
end
107-
public_path(path)
108-
end
109-
110-
# get the disk path for a location within the image output folder
111-
def image_output_path(name, format)
112-
path_parts = []
113-
path_parts << chop_trailing_slash(config['image_output_path'])
114-
path_parts << "#{name}.#{format}"
115-
public_path(File.join(*path_parts))
116-
end
117-
118-
# get the disk path for a location within the public folder (if set)
119-
def public_path(location)
120-
path_parts = []
121-
path_parts << Sprite.root
122-
path_parts << chop_trailing_slash(config['public_path']) if config['public_path'] and config['public_path'].length > 0
123-
path_parts << location
124-
125-
File.join(*path_parts)
126-
end
127-
128-
# chop off the trailing slash on a directory path (if it exists)
129-
def chop_trailing_slash(path)
130-
path = path[0...-1] if path[-1] == File::SEPARATOR
131-
path
97+
style.write(path, @sprite_files)
13298
end
13399

134100
# sets all the default values on the config
@@ -147,7 +113,7 @@ def set_config_defaults
147113
def set_image_defaults
148114
return unless @images.size == 0
149115

150-
sprites_path = File.join(Sprite.root, config['public_path'], config['image_source_path'], "sprites")
116+
sprites_path = image_source_path("sprites")
151117

152118
if File.exists?(sprites_path)
153119
Dir.glob(File.join(sprites_path, "*")) do |dir|
@@ -166,7 +132,6 @@ def set_image_defaults
166132
}
167133
end
168134
end
169-
170135
end
171136

172137
# expands out sources, taking the Glob paths and turning them into separate entries in the array
@@ -175,10 +140,55 @@ def expand_image_paths
175140
@images.each do |image|
176141
# expand out all the globs
177142
image['sources'] = image['sources'].to_a.map{ |source|
178-
Dir.glob(File.join(Sprite.root, config['public_path'], @config['image_source_path'], source))
143+
Dir.glob(image_source_path(source))
179144
}.flatten.compact
180145
end
181146
end
182147

148+
# get the disk path for the style output file
149+
def style_output_path(file_ext, relative = false)
150+
path = config['style_output_path']
151+
unless path.include?(".#{file_ext}")
152+
path = "#{path}.#{file_ext}"
153+
end
154+
public_path(path, relative)
155+
end
156+
157+
# get the disk path for a location within the image output folder
158+
def image_output_path(name, format, relative = false)
159+
path_parts = []
160+
path_parts << chop_trailing_slash(config['image_output_path']) if path_present?(config['image_output_path'])
161+
path_parts << "#{name}.#{format}"
162+
public_path(File.join(*path_parts), relative)
163+
end
164+
165+
# get the disk path for an image source file
166+
def image_source_path(location, relative = false)
167+
path_parts = []
168+
path_parts << chop_trailing_slash(config["image_source_path"]) if path_present?(config['image_source_path'])
169+
path_parts << location
170+
public_path(File.join(*path_parts), relative)
171+
end
172+
173+
# get the disk path for a location within the public folder (if set)
174+
def public_path(location, relative = false)
175+
path_parts = []
176+
path_parts << Sprite.root unless relative
177+
path_parts << chop_trailing_slash(config['public_path']) if path_present?(config['public_path'])
178+
path_parts << location
179+
180+
File.join(*path_parts)
181+
end
182+
183+
# chop off the trailing slash on a directory path (if it exists)
184+
def chop_trailing_slash(path)
185+
path = path[0...-1] if path[-1] == File::SEPARATOR
186+
path
187+
end
188+
189+
# check if the path is set
190+
def path_present?(path)
191+
path.to_s.strip != ""
192+
end
183193
end
184194
end

lib/sprite/image_combiner.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ def get_image(image_filename)
2424
def image_properties(image)
2525
{:name => File.basename(image.filename).split('.')[0], :width => image.columns, :height => image.rows}
2626
end
27+
28+
# REMOVE RMAGICK AND USE IMAGEMAGICK FROM THE COMMAND LINE
29+
# identify => find properties for an image
30+
# composite => combine 2 images
2731

2832
end
2933
end

lib/sprite/sass_extensions.rb

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
module Sprite::Sass::Extensions
2+
def sprite_background(group, image)
3+
sprite = sprite_data(group, image)
4+
# TODO
5+
""
6+
end
7+
8+
def sprite_width(group, image)
9+
sprite = sprite_data(group, image)
10+
# TODO
11+
""
12+
end
13+
14+
def sprite_height(group, image)
15+
sprite = sprite_data(group, image)
16+
# TODO
17+
""
18+
end
19+
20+
protected
21+
def sprite_data(group, image)
22+
unless @__sprite_data
23+
@__sprite_data = {
24+
"group_name" => {
25+
"image_name" => {
26+
:width => "",
27+
:height => "",
28+
:group => "dkajsdfk",
29+
:x => "",
30+
:y => ""
31+
}
32+
}
33+
}
34+
end
35+
36+
group_data = @__sprite_data[group]
37+
if group_data
38+
return group_data[image]
39+
else
40+
nil
41+
end
42+
end
43+
44+
end
45+
46+
if defined?(Sass)
47+
module Sass::Script::Functions
48+
include Sprite::Sass::Extensions
49+
end
50+
end

lib/sprite/styles/css_generator.rb

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,23 @@ def initialize(builder)
66
@builder = builder
77
end
88

9-
def generate(sprite_files)
10-
output = ""
11-
9+
def write(path, sprite_files)
1210
# set up class_name to append to each rule
1311
sprites_class = @builder.config['sprites_class'] ? ".#{@builder.config['sprites_class']}" : ""
1412

15-
# write stylesheet file to disk
16-
sprite_files.each do |sprite_file, sprites|
17-
sprites.each do |sprite|
18-
output << "#{sprites_class}.#{sprite[:group]}#{@builder.config['class_separator']}#{sprite[:name]} {\n"
19-
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"
20-
output << " width: #{sprite[:width]}px;\n"
21-
output << " height: #{sprite[:height]}px;\n"
22-
output << "}\n"
13+
# write styles to disk
14+
File.open(path, 'w') do |f|
15+
# write stylesheet file to disk
16+
sprite_files.each do |sprite_file, sprites|
17+
sprites.each do |sprite|
18+
f.puts "#{sprites_class}.#{sprite[:group]}#{@builder.config['class_separator']}#{sprite[:name]} {"
19+
f.puts " 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;"
20+
f.puts " width: #{sprite[:width]}px;"
21+
f.puts " height: #{sprite[:height]}px;"
22+
f.puts "}"
23+
end
2324
end
24-
end
25-
26-
output
25+
end
2726
end
2827

2928
def extension

lib/sprite/styles/sass_generator.rb

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,32 @@ def initialize(builder)
77
@builder = builder
88
end
99

10-
def generate(sprite_files)
11-
output = ""
12-
13-
# set up class_name to append to each rule
14-
sprites_class = @builder.config['sprites_class'] ? ".#{@builder.config['sprites_class']}" : ""
15-
16-
output << "#{sprites_class}\n"
17-
sprite_files.each do |sprite_file, sprites|
18-
sprites.each do |sprite|
19-
output << " &.#{sprite[:group]}#{@builder.config['class_separator']}#{sprite[:name]}\n"
20-
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"
21-
output << " width: #{sprite[:width]}px\n"
22-
output << " height: #{sprite[:height]}px\n"
23-
output << "\n"
10+
def write(path, sprite_files)
11+
@level = 0
12+
13+
File.open(path, 'w') do |f|
14+
if @builder.config['sprites_class']
15+
f.puts ".#{@builder.config['sprites_class']}"
16+
@level += 1
17+
end
18+
19+
sprite_files.each do |sprite_file, sprites|
20+
sprites.each do |sprite|
21+
f.puts sass_line("&.#{sprite[:group]}#{@builder.config['class_separator']}#{sprite[:name]}")
22+
@level += 1
23+
f.puts sass_line("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")
24+
f.puts sass_line("width: #{sprite[:width]}px")
25+
f.puts sass_line("height: #{sprite[:height]}px")
26+
f.puts sass_line("")
27+
@level -= 1
28+
end
2429
end
2530
end
26-
27-
output
31+
end
32+
33+
# write sass output with correct tab spaces prepended
34+
def sass_line(sass)
35+
"#{' '*@level}#{sass}"
2836
end
2937

3038
def extension

lib/sprite/styles/sass_mixin_generator.rb

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,52 @@
1+
require 'yaml'
12
module Sprite
23
module Styles
3-
# renders a yml file that is parsed by a sass extension
4+
# renders a yml file that is later parsed by a sass extension when generating the mixins
45
class SassMixinGenerator
56
def initialize(builder)
67
@builder = builder
78
end
8-
9-
def generate(output)
10-
"NOT DONE YET"
9+
10+
def write(path, sprite_files)
11+
# build the yml file
12+
config_location = write_config(path, sprite_files)
13+
14+
# write the sass mixins to disk
15+
File.open(path, 'w') do |f|
16+
f.puts "!sprite_data = '#{config_location}'"
17+
f.puts ""
18+
f.puts "= sprite(!group_name, !image_name)"
19+
f.puts " background= sprite_background(!group_name, !image_name)"
20+
f.puts " width= sprite_width(!group_name, !image_name)"
21+
f.puts " height= sprite_height(!group_name, !image_name)"
22+
f.puts ""
23+
end
1124
end
12-
25+
26+
# write the sprite configuration file (used by the yml extension)
27+
def write_config(path, sprite_files)
28+
# build a grouped hash with all the sprites in it
29+
result = {}
30+
sprite_files.each do |sprite_file, sprites|
31+
sprites.each do |sprite|
32+
if sprite[:group]
33+
result[sprite[:group]] ||= {}
34+
result[sprite[:group]][sprite[:name]] = sprite
35+
end
36+
end
37+
end
38+
39+
# write the config yml to disk
40+
config_path = path.gsub(".sass", ".yml")
41+
File.open(config_path, 'w') do |f|
42+
YAML.dump(result, f)
43+
end
44+
45+
config_path
46+
end
47+
1348
def extension
14-
"yml"
49+
"sass"
1550
end
1651

1752
end

0 commit comments

Comments
 (0)