Skip to content

Commit ddcad0e

Browse files
committed
Refactoring Plugin
1 parent 07f80b3 commit ddcad0e

File tree

7 files changed

+139
-120
lines changed

7 files changed

+139
-120
lines changed

README.md

Lines changed: 104 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1+
WORK IN PROGRESS. This README describes stuff yet to be implemented. Please read it though and msg me feedback if you think things should work differently!
2+
13
# sprite #
24

3-
`sprite` is a gem that helps generate css sprite images automagically. It's aim is to support all web frameworks (Merb/Rails/Sinatra)
5+
`sprite` is a gem that helps generate css sprite images automagically. It's aim is to support all web frameworks (Merb/Rails/Sinatra), and have extensible output generator. By default, it supports CSS and SASS output (via mixins).
46

5-
it's a fork an extension of Richard Huang's excellent Rails plugin `css_sprite` (github.com/flyerhzm/css_sprite)
7+
it's a fork and extension on Richard Huang's excellent Rails specific plugin [`css_sprite`](http://github.com/flyerhzm/css_sprite)
68

7-
****
9+
## Install ##
810

9-
## Install ##
11+
### Install the `rmagick` gem ###
1012

11-
install rmagick gem first:
13+
sprite currently requires the rmagick gem. to install it, use
1214

1315
gem install rmagick
1416

@@ -19,91 +21,131 @@ if you have any problems with the rmagick gem, install imagemagick via macports
1921

2022
or via installer: http://github.com/maddox/magick-installer/tree/master
2123

22-
install it as a gem:
24+
### Install the `sprite` gem ###
25+
26+
Install the sprite gem from gemcutter
2327

2428
gem sources -a http://gemcutter.org
25-
gem install sass_sprite
29+
gem install sprite
2630

27-
### if using merb ###
28-
29-
gem "sprite"
30-
31-
### if using rails ###
31+
### if using Merb ###
3232

33-
script/plugin install git://github.com/merbjedi/sprite.git
33+
With Merb 1.1 and Bundler, just add the line `gem 'sprite'` your ./Gemfile and then run `gem bundle`
34+
35+
### if using Rails ###
3436

35-
or add to environment.rb
37+
add to environment.rb
3638

3739
config.gem "sprite"
3840

39-
***
41+
or install as a plugin
4042

41-
## Configuration ##
42-
43-
add `config/sprite.yml`, define about compositing what images.
44-
45-
forum_icon_vertical.gif: # destination image file
46-
sources: # source image file list
47-
- good_topic.gif
48-
- mid_topic.gif
49-
- unread_topic.gif
50-
- sticky_topic.gif
51-
orient: vertical # composite gravity, vertical or horizontal
52-
span: 5 # span of space between two images
43+
script/plugin install git://github.com/merbjedi/sprite.git
5344

54-
first line defines the destination image filename.
55-
`sources` is a list of source image filenames what want to composite. They are parsed by <code>Dir.glob</code>.
56-
`orient` defines the composite gravity type, horizontal or vertical. Default is 'vertical'.
57-
`span` defines the span between two images. Default is 0.
45+
## Usage ##
5846

59-
you can define any number of destination image files.
47+
if installed as a gem, at your root project folder you can just run
48+
49+
sprite
6050

61-
***
51+
if you would rather not install the gem, you can also use it with rake
6252

63-
## Usage ##
53+
rake sprite:build
6454

65-
if you use it as a gem, add a task `lib/tasks/sprites.rake` first:
55+
## Configuration ##
6656

67-
require 'sprites'
57+
add `config/sprite.yml` to set sprite configuration options and define what sprites get generated where
58+
59+
* `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
60+
- `style:` defines how the style rules are outputted. built in options are `css`, `sass`, and `sass_mixin`. (defaults to `css`)
61+
- `output_path:` defines the file path where your style settings get written. the file extension not needed as it will be determined by the style setting above (defaults to `public/stylesheets/sprite`)
62+
- `image_output_path:` defines the folder path where the combined sprite images files are written (defaults to `public/images/sprite/`)
63+
- `source_path:` defines the folder where source image files are read from (defaults to `public/images/`)
64+
- `default_format:` defines the default file image format of the generated files. (defaults to `png`)
65+
- `class_separator:` used within the generated class names between the image name and sprite name (defaults to `_`)
66+
67+
* `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:
68+
- `name:` name of image (required)
69+
- `sources:` defines a list of source image filenames to build the target image from (required). They are parsed by <code>Dir.glob</code>
70+
- `align:` defines the composite gravity type, horizontal or vertical. (defaults to `vertical`)
71+
- `spaced_by:` spacing (in pixels) between the combined images. (defaults to `0`)
72+
- `format:` define what image file format gets created (optional, uses `default_format` setting if not set)
6873

69-
if you use it as a plugin, ignore the step above.
74+
you can define any number of destination image files.
7075

71-
then just run rake task:
72-
73-
rake sprites:build
76+
## Sample Configuration `config/sprite.yml` ##
7477

75-
the result css is generated at `public/stylesheets/sprite.css`
78+
# defines the base configuration options (file paths, etc, default style, etc)
7679

77-
.good_topic {
78-
background: url(http://github.com/'/images/forum_icon_vertical.gif') no-repeat 0px 0px;
79-
width: 20px;
80-
height: 19px;
80+
config:
81+
style: css
82+
output_path: public/sass/mixins/sprites.sass
83+
image_output_path: public/images/sprite/
84+
source_path: public/images/
85+
class_separator: '_'
86+
default_format: png
87+
88+
# defines what sprite collections get created
89+
images:
90+
91+
# creates a public/images/sprite/blue_stars.png image with 4 sprites in it
92+
- name: blue_stars
93+
format: png
94+
align: horizontal
95+
spaced_by: 50
96+
sources:
97+
- icons/blue_stars/small.png
98+
- icons/blue_stars/medium.png
99+
- icons/blue_stars/large.png
100+
- icons/blue_stars/xlarge.png
101+
102+
# creates a public/images/sprite/green_stars.jpg image with
103+
# all the jpg files contained within /images/icons/green_stars/
104+
- name: green_stars
105+
format: jpg
106+
align: vertical
107+
spaced_by: 50
108+
sources:
109+
- icons/green_stars/*.jpg
110+
111+
## Style Settings ##
112+
113+
By default, it will use with `style: css` and generate the file at `public/stylesheets/sprite.css`
114+
115+
.sprite.blue_stars_small {
116+
background: url(http://github.com/'/images/icons/blue_stars/small.png') no-repeat 0px 0px;
117+
width: 12px;
118+
height: 6px;
81119
}
82-
.mid_topic {
83-
background: url('http://github.com/images/%3Cspan%20class=%22x%20x-first%20x-last%22%3Eforum_icon_vertical.gif%3C/span%3E') no-repeat 0px 24px
84-
width: 20px;
85-
height: 19px;
120+
.sprite.blue_stars_medium {
121+
background: url('http://github.com/images/%3Cspan%20class=%22x%20x-first%20x-last%22%3Eicons/blue_stars/medium.png%3C/span%3E') no-repeat 0px 6px;
122+
width: 30px;
123+
height: 15px;
86124
}
87-
.unread_topic {
88-
background: url('http://github.com/images/%3Cspan%20class=%22x%20x-first%20x-last%22%3Eforum_icon_vertical.gif%3C/span%3E') no-repeat 0px 48px;
89-
width: 19px;
90-
height: 18px;
125+
.sprite.blue_stars_large {
126+
background: url('http://github.com/images/%3Cspan%20class=%22x%20x-first%20x-last%22%3Eicons/blue_stars/large.png%3C/span%3E') no-repeat 0px 21px;
127+
width: 60px;
128+
height: 30px;
91129
}
92-
.sticky_topic {
93-
background: url('http://github.com/images/%3Cspan%20class=%22x%20x-first%20x-last%22%3Eforum_icon_vertical.gif%3C/span%3E') no-repeat 0px 71px;
94-
width: 19px;
95-
height: 18px;
130+
.sprite.blue_stars_xlarge {
131+
background: url('http://github.com/images/%3Cspan%20class=%22x%20x-first%20x-last%22%3Eicons/blue_stars/xlarge.png%3C/span%3E') no-repeat 0px 96px;
132+
width: 100px;
133+
height: 75px;
96134
}
97135

136+
We also support mixin syntax via `style: sass_mixin`. If set, sprite will only generate a yml with your specific sprite configurations. It then provides a SASS mixin which you can use in order to mix in these sprites anywhere within your SASS stylesheets.
98137

99-
***
138+
// you can then use your sprite like this
139+
.largebluestar
140+
+sprite("blue_stars", "large")
100141

101-
## Contributors ##
142+
.mysmallbluestar
143+
+sprite("blue_stars", "small")
102144

103-
merbjedi - reorganized as a general purpose ruby plugin for merb/rails/sinatra/
104-
josedelcorral - fix the style of generated css
145+
## Contributors ##
105146

106-
***
147+
merbjedi - reorganized as a general purpose ruby plugin for merb/rails/sinatra
107148

149+
josedelcorral - fix the style of generated css
108150

109151
Copyright (c) 2009 [Richard Huang ([email protected])], released under the MIT license

config/css_sprite_example_config.yml

Lines changed: 0 additions & 24 deletions
This file was deleted.

lib/sprite.rb

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1 @@
1-
require 'rake'
21
require 'sprite/sprite'
3-
unless Rake::Task.task_defined? "sprite:build"
4-
load File.join(File.dirname(__FILE__), '..', 'tasks', 'sprite_tasks.rake')
5-
end
6-

lib/sprite/sprite.rb

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,28 @@
33
class Sprite
44
CONFIG_PATH = RAILS_ROOT + '/config/'
55
DEFAULT_IMAGE_PATH = RAILS_ROOT + '/public/images/'
6-
CSS_OUTPUT = RAILS_ROOT + '/tmp/css_sprite.css'
6+
DEFAULT_FILE_PATH = RAILS_ROOT + '/tmp/sprite.css'
77

88
def initialize
99
@image_path = DEFAULT_IMAGE_PATH
10-
@config_files = Dir.glob("#{CONFIG_PATH}/css_sprite*.yml")
10+
@config_files = Dir.glob("#{CONFIG_PATH}/sprite.yml")
1111
end
1212

1313
def build
1414
@config_files.each do |config_file|
1515
@output = {}
16+
17+
# build up settings
1618
sprite_config = File.open(config_file) {|f| YAML::load(f)}
17-
@image_path = (sprite_config['config']['base_directory'])?RAILS_ROOT+"/"+sprite_config['config']['base_directory']+"/":DEFAULT_IMAGE_PATH
18-
@css_output = (sprite_config['config']['css_output'])?RAILS_ROOT+"/"+sprite_config['config']['css_output']:CSS_OUTPUT
19+
@image_path = sprite_config['config']['base_image_path'] ? RAILS_ROOT+"/"+sprite_config['config']['base_image_path']+"/" : DEFAULT_IMAGE_PATH
20+
@file_path = sprite_config['config']['output_file'] ? RAILS_ROOT+"/"+sprite_config['config']['output_file'] : DEFAULT_FILE_PATH
21+
22+
# create images
1923
sprite_config['images'].each do |configuration|
2024
output_image(configuration)
2125
end
26+
27+
# write css
2228
output_css(sprite_config)
2329
end
2430
end
@@ -48,8 +54,8 @@ def output_image(configuration)
4854
dest_image.write(@image_path + dest)
4955
end
5056

51-
def output_css(configuration)
52-
File.open(@css_output, 'w') do |f|
57+
def output_file(configuration)
58+
File.open(@file_path, 'w') do |f|
5359
@output.each do |dest, results|
5460
results.each do |result|
5561
f.puts ".#{result[:name]}"

rails/init.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
require 'sprite'
2+

sprite.gemspec

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,44 @@
11
# Generated by jeweler
2-
# DO NOT EDIT THIS FILE
3-
# Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
2+
# DO NOT EDIT THIS FILE DIRECTLY
3+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
44
# -*- encoding: utf-8 -*-
55

66
Gem::Specification.new do |s|
77
s.name = %q{sprite}
8-
s.version = "1.0.1"
8+
s.version = "0.1.0"
99

1010
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11-
s.authors = ["Richard Huang"]
12-
s.date = %q{2009-10-16}
13-
s.description = %q{sprite is a rails plugin/gem to generate css sprite image automatically.}
14-
s.email = %q{flyerhzm@gmail.com}
11+
s.authors = ["Jacques Crocker", "Richard Huang"]
12+
s.date = %q{2009-11-13}
13+
s.description = %q{sprite is a rails/merb plugin that generates sprites for css, sass}
14+
s.email = %q{merbjedi@gmail.com}
1515
s.extra_rdoc_files = [
16-
"README.textile"
16+
"README.md"
1717
]
1818
s.files = [
1919
"MIT-LICENSE",
20-
"README.textile",
20+
"README.md",
2121
"Rakefile",
2222
"VERSION",
23-
"css_sprite.gemspec",
24-
"init.rb",
25-
"install.rb",
26-
"lib/css_sprite.rb",
27-
"lib/css_sprite/sprite.rb",
28-
"spec/css_sprite_spec.rb",
23+
"config/css_sprite_example_config.yml",
24+
"lib/sprite.rb",
25+
"lib/sprite/sprite.rb",
2926
"spec/resources/good_topic.gif",
3027
"spec/resources/mid_topic.gif",
3128
"spec/spec.opts",
3229
"spec/spec_helper.rb",
33-
"tasks/css_sprite_tasks.rake",
34-
"uninstall.rb"
30+
"spec/sprite_spec.rb",
31+
"sprite.gemspec",
32+
"tasks/sprite_tasks.rake"
3533
]
36-
s.homepage = %q{}
34+
s.homepage = %q{http://github.com/merbjedi/sprite}
3735
s.rdoc_options = ["--charset=UTF-8"]
3836
s.require_paths = ["lib"]
3937
s.rubygems_version = %q{1.3.5}
40-
s.summary = %q{css_sprite is a rails plugin/gem to generate css sprite image automatically.}
38+
s.summary = %q{generate your css sprites automagically}
4139
s.test_files = [
42-
"spec/css_sprite_spec.rb",
43-
"spec/spec_helper.rb"
40+
"spec/spec_helper.rb",
41+
"spec/sprite_spec.rb"
4442
]
4543

4644
if s.respond_to? :specification_version then
@@ -53,3 +51,4 @@ Gem::Specification.new do |s|
5351
else
5452
end
5553
end
54+

tasks/sprite_tasks.rake

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
21
namespace :sprite do
3-
desc "buid css sprite image"
2+
desc "build sprite images based on config/sprite.yml"
43
task :build do
54
require File.join(File.dirname(__FILE__), '../lib/sprite/sprite.rb')
65
Sprite.new.build

0 commit comments

Comments
 (0)