sprockets-svg 0.1.2 → 0.2.0
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/lib/sprockets/svg.rb +9 -3
- data/lib/sprockets/svg/proxy_asset.rb +85 -0
- data/lib/sprockets/svg/server.rb +1 -13
- data/lib/sprockets/svg/version.rb +1 -1
- metadata +3 -3
- data/lib/sprockets/svg/png.rb +0 -35
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fc803303d0b7589ee6a223b66943829369763e2d
|
4
|
+
data.tar.gz: 99cc9b849a270e98fedd58d51436d53f969c155a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 14c328cc5f480d9714002fc756ac2513202c1ba79237f874d859c63490f9241c58ca77a05599a3e9bcef48aedcd0de20dd05c5a2482fcacf552e9143767a6b0b
|
7
|
+
data.tar.gz: 4cac1740330bb335efd7e64606ca894ada311f468e190419725094a87e6818a19c7b908f0c7e027e289e51d7a2e64f1de877d3d5aea7de348bb66f391f3e3846
|
data/lib/sprockets/svg.rb
CHANGED
@@ -1,11 +1,19 @@
|
|
1
1
|
require 'sprockets/svg/version'
|
2
2
|
|
3
3
|
require 'nokogiri'
|
4
|
+
require 'RMagick'
|
4
5
|
|
5
6
|
module Sprockets
|
6
7
|
module Svg
|
7
8
|
extend self
|
8
9
|
|
10
|
+
# TODO: integrate svgo instead: https://github.com/svg/svgo
|
11
|
+
# See https://github.com/lautis/uglifier on how to integrate a npm package as a gem.
|
12
|
+
def self.convert(svg_path, png_path)
|
13
|
+
image = Magick::ImageList.new(svg_path)
|
14
|
+
image.write(png_path)
|
15
|
+
end
|
16
|
+
|
9
17
|
def self.image?(path)
|
10
18
|
return false unless path.ends_with?('.svg')
|
11
19
|
|
@@ -24,11 +32,9 @@ module Sprockets
|
|
24
32
|
end
|
25
33
|
|
26
34
|
require_relative 'svg/cleaner'
|
27
|
-
require_relative 'svg/
|
35
|
+
require_relative 'svg/proxy_asset'
|
28
36
|
require_relative 'svg/server'
|
29
37
|
|
30
|
-
Sprockets::Asset.send(:include, Sprockets::Svg::Png)
|
31
|
-
Sprockets::StaticAsset.send(:include, Sprockets::Svg::Png)
|
32
38
|
Sprockets::Environment.send(:include, Sprockets::Svg::Server)
|
33
39
|
Sprockets::Index.send(:include, Sprockets::Svg::Server)
|
34
40
|
|
@@ -0,0 +1,85 @@
|
|
1
|
+
module Sprockets
|
2
|
+
module Svg
|
3
|
+
class ProxyAsset
|
4
|
+
|
5
|
+
def initialize(original_asset)
|
6
|
+
@original_asset = original_asset
|
7
|
+
end
|
8
|
+
|
9
|
+
def digest_path
|
10
|
+
png_path(@original_asset.digest_path)
|
11
|
+
end
|
12
|
+
|
13
|
+
def logical_path
|
14
|
+
@original_asset.logical_path + '.png'
|
15
|
+
end
|
16
|
+
|
17
|
+
def write_to(filename, options = {})
|
18
|
+
# Gzip contents if filename has '.gz'
|
19
|
+
options[:compress] ||= File.extname(filename) == '.gz'
|
20
|
+
|
21
|
+
tmp_path = Tempfile.new(['svg2png', '.svg']).path
|
22
|
+
png_path = tmp_path + '.png'
|
23
|
+
|
24
|
+
@original_asset.write_to(tmp_path, options.merge(compress: false))
|
25
|
+
Svg.convert(tmp_path, png_path)
|
26
|
+
|
27
|
+
FileUtils.mkdir_p File.dirname(filename)
|
28
|
+
|
29
|
+
if options[:compress]
|
30
|
+
# Open file and run it through `Zlib`
|
31
|
+
File.open(png_path, 'rb') do |rd|
|
32
|
+
File.open("#{filename}+", 'wb') do |wr|
|
33
|
+
gz = Zlib::GzipWriter.new(wr, Zlib::BEST_COMPRESSION)
|
34
|
+
gz.mtime = mtime.to_i
|
35
|
+
buf = ""
|
36
|
+
while rd.read(16384, buf)
|
37
|
+
gz.write(buf)
|
38
|
+
end
|
39
|
+
gz.close
|
40
|
+
end
|
41
|
+
end
|
42
|
+
else
|
43
|
+
# If no compression needs to be done, we can just copy it into place.
|
44
|
+
FileUtils.cp(png_path, "#{filename}+")
|
45
|
+
end
|
46
|
+
|
47
|
+
# Atomic write
|
48
|
+
FileUtils.mv("#{filename}+", filename)
|
49
|
+
|
50
|
+
# Set mtime correctly
|
51
|
+
File.utime(mtime, mtime, filename)
|
52
|
+
|
53
|
+
nil
|
54
|
+
ensure
|
55
|
+
# Ensure tmp file gets cleaned up
|
56
|
+
FileUtils.rm("#{filename}+") if File.exist?("#{filename}+")
|
57
|
+
end
|
58
|
+
|
59
|
+
def to_s
|
60
|
+
tmp_path = Tempfile.new(['png-cache', '.png']).path
|
61
|
+
write_to(tmp_path)
|
62
|
+
File.read(tmp_path)
|
63
|
+
end
|
64
|
+
|
65
|
+
private
|
66
|
+
|
67
|
+
def png_path(svg_path)
|
68
|
+
if svg_path =~ /^(.*)\-([0-9a-f]{32})\.svg$/
|
69
|
+
"#{$1}.svg-#{$2}.png"
|
70
|
+
else
|
71
|
+
"#{svg_path}.png"
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def method_missing(name, *args, &block)
|
76
|
+
if @original_asset.respond_to?(name)
|
77
|
+
@original_asset.public_send(name, *args, &block)
|
78
|
+
else
|
79
|
+
super
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
data/lib/sprockets/svg/server.rb
CHANGED
@@ -19,24 +19,12 @@ module Sprockets
|
|
19
19
|
asset = find_asset_without_conversion(path, options)
|
20
20
|
|
21
21
|
if asset && convert
|
22
|
-
asset =
|
22
|
+
asset = ProxyAsset.new(asset)
|
23
23
|
end
|
24
24
|
|
25
25
|
asset
|
26
26
|
end
|
27
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_path = Tempfile.new(['svg2png', '.svg']).path
|
34
|
-
svg_asset.write_to(tmp_path)
|
35
|
-
png_asset = ::Sprockets::StaticAsset.new(self, svg_asset.logical_path + '.png', Pathname.new(tmp_path + '.png'))
|
36
|
-
png_asset.instance_variable_set(:@digest, svg_asset.digest)
|
37
|
-
png_asset
|
38
|
-
end
|
39
|
-
|
40
28
|
def each_file(*args)
|
41
29
|
return to_enum(__method__) unless block_given?
|
42
30
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sprockets-svg
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jean Boussier
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-06-
|
11
|
+
date: 2014-06-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -83,7 +83,7 @@ files:
|
|
83
83
|
- lib/sprockets-svg.rb
|
84
84
|
- lib/sprockets/svg.rb
|
85
85
|
- lib/sprockets/svg/cleaner.rb
|
86
|
-
- lib/sprockets/svg/
|
86
|
+
- lib/sprockets/svg/proxy_asset.rb
|
87
87
|
- lib/sprockets/svg/railtie.rb
|
88
88
|
- lib/sprockets/svg/server.rb
|
89
89
|
- lib/sprockets/svg/version.rb
|
data/lib/sprockets/svg/png.rb
DELETED
@@ -1,35 +0,0 @@
|
|
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_conversion, :write_to)
|
8
|
-
base.send(:alias_method, :write_to, :write_to_with_png_conversion)
|
9
|
-
end
|
10
|
-
|
11
|
-
def write_to_with_png_conversion(path, options={})
|
12
|
-
write_to_without_png_conversion(path, options)
|
13
|
-
if Svg.image?(path)
|
14
|
-
Png.convert(path, png_path(path))
|
15
|
-
end
|
16
|
-
nil
|
17
|
-
end
|
18
|
-
|
19
|
-
def png_path(svg_path)
|
20
|
-
if svg_path =~ /^(.*)\-([0-9a-f]{40})\.svg$/
|
21
|
-
"#{$1}.svg-#{$2}.png"
|
22
|
-
else
|
23
|
-
"#{svg_path}.png"
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
# TODO: integrate svgo instead: https://github.com/svg/svgo
|
28
|
-
# See https://github.com/lautis/uglifier on how to integrate a npm package as a gem.
|
29
|
-
def self.convert(svg_path, png_path)
|
30
|
-
image = Magick::ImageList.new(svg_path)
|
31
|
-
image.write(png_path)
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|