Skip to content

Commit

Permalink
Updating default class separator to be dash instead of underscore
Browse files Browse the repository at this point in the history
  • Loading branch information
merbjedi committed Nov 18, 2009
1 parent 04dac31 commit e3f3244
Show file tree
Hide file tree
Showing 39 changed files with 48 additions and 38 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,9 @@ Configuration of `sprite` is done via `config/sprite.yml`. It allows you to set
- `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
- `image_output_path:` defines the folder path where the combined sprite images files are written (defaults to `public/images/sprites/`)
- `source_path:` defines the folder where source image files are read from (defaults to `public/images/`)
- `sprites_class:` defines the class name that gets added to all sprite stylesheet rules (defaults to `sprites`)
- `default_format:` defines the default file image format of the generated files. (defaults to `png`)
- `class_separator:` used within the generated class names between the image name and sprite name (defaults to `_`)
- `class_separator:` used to generated the class name by separating the image name and sprite name (defaults to `-`)

* `images:` section provides an array of configurations which define which image files are built, and where they get their sprites from. each image setup provides the following config options:
- `name:` name of image (required)
Expand All @@ -114,7 +115,8 @@ you can define any number of destination image files.
output_path: public/sass/mixins/sprites.sass
image_output_path: public/images/sprites/
source_path: public/images/
class_separator: '_'
sprites_class: 'sprites'
class_separator: '-'
default_format: png

# defines what sprite collections get created
Expand Down
20 changes: 12 additions & 8 deletions lib/sprite/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def output_image(image)
x = 0
y = dest_image.rows + spaced_by
end
results << combiner.image_properties(source_image).merge(:x => x, :y => y)
results << combiner.image_properties(source_image).merge(:x => x, :y => y, :group => name)
dest_image = combiner.composite_images(dest_image, source_image, x, y)
end
@output[name] = results
Expand All @@ -89,11 +89,14 @@ def output_file
path = output_path("css")
FileUtils.mkdir_p(File.dirname(path))

# set up class_name to append to each rule
sprites_class = config['sprites_class'] ? ".#{config['sprites_class']}" : ""

# write stylesheet file to disk
File.open(path, 'w') do |f|
@output.each do |dest, results|
results.each do |result|
f.puts ".#{result[:name]} {"
f.puts "#{sprites_class}.#{result[:group]}#{config['class_separator']}#{result[:name]} {"
f.puts " background: url('/images/#{dest}') no-repeat #{result[:x]}px #{result[:y]}px;"
f.puts " width: #{result[:width]}px;"
f.puts " height: #{result[:height]}px;"
Expand All @@ -115,12 +118,13 @@ def image_path(name, format)

# sets all the default values on the config
def set_config_defaults
@config['style'] ||= 'css'
@config['output_path'] ||= 'public/stylesheets/sprites'
@config['image_output_path'] ||= 'public/images/sprites/'
@config['source_path'] ||= 'public/images/'
@config['default_format'] ||= 'png'
@config['class_separator'] ||= '_'
@config['style'] ||= 'css'
@config['output_path'] ||= 'public/stylesheets/sprites'
@config['image_output_path'] ||= 'public/images/sprites/'
@config['source_path'] ||= 'public/images/'
@config['default_format'] ||= 'png'
@config['class_separator'] ||= '-'
@config["sprites_class"] ||= 'sprites'
end

# expands out sources, taking the Glob paths and turning them into separate entries in the array
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
config:
style: css
output_path: output/stylesheets/android_icons
output_path: output/stylesheets/android-icons
image_output_path: output/images/sprites/
source_path: resources/images/
class_separator: '_'
class_separator: '-'
default_format: png

# defines what sprite collections get created
images:
- name: android_icons
- name: android-icons
format: png
align: vertical
spaced_by: 50
sources:
- android_icons/*.png
- android-icons/*.png
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@ config:
output_path: output/stylesheets/sprites
image_output_path: output/images/sprites/
source_path: resources/images/
class_separator: '_'
class_separator: '-'
default_format: png

# defines what sprite collections get created
images:
- name: android_icons
- name: android-icons
format: png
align: vertical
spaced_by: 50
sources:
- android_icons/*.png
- android-icons/*.png

- name: topics
format: gif
align: vertical
spaced_by: 50
sources:
- topics/good_topic.gif
- topics/mid_topic.gif
- topics/good-topic.gif
- topics/mid-topic.gif
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
24 changes: 14 additions & 10 deletions spec/sprite/builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

context "configuration parsing" do
before(:all) do
@sprite = Sprite::Builder.from_config("resources/configs/config_test.yml")
@sprite = Sprite::Builder.from_config("resources/configs/config-test.yml")
end

it "should load the settings keys from file" do
@sprite.config.keys.size.should == 6
@sprite.config.keys.size.should == 7
end

it "should load the image keys from file" do
Expand Down Expand Up @@ -46,30 +46,34 @@
@sprite.config['default_format'].should == "png"
end

it "'class_separator:' setting should default to '_'" do
@sprite.config['class_separator'].should == "_"
it "'sprites_class:' setting should default to 'sprites'" do
@sprite.config['sprites_class'].should == "sprites"
end

it "'class_separator:' setting should default to '-'" do
@sprite.config['class_separator'].should == "-"
end
end

context "generate android icon sprites" do
before(:all) do
clear_output
@sprite = Sprite::Builder.from_config("resources/configs/android_icons.yml")
@sprite = Sprite::Builder.from_config("resources/configs/android-icons.yml")
@sprite.build
end

it "should generate android.png" do
File.exists?("#{Sprite.root}/output/images/sprites/android_icons.png").should be_true
File.exists?("#{Sprite.root}/output/images/sprites/android-icons.png").should be_true
end

it "should generate android_icons.css" do
File.exists?("#{Sprite.root}/output/stylesheets/android_icons.css").should be_true
it "should generate android-icons.css" do
File.exists?("#{Sprite.root}/output/stylesheets/android-icons.css").should be_true
end

context "sprite result image" do
before(:all) do
combiner = Sprite::ImageCombiner.new
@result_image = combiner.get_image("#{Sprite.root}/output/images/sprites/android_icons.png")
@result_image = combiner.get_image("#{Sprite.root}/output/images/sprites/android-icons.png")
@result_properties = combiner.image_properties(@result_image)
end

Expand All @@ -81,7 +85,7 @@

context "sprite result styles" do
before(:all) do
@styles = File.read("#{Sprite.root}/output/stylesheets/android_icons.css")
@styles = File.read("#{Sprite.root}/output/stylesheets/android-icons.css")
end

it "should have some styles in it" do
Expand Down
18 changes: 9 additions & 9 deletions spec/sprite/image_combiner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,36 @@
@combiner = Sprite::ImageCombiner.new

@image_paths = {
:good_topic => "#{Sprite.root}/resources/images/topics/good_topic.gif",
:mid_topic => "#{Sprite.root}/resources/images/topics/mid_topic.gif"
:good => "#{Sprite.root}/resources/images/topics/good-topic.gif",
:mid => "#{Sprite.root}/resources/images/topics/mid-topic.gif"
}
end

context "image handling" do
context "get_image" do
it "should get a image" do
@combiner.get_image(@image_paths[:good_topic]).class.should == Magick::Image
@combiner.get_image(@image_paths[:good]).class.should == Magick::Image
end
end

context "image_properties" do
it "should get image properties" do
image = @combiner.get_image(@image_paths[:good_topic])
@combiner.image_properties(image).should == {:name => 'good_topic', :width => 20, :height => 19}
image = @combiner.get_image(@image_paths[:good])
@combiner.image_properties(image).should == {:name => 'good-topic', :width => 20, :height => 19}
end
end

context "composite_images" do
it "should composite two images into one horizontally" do
image1 = @combiner.get_image(@image_paths[:good_topic])
image2 = @combiner.get_image(@image_paths[:mid_topic])
image1 = @combiner.get_image(@image_paths[:good])
image2 = @combiner.get_image(@image_paths[:mid])
image = @combiner.composite_images(image1, image2, image1.columns, 0)
@combiner.image_properties(image).should == {:name => nil, :width => 40, :height => 19}
end

it "should composite two images into one verically" do
image1 = @combiner.get_image(@image_paths[:good_topic])
image2 = @combiner.get_image(@image_paths[:mid_topic])
image1 = @combiner.get_image(@image_paths[:good])
image2 = @combiner.get_image(@image_paths[:mid])
image = @combiner.composite_images(image1, image2, 0, image1.rows)
@combiner.image_properties(image).should == {:name => nil, :width => 20, :height => 38}
end
Expand Down

0 comments on commit e3f3244

Please sign in to comment.