sprockets-svg 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +67 -0
- data/Rakefile +1 -0
- data/lib/sprockets-svg.rb +2 -0
- data/lib/sprockets/svg.rb +23 -0
- data/lib/sprockets/svg/cleaner.rb +16 -0
- data/lib/sprockets/svg/png.rb +27 -0
- data/lib/sprockets/svg/railtie.rb +12 -0
- data/lib/sprockets/svg/server.rb +40 -0
- data/lib/sprockets/svg/version.rb +5 -0
- data/sprockets-svg.gemspec +26 -0
- metadata +127 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 637ef504757d5585190afec174fce8a7000d9dd1
|
4
|
+
data.tar.gz: 9b96ee0e1a7ce01abca2ca5c85c66e869a002ff0
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2b66f14048832b1da4a1478348aa10316b53952b8eaa12b127fec90a1f57d1e794ff081951d3e34ba633156078f7fe59354544310b0df65c7fb905a75e4c1670
|
7
|
+
data.tar.gz: 30c4176c3f48a590bf85f890f863765e4b7d4537cef6278c1350d71f6abfe49e0da7f5922dcc1652a52448a42b0eba7ef93738b8dcc611815f97202b6544157b
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Jean Boussier
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
# Sprockets::Svg
|
2
|
+
|
3
|
+
SVG toolchain for sprockets
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
|
8
|
+
And then execute:
|
9
|
+
|
10
|
+
$ bundle
|
11
|
+
|
12
|
+
Or install it yourself as:
|
13
|
+
|
14
|
+
$ gem install sprockets-svg
|
15
|
+
|
16
|
+
## Usage
|
17
|
+
|
18
|
+
### Ruby on Rails
|
19
|
+
|
20
|
+
Add this line to your application's Gemfile:
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
gem 'sprockets-svg'
|
24
|
+
```
|
25
|
+
|
26
|
+
From now on, svg files will be automatically optimised.
|
27
|
+
|
28
|
+
To link the png version of `picture.svg` from scss:
|
29
|
+
|
30
|
+
```scss
|
31
|
+
#id {
|
32
|
+
background-image: image-url(picture.svg.png)
|
33
|
+
}
|
34
|
+
```
|
35
|
+
|
36
|
+
or from a view:
|
37
|
+
|
38
|
+
```erb
|
39
|
+
<%= image_tag 'picture.svg.png' %>
|
40
|
+
```
|
41
|
+
|
42
|
+
### Standalone Sprockets
|
43
|
+
|
44
|
+
If you are using sprockets outside of Rails, to setup `sprockets-svg` you just need:
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
assets = Sprockets::Environment.new do |env|
|
48
|
+
# Your assets settings
|
49
|
+
end
|
50
|
+
|
51
|
+
require "sprockets-svg"
|
52
|
+
Sprockets::Svg.install(assets)
|
53
|
+
```
|
54
|
+
|
55
|
+
## Optimizations
|
56
|
+
|
57
|
+
For now the only optimization performed is to remove hidden elements.
|
58
|
+
|
59
|
+
In the future I'd like to rely on the [`svgo` toolchain](https://github.com/svg/svgo).
|
60
|
+
|
61
|
+
## Contributing
|
62
|
+
|
63
|
+
1. Fork it ( http://github.com/<my-github-username>/sprockets-svg/fork )
|
64
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
65
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
66
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
67
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'sprockets/svg/version'
|
2
|
+
|
3
|
+
module Sprockets
|
4
|
+
module Svg
|
5
|
+
extend self
|
6
|
+
|
7
|
+
def install(assets)
|
8
|
+
assets.register_preprocessor 'image/svg+xml', :svg_min do |context, data|
|
9
|
+
Sprockets::Svg::Cleaner.process(data)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
require_relative 'svg/cleaner'
|
17
|
+
require_relative 'svg/png'
|
18
|
+
require_relative 'svg/server'
|
19
|
+
|
20
|
+
Sprockets::Asset.send(:include, Sprockets::Svg::Png)
|
21
|
+
Sprockets::Environment.send(:include, Sprockets::Svg::Server)
|
22
|
+
|
23
|
+
require_relative 'svg/railtie' if defined?(Rails)
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'nokogiri'
|
2
|
+
|
3
|
+
module Sprockets
|
4
|
+
module Svg
|
5
|
+
class Cleaner
|
6
|
+
# TODO: integrate svgo instead: https://github.com/svg/svgo
|
7
|
+
# See https://github.com/lautis/uglifier on how to integrate a npm package as a gem.
|
8
|
+
def self.process(svg)
|
9
|
+
p :preprocess_svg
|
10
|
+
document = Nokogiri::XML(svg)
|
11
|
+
document.css('[display=none]').remove()
|
12
|
+
document.to_s
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'rmagick'
|
2
|
+
|
3
|
+
module Sprockets
|
4
|
+
module Svg
|
5
|
+
module Png
|
6
|
+
def self.included(base)
|
7
|
+
base.send(:alias_method, :write_to_without_png_convertion, :write_to)
|
8
|
+
base.send(:alias_method, :write_to, :write_to_with_png_convertion)
|
9
|
+
end
|
10
|
+
|
11
|
+
def write_to_with_png_convertion(path, options={})
|
12
|
+
write_to_without_png_convertion(path, options)
|
13
|
+
if path.ends_with?('.svg')
|
14
|
+
Png.convert(path, path + '.png')
|
15
|
+
end
|
16
|
+
nil
|
17
|
+
end
|
18
|
+
|
19
|
+
# TODO: integrate svgo instead: https://github.com/svg/svgo
|
20
|
+
# See https://github.com/lautis/uglifier on how to integrate a npm package as a gem.
|
21
|
+
def self.convert(svg_path, png_path)
|
22
|
+
image = Magick::ImageList.new(svg_path)
|
23
|
+
image.write(png_path)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'tempfile'
|
2
|
+
require 'pathname'
|
3
|
+
|
4
|
+
module Sprockets
|
5
|
+
module Svg
|
6
|
+
module Server
|
7
|
+
|
8
|
+
def self.included(base)
|
9
|
+
base.send(:alias_method, :find_asset_without_convertion, :find_asset)
|
10
|
+
base.send(:alias_method, :find_asset, :find_asset_with_convertion)
|
11
|
+
end
|
12
|
+
|
13
|
+
def find_asset_with_convertion(path, options = {})
|
14
|
+
convert = false
|
15
|
+
if path.ends_with?('.svg.png')
|
16
|
+
path = path.gsub(/\.png/, '')
|
17
|
+
convert = true
|
18
|
+
end
|
19
|
+
asset = find_asset_without_convertion(path, options)
|
20
|
+
|
21
|
+
if convert
|
22
|
+
asset = svg_asset_to_static_png(asset)
|
23
|
+
end
|
24
|
+
|
25
|
+
asset
|
26
|
+
end
|
27
|
+
|
28
|
+
def svg2png_cache_path
|
29
|
+
@cache_path ||= cache.instance_variable_get(:@root).join('svg2png')
|
30
|
+
end
|
31
|
+
|
32
|
+
def svg_asset_to_static_png(svg_asset)
|
33
|
+
tmp = Tempfile.new(['svg2png', '.svg'])
|
34
|
+
svg_asset.write_to(tmp.path)
|
35
|
+
::Sprockets::StaticAsset.new(self, svg_asset.logical_path + '.png', Pathname.new(tmp.path + '.png'))
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'sprockets/svg/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'sprockets-svg'
|
8
|
+
spec.version = Sprockets::Svg::VERSION
|
9
|
+
spec.authors = ['Jean Boussier']
|
10
|
+
spec.email = ['jean.boussier@gmail.com']
|
11
|
+
spec.summary = %q{SVG toolchain for sprockets}
|
12
|
+
spec.description = %q{Minify SVG assets, and optionally convert them to PNG for browser compatibility.}
|
13
|
+
spec.homepage = ''
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ['lib']
|
20
|
+
|
21
|
+
spec.add_development_dependency 'bundler', '~> 1.5'
|
22
|
+
spec.add_development_dependency 'rake'
|
23
|
+
spec.add_dependency 'sprockets'
|
24
|
+
spec.add_dependency 'nokogiri'
|
25
|
+
spec.add_dependency 'rmagick'
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,127 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sprockets-svg
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jean Boussier
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-04-12 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.5'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.5'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: sprockets
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: nokogiri
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rmagick
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: Minify SVG assets, and optionally convert them to PNG for browser compatibility.
|
84
|
+
email:
|
85
|
+
- jean.boussier@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- Gemfile
|
92
|
+
- LICENSE.txt
|
93
|
+
- README.md
|
94
|
+
- Rakefile
|
95
|
+
- lib/sprockets-svg.rb
|
96
|
+
- lib/sprockets/svg.rb
|
97
|
+
- lib/sprockets/svg/cleaner.rb
|
98
|
+
- lib/sprockets/svg/png.rb
|
99
|
+
- lib/sprockets/svg/railtie.rb
|
100
|
+
- lib/sprockets/svg/server.rb
|
101
|
+
- lib/sprockets/svg/version.rb
|
102
|
+
- sprockets-svg.gemspec
|
103
|
+
homepage: ''
|
104
|
+
licenses:
|
105
|
+
- MIT
|
106
|
+
metadata: {}
|
107
|
+
post_install_message:
|
108
|
+
rdoc_options: []
|
109
|
+
require_paths:
|
110
|
+
- lib
|
111
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
112
|
+
requirements:
|
113
|
+
- - ">="
|
114
|
+
- !ruby/object:Gem::Version
|
115
|
+
version: '0'
|
116
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
117
|
+
requirements:
|
118
|
+
- - ">="
|
119
|
+
- !ruby/object:Gem::Version
|
120
|
+
version: '0'
|
121
|
+
requirements: []
|
122
|
+
rubyforge_project:
|
123
|
+
rubygems_version: 2.2.1
|
124
|
+
signing_key:
|
125
|
+
specification_version: 4
|
126
|
+
summary: SVG toolchain for sprockets
|
127
|
+
test_files: []
|