sprockets-svg 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 85e56d6446c52c59929d03a10f8049bd82641227
4
- data.tar.gz: 1e909a1fbdfcf1296cad5ccb24f7d3553af71e95
3
+ metadata.gz: fc803303d0b7589ee6a223b66943829369763e2d
4
+ data.tar.gz: 99cc9b849a270e98fedd58d51436d53f969c155a
5
5
  SHA512:
6
- metadata.gz: 62c102f80c869e1972e13edb991c7bb816b1f0842a1e38f6854deb71f4ac81c1aea902e89a04df7fd52ac20e457df8f7d23753984aa35353ca5ef1b5ab11945d
7
- data.tar.gz: 285921e5a4a3821d757e789c594c1b61cb8cec311c167da13339297eca98e533d9f93e00c0b31ee01c797dc56c80ef084446ea392d285da488f494565353a741
6
+ metadata.gz: 14c328cc5f480d9714002fc756ac2513202c1ba79237f874d859c63490f9241c58ca77a05599a3e9bcef48aedcd0de20dd05c5a2482fcacf552e9143767a6b0b
7
+ data.tar.gz: 4cac1740330bb335efd7e64606ca894ada311f468e190419725094a87e6818a19c7b908f0c7e027e289e51d7a2e64f1de877d3d5aea7de348bb66f391f3e3846
@@ -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/png'
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
@@ -19,24 +19,12 @@ module Sprockets
19
19
  asset = find_asset_without_conversion(path, options)
20
20
 
21
21
  if asset && convert
22
- asset = svg_asset_to_static_png(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
 
@@ -1,5 +1,5 @@
1
1
  module Sprockets
2
2
  module Svg
3
- VERSION = '0.1.2'
3
+ VERSION = '0.2.0'
4
4
  end
5
5
  end
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.1.2
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-16 00:00:00.000000000 Z
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/png.rb
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
@@ -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