sprite-generator 0.1.7 → 0.1.8
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +17 -7
- data/Rakefile +6 -6
- data/VERSION +1 -1
- data/lib/sprite/builder.rb +75 -49
- data/lib/sprite/runner.rb +37 -12
- data/lib/sprite/styles/css_generator.rb +10 -6
- data/sprite-generator.gemspec +3 -3
- metadata +3 -3
data/README.md
CHANGED
@@ -72,6 +72,8 @@ Configuration of `sprite-generator` is done via `config/sprite.yml`. It allows y
|
|
72
72
|
- `default_format:` defines the default file image format of the generated files. (defaults to `png`)
|
73
73
|
- `default_spacing:` defines the default pixel spacing between sprites (defaults to 0)
|
74
74
|
- `class_separator:` used to generated the class name by separating the image name and sprite name (defaults to `-`)
|
75
|
+
- `pseudo_classes`: array of strings. Example: if some image name will end with `_hover` and `pseudo_classes` will equal `['hover']`, then the style for that picture will end with `:hover` instead of `_hover`. (defaults to ['hover'])
|
76
|
+
- `parameters`: hash of parameters that will be substituted in other options. In order to be used parameter must be declared in parameters hash and have a default value. Parameter can be overriden in command line: `sprite -p param_name=new_param_value`. (defaults to `{}`)
|
75
77
|
|
76
78
|
* `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:
|
77
79
|
- `name:` name of image (required)
|
@@ -88,14 +90,16 @@ All image and style paths should be set relative to the public folder (which is
|
|
88
90
|
|
89
91
|
config:
|
90
92
|
style: css
|
91
|
-
style_output_path: stylesheets/sprites
|
92
|
-
image_output_path: images/sprites/
|
93
|
-
image_source_path: images/
|
93
|
+
style_output_path: ${theme}/stylesheets/sprites
|
94
|
+
image_output_path: ${theme}/images/sprites/
|
95
|
+
image_source_path: ${theme}/images/
|
94
96
|
public_path: public/
|
95
97
|
sprites_class: 'sprites'
|
96
98
|
class_separator: '-'
|
97
99
|
default_format: png
|
98
100
|
default_spacing: 50
|
101
|
+
parameters:
|
102
|
+
theme: mytheme
|
99
103
|
|
100
104
|
# defines what sprite collections get created
|
101
105
|
images:
|
@@ -110,6 +114,7 @@ All image and style paths should be set relative to the public folder (which is
|
|
110
114
|
- icons/blue-stars/medium.png
|
111
115
|
- icons/blue-stars/large.png
|
112
116
|
- icons/blue-stars/xlarge.png
|
117
|
+
- icons/blue-stars/large_hover.png
|
113
118
|
|
114
119
|
# creates a public/images/sprites/green-stars.jpg image with
|
115
120
|
# all the gif files contained within /images/icons/green-stars/
|
@@ -125,22 +130,27 @@ All image and style paths should be set relative to the public folder (which is
|
|
125
130
|
By default, it will use with `style: css` and generate the file at `public/stylesheets/sprites.css`
|
126
131
|
|
127
132
|
.sprites.blue-stars-small {
|
128
|
-
background: url('/images/icons/blue-stars/small.png') no-repeat 0px 0px;
|
133
|
+
background: url('/mytheme/images/icons/blue-stars/small.png') no-repeat 0px 0px;
|
129
134
|
width: 12px;
|
130
135
|
height: 6px;
|
131
136
|
}
|
132
137
|
.sprites.blue-stars-medium {
|
133
|
-
background: url('/images/icons/blue-stars/medium.png') no-repeat 0px 6px;
|
138
|
+
background: url('/mytheme/images/icons/blue-stars/medium.png') no-repeat 0px 6px;
|
134
139
|
width: 30px;
|
135
140
|
height: 15px;
|
136
141
|
}
|
137
142
|
.sprites.blue-stars-large {
|
138
|
-
background: url('/images/icons/blue-stars/large.png') no-repeat 0px 21px;
|
143
|
+
background: url('/mytheme/images/icons/blue-stars/large.png') no-repeat 0px 21px;
|
144
|
+
width: 60px;
|
145
|
+
height: 30px;
|
146
|
+
}
|
147
|
+
.sprites.blue-stars-large:hover {
|
148
|
+
background: url('/mytheme/images/icons/blue-stars/large_hover.png') no-repeat 0px 21px;
|
139
149
|
width: 60px;
|
140
150
|
height: 30px;
|
141
151
|
}
|
142
152
|
.sprites.blue-stars-xlarge {
|
143
|
-
background: url('/images/icons/blue-stars/xlarge.png') no-repeat 0px 96px;
|
153
|
+
background: url('/mytheme/images/icons/blue-stars/xlarge.png') no-repeat 0px 96px;
|
144
154
|
width: 100px;
|
145
155
|
height: 75px;
|
146
156
|
}
|
data/Rakefile
CHANGED
@@ -11,14 +11,14 @@ Spec::Rake::SpecTask.new(:spec) do |t|
|
|
11
11
|
end
|
12
12
|
|
13
13
|
Jeweler::Tasks.new do |gemspec|
|
14
|
-
gemspec.name = "sprite"
|
14
|
+
gemspec.name = "sprite-generator"
|
15
15
|
gemspec.summary = "generate your css sprites automagically"
|
16
|
-
gemspec.description = "sprite is a rails/merb plugin that generates sprites for css, sass"
|
17
|
-
gemspec.email = "
|
18
|
-
gemspec.homepage = "http://github.com/
|
19
|
-
gemspec.authors = ["Jacques Crocker"]
|
16
|
+
gemspec.description = "sprite-generator is a rails/merb plugin that generates sprites for css, sass"
|
17
|
+
gemspec.email = "astyagun@gmail.com"
|
18
|
+
gemspec.homepage = "http://github.com/iast/sprite-generator"
|
19
|
+
gemspec.authors = ["Jacques Crocker", "Anton Styagun"]
|
20
20
|
gemspec.files.exclude '.gitignore'
|
21
|
-
|
21
|
+
|
22
22
|
# removing test files and specs from the gem to save space
|
23
23
|
gemspec.files -= Dir.glob("spec/**/*")
|
24
24
|
gemspec.test_files = []
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.8
|
data/lib/sprite/builder.rb
CHANGED
@@ -1,17 +1,19 @@
|
|
1
1
|
require 'fileutils'
|
2
|
+
|
2
3
|
module Sprite
|
3
|
-
class Builder
|
4
|
+
class Builder
|
4
5
|
DEFAULT_CONFIG_PATH = 'config/sprite.yml'
|
5
|
-
|
6
|
+
|
6
7
|
attr_reader :config
|
7
8
|
attr_reader :images
|
8
|
-
|
9
|
-
def self.from_config(
|
9
|
+
|
10
|
+
def self.from_config(parameters)
|
10
11
|
results = {}
|
11
|
-
config_path = File.join(Sprite.root, path || DEFAULT_CONFIG_PATH)
|
12
|
-
|
12
|
+
config_path = File.join(Sprite.root, parameters[:path] || DEFAULT_CONFIG_PATH)
|
13
|
+
parameters[:path] &= nil
|
14
|
+
|
13
15
|
# read configuration
|
14
|
-
if File.exists?(config_path)
|
16
|
+
if File.exists?(config_path)
|
15
17
|
begin
|
16
18
|
results = File.open(config_path) {|f| YAML::load(f)} || {}
|
17
19
|
rescue => e
|
@@ -20,105 +22,111 @@ module Sprite
|
|
20
22
|
end
|
21
23
|
end
|
22
24
|
|
23
|
-
new(results["config"], results["images"])
|
25
|
+
new(results["config"], results["images"], parameters)
|
24
26
|
end
|
25
27
|
|
26
|
-
def initialize(config = nil, images = nil)
|
28
|
+
def initialize(config = nil, images = nil, parameters = {})
|
29
|
+
@parameters = parameters
|
30
|
+
|
27
31
|
@config = config || {}
|
28
32
|
set_config_defaults
|
29
|
-
|
33
|
+
|
30
34
|
@images = images || []
|
31
35
|
set_image_defaults
|
36
|
+
expand_parameters
|
32
37
|
expand_image_paths
|
33
|
-
|
38
|
+
|
34
39
|
# initialize sprite files
|
35
40
|
@sprite_files = {}
|
41
|
+
|
36
42
|
end
|
37
|
-
|
38
|
-
def build
|
43
|
+
|
44
|
+
def build
|
39
45
|
@sprite_files = {}
|
40
|
-
|
46
|
+
|
41
47
|
if images.size > 0
|
42
48
|
# create images
|
43
49
|
images.each do |image|
|
44
50
|
write_image(image)
|
45
51
|
end
|
46
|
-
|
52
|
+
|
47
53
|
if @sprite_files.values.length > 0
|
48
54
|
# write css
|
49
55
|
write_styles
|
50
56
|
end
|
51
57
|
end
|
52
58
|
end
|
53
|
-
|
59
|
+
|
54
60
|
protected
|
55
61
|
def write_image(image)
|
56
62
|
results = []
|
57
63
|
sources = image['sources'].to_a
|
58
64
|
return unless sources.length > 0
|
59
|
-
|
65
|
+
|
60
66
|
name = image['name']
|
61
67
|
format = image['format'] || config["default_format"]
|
62
68
|
spaced_by = image['spaced_by'] || config["default_spacing"] || 0
|
63
|
-
|
69
|
+
align = image['align'] || config['default_align']
|
70
|
+
|
64
71
|
combiner = ImageCombiner.new
|
65
|
-
|
72
|
+
|
66
73
|
dest_image = combiner.get_image(sources.shift)
|
67
74
|
results << combiner.image_properties(dest_image).merge(:x => 0, :y => 0, :group => name)
|
68
75
|
sources.each do |source|
|
69
76
|
source_image = combiner.get_image(source)
|
70
|
-
if
|
77
|
+
if align == 'horizontal'
|
71
78
|
x = dest_image.columns + spaced_by
|
72
79
|
y = 0
|
73
|
-
align = "horizontal"
|
74
80
|
else
|
75
81
|
x = 0
|
76
82
|
y = dest_image.rows + spaced_by
|
77
|
-
align = "vertical"
|
78
83
|
end
|
79
84
|
results << combiner.image_properties(source_image).merge(:x => -x, :y => -y, :group => name, :align => align)
|
80
85
|
dest_image = combiner.composite_images(dest_image, source_image, x, y)
|
81
86
|
end
|
82
|
-
|
87
|
+
|
83
88
|
# set up path
|
84
89
|
path = image_output_path(name, format)
|
85
90
|
FileUtils.mkdir_p(File.dirname(path))
|
86
|
-
|
91
|
+
|
87
92
|
# write sprite image file to disk
|
88
93
|
dest_image.write(path)
|
89
94
|
@sprite_files["#{name}.#{format}"] = results
|
90
95
|
end
|
91
|
-
|
96
|
+
|
92
97
|
def write_styles
|
93
98
|
style = Styles.get(config["style"]).new(self)
|
94
|
-
|
99
|
+
|
95
100
|
# use the absolute style output path to make sure we have the directory set up
|
96
101
|
path = style_output_path(style.extension, false)
|
97
102
|
FileUtils.mkdir_p(File.dirname(path))
|
98
|
-
|
103
|
+
|
99
104
|
# send the style the relative path
|
100
105
|
style.write(style_output_path(style.extension, true), @sprite_files)
|
101
106
|
end
|
102
|
-
|
107
|
+
|
103
108
|
# sets all the default values on the config
|
104
109
|
def set_config_defaults
|
105
|
-
@config['style']
|
106
|
-
@config['style_output_path']
|
107
|
-
@config['image_output_path']
|
108
|
-
@config['image_source_path']
|
109
|
-
@config['public_path']
|
110
|
-
@config['default_format']
|
111
|
-
@config['class_separator']
|
112
|
-
@config[
|
113
|
-
@config[
|
110
|
+
@config['style'] ||= 'css'
|
111
|
+
@config['style_output_path'] ||= 'stylesheets/sprites'
|
112
|
+
@config['image_output_path'] ||= 'images/sprites/'
|
113
|
+
@config['image_source_path'] ||= 'images/'
|
114
|
+
@config['public_path'] ||= 'public/'
|
115
|
+
@config['default_format'] ||= 'png'
|
116
|
+
@config['class_separator'] ||= '-'
|
117
|
+
@config['sprites_class'] ||= 'sprites'
|
118
|
+
@config['default_spacing'] ||= 0
|
119
|
+
@config['pseudo_classes'] ||= ['hover']
|
120
|
+
@config['parameters'] ||= {}
|
121
|
+
@config['default_orientation'] ||= 'horizontal'
|
114
122
|
end
|
115
|
-
|
123
|
+
|
116
124
|
# if no image configs are detected, set some intelligent defaults
|
117
125
|
def set_image_defaults
|
118
126
|
return unless @images.size == 0
|
119
|
-
|
127
|
+
|
120
128
|
sprites_path = image_source_path("sprites")
|
121
|
-
|
129
|
+
|
122
130
|
if File.exists?(sprites_path)
|
123
131
|
Dir.glob(File.join(sprites_path, "*")) do |dir|
|
124
132
|
next unless File.directory?(dir)
|
@@ -137,7 +145,7 @@ module Sprite
|
|
137
145
|
end
|
138
146
|
end
|
139
147
|
end
|
140
|
-
|
148
|
+
|
141
149
|
# expands out sources, taking the Glob paths and turning them into separate entries in the array
|
142
150
|
def expand_image_paths
|
143
151
|
# cycle through image sources and expand out globs
|
@@ -148,7 +156,25 @@ module Sprite
|
|
148
156
|
}.flatten.compact
|
149
157
|
end
|
150
158
|
end
|
151
|
-
|
159
|
+
|
160
|
+
def expand_parameters
|
161
|
+
@config.each do |k, v|
|
162
|
+
next if k == "parameters" or not v.is_a? String
|
163
|
+
if @config['parameters']
|
164
|
+
@config['parameters'].each do |parameter_name, parameter_default_value|
|
165
|
+
parameter_value = @parameters[parameter_name.to_sym] || parameter_default_value
|
166
|
+
puts "Warning! Parameter #{parameter_name.to_sym} has no value!" unless parameter_value
|
167
|
+
v.gsub! /\$\{#{parameter_name.to_s}\}/, parameter_value.to_s
|
168
|
+
end
|
169
|
+
else
|
170
|
+
parameters_used = v.match(/\$\{([\w\s_]+)\}/)
|
171
|
+
parameters_used.each do |param|
|
172
|
+
puts "Warning! Parameter #{param.to_sym} has no definition, but is used in config file!"
|
173
|
+
end if parameters_used
|
174
|
+
end
|
175
|
+
end
|
176
|
+
end
|
177
|
+
|
152
178
|
# get the disk path for the style output file
|
153
179
|
def style_output_path(file_ext, relative = false)
|
154
180
|
path = config['style_output_path']
|
@@ -157,7 +183,7 @@ module Sprite
|
|
157
183
|
end
|
158
184
|
public_path(path, relative)
|
159
185
|
end
|
160
|
-
|
186
|
+
|
161
187
|
# get the disk path for a location within the image output folder
|
162
188
|
def image_output_path(name, format, relative = false)
|
163
189
|
path_parts = []
|
@@ -165,7 +191,7 @@ module Sprite
|
|
165
191
|
path_parts << "#{name}.#{format}"
|
166
192
|
public_path(File.join(*path_parts), relative)
|
167
193
|
end
|
168
|
-
|
194
|
+
|
169
195
|
# get the disk path for an image source file
|
170
196
|
def image_source_path(location, relative = false)
|
171
197
|
path_parts = []
|
@@ -173,26 +199,26 @@ module Sprite
|
|
173
199
|
path_parts << location
|
174
200
|
public_path(File.join(*path_parts), relative)
|
175
201
|
end
|
176
|
-
|
202
|
+
|
177
203
|
# get the disk path for a location within the public folder (if set)
|
178
204
|
def public_path(location, relative = false)
|
179
205
|
path_parts = []
|
180
206
|
path_parts << Sprite.root unless relative
|
181
207
|
path_parts << chop_trailing_slash(config['public_path']) if path_present?(config['public_path'])
|
182
208
|
path_parts << location
|
183
|
-
|
209
|
+
|
184
210
|
File.join(*path_parts)
|
185
211
|
end
|
186
|
-
|
212
|
+
|
187
213
|
# chop off the trailing slash on a directory path (if it exists)
|
188
214
|
def chop_trailing_slash(path)
|
189
215
|
path = path[0...-1] if path[-1] == File::SEPARATOR
|
190
216
|
path
|
191
217
|
end
|
192
|
-
|
218
|
+
|
193
219
|
# check if the path is set
|
194
220
|
def path_present?(path)
|
195
221
|
path.to_s.strip != ""
|
196
222
|
end
|
197
223
|
end
|
198
|
-
end
|
224
|
+
end
|
data/lib/sprite/runner.rb
CHANGED
@@ -1,22 +1,47 @@
|
|
1
|
+
require 'getoptlong'
|
2
|
+
|
1
3
|
module Sprite
|
2
4
|
class Runner
|
3
|
-
|
4
|
-
attr_accessor :options
|
5
|
+
|
5
6
|
def initialize(args)
|
6
|
-
|
7
|
+
@parameters = {}
|
8
|
+
set_options(args)
|
7
9
|
end
|
8
|
-
|
10
|
+
|
9
11
|
def set_options(args)
|
10
|
-
opts =
|
11
|
-
|
12
|
-
|
13
|
-
|
12
|
+
opts = GetoptLong.new(
|
13
|
+
['--help', '-h', GetoptLong::NO_ARGUMENT],
|
14
|
+
['--param', '-p', GetoptLong::REQUIRED_ARGUMENT],
|
15
|
+
['--path', GetoptLong::REQUIRED_ARGUMENT]
|
16
|
+
)
|
17
|
+
opts.each do |option, argument|
|
18
|
+
case option
|
19
|
+
when '--help'
|
20
|
+
puts
|
21
|
+
puts " sprite [--path /path/to/config/file] [(--param | -p) param_name=param_value] ..."
|
22
|
+
puts
|
23
|
+
puts " Notes:"
|
24
|
+
puts " * `sprite --path /path/to/config/file` is equal to `sprite -p path=/path/to/config/file`"
|
25
|
+
puts " * param_name can contain only letters, numbers and underscores"
|
26
|
+
puts
|
27
|
+
exit 0
|
28
|
+
when '--param'
|
29
|
+
parsed_argument = argument.match(/([^=]+)=(.+)/)
|
30
|
+
unless parsed_argument[0] =~ /[\w\d_]/
|
31
|
+
puts "Error: parameter name can contain only letters, digits and underscores."
|
32
|
+
exit 1
|
33
|
+
end
|
34
|
+
@parameters[parsed_argument[1].to_sym] = parsed_argument[2]
|
35
|
+
when '--path'
|
36
|
+
@parameters[:path] = argument if argument
|
37
|
+
end
|
38
|
+
end
|
14
39
|
end
|
15
|
-
|
40
|
+
|
16
41
|
# run sprite creator
|
17
42
|
def run!
|
18
43
|
begin
|
19
|
-
Sprite::Builder.from_config(
|
44
|
+
Sprite::Builder.from_config(@parameters).build
|
20
45
|
# rescue Exception => e
|
21
46
|
# # catch errors
|
22
47
|
# puts "ERROR"
|
@@ -25,6 +50,6 @@ module Sprite
|
|
25
50
|
end
|
26
51
|
0
|
27
52
|
end
|
28
|
-
|
53
|
+
|
29
54
|
end
|
30
|
-
end
|
55
|
+
end
|
@@ -5,29 +5,33 @@ module Sprite
|
|
5
5
|
def initialize(builder)
|
6
6
|
@builder = builder
|
7
7
|
end
|
8
|
-
|
8
|
+
|
9
9
|
def write(path, sprite_files)
|
10
10
|
# set up class_name to append to each rule
|
11
11
|
sprites_class = @builder.config['sprites_class'] ? ".#{@builder.config['sprites_class']}" : ""
|
12
|
-
|
12
|
+
|
13
13
|
# write styles to disk
|
14
14
|
File.open(File.join(Sprite.root, path), 'w') do |f|
|
15
15
|
# write stylesheet file to disk
|
16
16
|
sprite_files.each do |sprite_file, sprites|
|
17
17
|
sprites.each do |sprite|
|
18
|
-
|
18
|
+
# if sprite name ends with some of special states, consider this a state
|
19
|
+
# for example if the sprite name is 'image_hover' and 'hover' is among states, set sprite_name to image:hover
|
20
|
+
sprite_name = sprite[:name].gsub /(.*)(_)(#{@builder.config['pseudo_classes'].join "|"})$/, '\1:\3'
|
21
|
+
|
22
|
+
f.puts "#{sprites_class}.#{sprite[:group]}#{@builder.config['class_separator']}#{sprite_name} {"
|
19
23
|
f.puts " background: url('/#{@builder.config['image_output_path']}#{sprite_file}') no-repeat #{sprite[:x]}px #{sprite[:y]}px;"
|
20
24
|
f.puts " width: #{sprite[:width]}px;"
|
21
25
|
f.puts " height: #{sprite[:height]}px;"
|
22
26
|
f.puts "}"
|
23
27
|
end
|
24
28
|
end
|
25
|
-
end
|
29
|
+
end
|
26
30
|
end
|
27
|
-
|
31
|
+
|
28
32
|
def extension
|
29
33
|
"css"
|
30
34
|
end
|
31
35
|
end
|
32
36
|
end
|
33
|
-
end
|
37
|
+
end
|
data/sprite-generator.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{sprite-generator}
|
8
|
-
s.version = "0.1.
|
8
|
+
s.version = "0.1.8"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Jacques Crocker", "Anton Styagun"]
|
12
|
-
s.date = %q{2009-
|
12
|
+
s.date = %q{2009-12-01}
|
13
13
|
s.default_executable = %q{sprite}
|
14
14
|
s.description = %q{sprite-generator is a rails/merb plugin that generates sprites for css, sass}
|
15
15
|
s.email = %q{astyagun@gmail.com}
|
@@ -37,7 +37,7 @@ Gem::Specification.new do |s|
|
|
37
37
|
"sprite-generator.gemspec",
|
38
38
|
"tasks/sprite_tasks.rake"
|
39
39
|
]
|
40
|
-
s.homepage = %q{http://github.com/iast/sprite}
|
40
|
+
s.homepage = %q{http://github.com/iast/sprite-generator}
|
41
41
|
s.rdoc_options = ["--charset=UTF-8"]
|
42
42
|
s.require_paths = ["lib"]
|
43
43
|
s.rubygems_version = %q{1.3.5}
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sprite-generator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jacques Crocker
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2009-
|
13
|
+
date: 2009-12-01 00:00:00 +02:00
|
14
14
|
default_executable: sprite
|
15
15
|
dependencies: []
|
16
16
|
|
@@ -42,7 +42,7 @@ files:
|
|
42
42
|
- sprite-generator.gemspec
|
43
43
|
- tasks/sprite_tasks.rake
|
44
44
|
has_rdoc: true
|
45
|
-
homepage: http://github.com/iast/sprite
|
45
|
+
homepage: http://github.com/iast/sprite-generator
|
46
46
|
licenses: []
|
47
47
|
|
48
48
|
post_install_message:
|