svg_sprite 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +59 -2
- data/lib/svg_sprite.rb +4 -8
- data/lib/svg_sprite/cli.rb +13 -4
- data/lib/svg_sprite/svg.rb +36 -7
- data/lib/svg_sprite/template.rb +6 -0
- data/lib/svg_sprite/template/css.erb +1 -1
- data/lib/svg_sprite/template/scss.erb +1 -1
- data/lib/svg_sprite/version.rb +1 -1
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ece2edc0ee57a16a3be26fbee5c14a7dfec7543f
|
4
|
+
data.tar.gz: e8768667775ac396d65847de4501fb2ff9521b8b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 666f6801f1b8af0020631934647a1d509fc53a4a4d5875076569229d18878f0f735ff3d676fa98b544ae08124d4751ee5eeceba256fd133b2c1ae56f37cbd0f5
|
7
|
+
data.tar.gz: 14922c07b23583d3e1c5306fdd217002498ffa4c41f9493c78d37cac4d7c2194a8632557c05791be3b65980ef018a764251b2cbe91a0b13876b8d95054e4ddd0
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# SvgSprite
|
2
2
|
|
3
|
-
Create SVG sprites by embedding images into CSS using data URIs.
|
3
|
+
Create SVG sprites by embedding images into CSS using data URIs. The SVGs are optimized using [svg_optimizer](https://github.com/fnando/svg_optimizer).
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
@@ -63,6 +63,63 @@ Let's say the `images/icons` directory has a file called `user.svg`. The command
|
|
63
63
|
.icons-user-after
|
64
64
|
```
|
65
65
|
|
66
|
+
### Programming API
|
67
|
+
|
68
|
+
To generate the sprite without saving the file:
|
69
|
+
|
70
|
+
```ruby
|
71
|
+
require "svg_sprite"
|
72
|
+
rendered_css = SvgSprite.create({
|
73
|
+
source: "./images/icons",
|
74
|
+
format: "scss",
|
75
|
+
name: "icons"
|
76
|
+
}).render
|
77
|
+
```
|
78
|
+
|
79
|
+
To save the sprite content to a file:
|
80
|
+
|
81
|
+
```ruby
|
82
|
+
require "svg_sprite"
|
83
|
+
rendered_css = SvgSprite.export({
|
84
|
+
source: "./images/icons",
|
85
|
+
output: "./styles/_icons.scss",
|
86
|
+
format: "scss",
|
87
|
+
name: "icons"
|
88
|
+
})
|
89
|
+
```
|
90
|
+
|
91
|
+
#### Adding new formats
|
92
|
+
|
93
|
+
First, register your template renderer. The assigned object should respond to `call(source, options)`.
|
94
|
+
|
95
|
+
```ruby
|
96
|
+
# `source` is SvgSprite::Source, which wraps all SVG files.
|
97
|
+
# Each item returned by `source` is a SvgSprite::SVG instance.
|
98
|
+
SvgSprite::TEMPLATES["custom"] = proc do |source, options|
|
99
|
+
content = source.to_a.map(&:name).join("\n")
|
100
|
+
"/*\nImage names:\n#{content}\n*/"
|
101
|
+
end
|
102
|
+
```
|
103
|
+
|
104
|
+
Then you can generate the sprite like the following:
|
105
|
+
|
106
|
+
```ruby
|
107
|
+
rendered_css = SvgSprite.create({
|
108
|
+
source: "./examples",
|
109
|
+
format: "custom",
|
110
|
+
name: "icons"
|
111
|
+
}).render
|
112
|
+
#=> /*
|
113
|
+
#=> Image names:
|
114
|
+
#=> blue-square
|
115
|
+
#=> orange-square
|
116
|
+
#=> green-square
|
117
|
+
#=> yellow-square
|
118
|
+
#=> */
|
119
|
+
```
|
120
|
+
|
121
|
+
See what's available in [SvgSprite::SVG](https://github.com/fnando/svg_sprite/blob/master/lib/svg_sprite/svg.rb) class.
|
122
|
+
|
66
123
|
## Development
|
67
124
|
|
68
125
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
@@ -71,7 +128,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
71
128
|
|
72
129
|
## Contributing
|
73
130
|
|
74
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
131
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/fnando/svg_sprite. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
75
132
|
|
76
133
|
|
77
134
|
## License
|
data/lib/svg_sprite.rb
CHANGED
@@ -1,22 +1,18 @@
|
|
1
1
|
module SvgSprite
|
2
|
-
require "erb"
|
3
2
|
require "base64"
|
3
|
+
require "cgi"
|
4
|
+
require "erb"
|
4
5
|
require "svg_optimizer"
|
5
6
|
require "thor"
|
6
7
|
|
7
|
-
OUTPUT_FORMATS = %w[scss css].freeze
|
8
|
-
require "svg_sprite/cli"
|
9
8
|
require "svg_sprite/version"
|
10
9
|
require "svg_sprite/template/scss"
|
11
10
|
require "svg_sprite/template/css"
|
11
|
+
require "svg_sprite/template"
|
12
12
|
require "svg_sprite/sprite"
|
13
13
|
require "svg_sprite/source"
|
14
14
|
require "svg_sprite/svg"
|
15
|
-
|
16
|
-
TEMPLATES = {
|
17
|
-
"scss" => Template::SCSS.new,
|
18
|
-
"css" => Template::CSS.new
|
19
|
-
}
|
15
|
+
require "svg_sprite/cli"
|
20
16
|
|
21
17
|
def self.create(options)
|
22
18
|
Sprite.new(
|
data/lib/svg_sprite/cli.rb
CHANGED
@@ -6,6 +6,10 @@ module SvgSprite
|
|
6
6
|
true
|
7
7
|
end
|
8
8
|
|
9
|
+
def self.formats
|
10
|
+
TEMPLATES.keys
|
11
|
+
end
|
12
|
+
|
9
13
|
desc "version", "Display svg_sprite version"
|
10
14
|
map %w[-v --version] => :version
|
11
15
|
|
@@ -15,11 +19,12 @@ module SvgSprite
|
|
15
19
|
|
16
20
|
desc "generate", "Generate the SVG sprite by embedding the images as data URIs."
|
17
21
|
option :source, aliases: %w[-s], required: true, desc: "The source directory. Will match SOURCE/**/*.svg."
|
18
|
-
option :output, aliases: %w[-o], required: true, desc: "The output file.
|
22
|
+
option :output, aliases: %w[-o], required: true, desc: "The output file."
|
19
23
|
option :name, aliases: %w[-n], default: "sprite", desc: "The sprite name. This will be used as the variable for dynamic files."
|
24
|
+
option :format, aliases: %w[-f], desc: "The output format. When not provided, will be inferred from output file's extension. Can be any of #{formats.join("|")}."
|
20
25
|
def generate
|
21
|
-
format = File.extname(options["output"])[1..-1]
|
22
|
-
fail Error, "ERROR: invalid output
|
26
|
+
format = options["format"] || File.extname(options["output"])[1..-1]
|
27
|
+
fail Error, "ERROR: invalid output format. Must be one of #{formats.join("|")}." unless valid_format?(format)
|
23
28
|
|
24
29
|
SvgSprite.export(
|
25
30
|
source: File.expand_path(options["source"]),
|
@@ -32,7 +37,11 @@ module SvgSprite
|
|
32
37
|
private
|
33
38
|
|
34
39
|
def valid_format?(format)
|
35
|
-
|
40
|
+
formats.include?(format)
|
41
|
+
end
|
42
|
+
|
43
|
+
def formats
|
44
|
+
self.class.formats
|
36
45
|
end
|
37
46
|
end
|
38
47
|
end
|
data/lib/svg_sprite/svg.rb
CHANGED
@@ -6,40 +6,69 @@ module SvgSprite
|
|
6
6
|
@path = path
|
7
7
|
end
|
8
8
|
|
9
|
+
# Return the image file name without the extension.
|
10
|
+
def name
|
11
|
+
File.basename(path, ".*")
|
12
|
+
end
|
13
|
+
|
14
|
+
# Detect if SVG has all dimensions defined.
|
9
15
|
def has_dimensions?
|
10
16
|
width && height
|
11
17
|
end
|
12
18
|
|
19
|
+
# Return a Nokogiri representation of the <svg> element.
|
13
20
|
def svg
|
14
|
-
@svg ||=
|
21
|
+
@svg ||= Nokogiri::XML(contents).css("svg").first
|
15
22
|
end
|
16
23
|
|
24
|
+
# Return the <svg>'s width.
|
17
25
|
def width
|
18
26
|
svg["width"]
|
19
27
|
end
|
20
28
|
|
29
|
+
# Return the <svg>'s height.
|
21
30
|
def height
|
22
31
|
svg["height"]
|
23
32
|
end
|
24
33
|
|
25
|
-
|
26
|
-
@xml ||= Nokogiri::XML(contents)
|
27
|
-
end
|
28
|
-
|
34
|
+
# Return the raw content. This content is not optimized by svg_optimizer.
|
29
35
|
def contents
|
30
36
|
@contents ||= File.read(path)
|
31
37
|
end
|
32
38
|
|
39
|
+
# Return the optimized content.
|
33
40
|
def optimized
|
34
41
|
@optimized ||= SvgOptimizer.optimize(contents)
|
35
42
|
end
|
36
43
|
|
44
|
+
# Return the URL-encoded version of the content.
|
45
|
+
def encoded
|
46
|
+
CGI.escape(contents)
|
47
|
+
end
|
48
|
+
|
49
|
+
# Return the Base64-encoded version of the content.
|
37
50
|
def base64
|
38
51
|
Base64.strict_encode64(optimized)
|
39
52
|
end
|
40
53
|
|
41
|
-
|
42
|
-
|
54
|
+
# Return the smaller data URI.
|
55
|
+
def data_uri
|
56
|
+
[base64_data_uri, urlencoded_data_uri].sort_by(&:bytesize).first
|
57
|
+
end
|
58
|
+
|
59
|
+
# Return the Base64 version of the data URI.
|
60
|
+
def base64_data_uri
|
61
|
+
%[data:image/svg+xml;base64,#{base64}]
|
62
|
+
end
|
63
|
+
|
64
|
+
# Return the URL-encoded version of the data URI.
|
65
|
+
def urlencoded_data_uri
|
66
|
+
%[data:image/svg+xml;charset=#{encoding},#{encoded}]
|
67
|
+
end
|
68
|
+
|
69
|
+
# The output encoding based on the global configuration.
|
70
|
+
def encoding
|
71
|
+
Encoding.default_external.name
|
43
72
|
end
|
44
73
|
end
|
45
74
|
end
|
@@ -3,7 +3,7 @@
|
|
3
3
|
.<%= options[:name] %>-<%= svg.name %>,
|
4
4
|
.<%= options[:name] %>-<%= svg.name %>-before:before,
|
5
5
|
.<%= options[:name] %>-<%= svg.name %>-after:after {
|
6
|
-
background-image: url(
|
6
|
+
background-image: url(<%= svg.data_uri %>);
|
7
7
|
<%- if svg.has_dimensions? -%>
|
8
8
|
width: <%= svg.width %>;
|
9
9
|
height: <%= svg.height %>;
|
@@ -15,7 +15,7 @@ $<%= options[:name] %>-names: (<%= names(source) %>);
|
|
15
15
|
|
16
16
|
<% for svg in source %>
|
17
17
|
%<%= options[:name] %>-<%= svg.name %> {
|
18
|
-
background-image: url(
|
18
|
+
background-image: url(<%= svg.data_uri %>);
|
19
19
|
<%- if svg.has_dimensions? -%>
|
20
20
|
width: <%= svg.width %>;
|
21
21
|
height: <%= svg.height %>;
|
data/lib/svg_sprite/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: svg_sprite
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nando Vieira
|
@@ -152,6 +152,7 @@ files:
|
|
152
152
|
- lib/svg_sprite/source.rb
|
153
153
|
- lib/svg_sprite/sprite.rb
|
154
154
|
- lib/svg_sprite/svg.rb
|
155
|
+
- lib/svg_sprite/template.rb
|
155
156
|
- lib/svg_sprite/template/css.erb
|
156
157
|
- lib/svg_sprite/template/css.rb
|
157
158
|
- lib/svg_sprite/template/scss.erb
|