sprockets-svg 0.4.2 → 1.0.0.beta1
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/Gemfile +1 -0
- data/lib/sprockets/svg.rb +9 -26
- data/lib/sprockets/svg/railtie.rb +5 -3
- data/lib/sprockets/svg/version.rb +1 -1
- data/spec/app/app/assets/stylesheets/application.css.scss +1 -1
- data/spec/app/config/environments/test.rb +3 -1
- data/spec/rails_spec.rb +11 -8
- data/sprockets-svg.gemspec +1 -1
- metadata +6 -10
- data/lib/sprockets/svg/proxy_asset.rb +0 -93
- data/lib/sprockets/svg/sass_functions.rb +0 -38
- data/lib/sprockets/svg/server.rb +0 -41
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 53db6df6150544cc5980d807795150e37467ef64
|
4
|
+
data.tar.gz: c6617061fa6ba299513e78879557320b0ce87dfe
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2ef4008781a13823cc905fff0bb1e4ff084ac70721abe720fa5d7a4a7dabe19ff063c9aa43378101bb62e6e8e898e1028a651b6d547283734a935625b20a6997
|
7
|
+
data.tar.gz: 749fb5253184279f9b1af415447988079f841ea42e90f32c63952a5d4c93abb1a3aac025abf7c6a873fb6a88e8e90bf6377a1a0d4ab4d2ebab7c06f09c8894dd
|
data/Gemfile
CHANGED
data/lib/sprockets/svg.rb
CHANGED
@@ -1,6 +1,5 @@
|
|
1
1
|
require 'sprockets/svg/version'
|
2
2
|
|
3
|
-
require 'nokogiri'
|
4
3
|
require 'rmagick'
|
5
4
|
|
6
5
|
module Sprockets
|
@@ -9,42 +8,26 @@ module Sprockets
|
|
9
8
|
|
10
9
|
# TODO: integrate svgo instead: https://github.com/svg/svgo
|
11
10
|
# See https://github.com/lautis/uglifier on how to integrate a npm package as a gem.
|
12
|
-
def self.convert(
|
13
|
-
|
14
|
-
image.
|
15
|
-
|
16
|
-
|
17
|
-
def self.image?(path)
|
18
|
-
return false unless path.ends_with?('.svg')
|
19
|
-
|
20
|
-
document = Nokogiri::XML(File.read(path))
|
21
|
-
svg = document.css('svg')
|
22
|
-
svg.attribute('height') && svg.attribute('width')
|
23
|
-
end
|
24
|
-
|
25
|
-
def self.png_path(svg_path)
|
26
|
-
if svg_path =~ /^(.*)\-([0-9a-f]{32})\.svg$/
|
27
|
-
"#{$1}.svg-#{$2}.png"
|
28
|
-
else
|
29
|
-
"#{svg_path}.png"
|
30
|
-
end
|
11
|
+
def self.convert(svg_blob)
|
12
|
+
image_list = Magick::Image.from_blob(svg_blob) { self.format = 'SVG' }
|
13
|
+
image = image_list.first
|
14
|
+
image.format = 'PNG'
|
15
|
+
image.to_blob
|
31
16
|
end
|
32
17
|
|
33
18
|
def install(assets)
|
34
19
|
assets.register_preprocessor 'image/svg+xml', :svg_min do |context, data|
|
35
20
|
Sprockets::Svg::Cleaner.process(data)
|
36
21
|
end
|
22
|
+
|
23
|
+
assets.register_transformer 'image/svg+xml', 'image/png', -> (input) {
|
24
|
+
Sprockets::Svg.convert(input[:data])
|
25
|
+
}
|
37
26
|
end
|
38
27
|
|
39
28
|
end
|
40
29
|
end
|
41
30
|
|
42
31
|
require_relative 'svg/cleaner'
|
43
|
-
require_relative 'svg/proxy_asset'
|
44
|
-
require_relative 'svg/sass_functions'
|
45
|
-
require_relative 'svg/server'
|
46
|
-
|
47
|
-
Sprockets::Environment.send(:include, Sprockets::Svg::Server)
|
48
|
-
Sprockets::Index.send(:include, Sprockets::Svg::Server)
|
49
32
|
|
50
33
|
require_relative 'svg/railtie' if defined?(Rails)
|
@@ -3,10 +3,12 @@ begin
|
|
3
3
|
|
4
4
|
module Sprockets::Svg
|
5
5
|
class Railtie < ::Rails::Railtie
|
6
|
-
initializer :setup_sprockets_svg
|
7
|
-
|
6
|
+
initializer :setup_sprockets_svg do
|
7
|
+
config.assets.configure do |env|
|
8
|
+
Sprockets::Svg.install(env)
|
9
|
+
end
|
8
10
|
end
|
9
11
|
end
|
10
12
|
end
|
11
13
|
rescue LoadError
|
12
|
-
end
|
14
|
+
end
|
@@ -1,7 +1,9 @@
|
|
1
1
|
App::Application.configure do
|
2
|
+
config.cache_store = :memory_store
|
3
|
+
config.assets.configure { |env| env.cache = Rails.cache }
|
2
4
|
config.assets.digest = true
|
3
5
|
config.cache_classes = true
|
4
|
-
config.
|
6
|
+
config.serve_static_files = true
|
5
7
|
config.static_cache_control = "public, max-age=3600"
|
6
8
|
config.eager_load = false
|
7
9
|
config.consider_all_requests_local = true
|
data/spec/rails_spec.rb
CHANGED
@@ -9,19 +9,19 @@ ASSETS = Pathname.new(COMPILED_ASSETS_PATH)
|
|
9
9
|
|
10
10
|
describe 'Sprockets::Svg' do
|
11
11
|
|
12
|
-
let(:svg_name) { 'facebook
|
12
|
+
let(:svg_name) { manifest['assets']['facebook.svg'] }
|
13
13
|
|
14
|
-
let(:png_name) { 'facebook.
|
14
|
+
let(:png_name) { manifest['assets']['facebook.png'] }
|
15
15
|
|
16
16
|
let(:svg_path) { ASSETS.join(svg_name) }
|
17
17
|
|
18
18
|
let(:png_path) { ASSETS.join(png_name) }
|
19
19
|
|
20
|
-
let(:font_path) { ASSETS.join('fontawesome-webfont-
|
20
|
+
let(:font_path) { ASSETS.join('fontawesome-webfont-62b810b30c0775c7680f56a9d411feb0b239a24aa33afcdda468c4686eeee9bb.svg') }
|
21
21
|
|
22
22
|
let(:svg_fingerprint) { Digest::MD5.hexdigest(svg_path.read) }
|
23
23
|
|
24
|
-
let(:manifest_path) { Dir[ASSETS.join('manifest-*.json')].first }
|
24
|
+
let(:manifest_path) { Dir[ASSETS.join('.sprockets-manifest-*.json')].first }
|
25
25
|
|
26
26
|
let(:manifest) { JSON.parse(File.read(manifest_path)) }
|
27
27
|
|
@@ -41,6 +41,9 @@ describe 'Sprockets::Svg' do
|
|
41
41
|
puts output unless $?.success?
|
42
42
|
expect($?).to be_success
|
43
43
|
|
44
|
+
expect(svg_name).not_to be_nil
|
45
|
+
expect(png_name).not_to be_nil
|
46
|
+
|
44
47
|
expect(svg_path).to be_exist
|
45
48
|
expect(png_path).to be_exist
|
46
49
|
|
@@ -51,7 +54,7 @@ describe 'Sprockets::Svg' do
|
|
51
54
|
expect(manifest['files'][svg_name]).to be_present
|
52
55
|
expect(manifest['files'][png_name]).to be_present
|
53
56
|
|
54
|
-
expect(manifest['assets']['facebook.
|
57
|
+
expect(manifest['assets']['facebook.png']).to be == png_name
|
55
58
|
|
56
59
|
expect(font_path).to be_exist
|
57
60
|
expect(Dir[ASSETS.join('fontawesome-webfont-*.png')]).to be_empty
|
@@ -61,7 +64,7 @@ describe 'Sprockets::Svg' do
|
|
61
64
|
describe AssetsController, type: :controller do
|
62
65
|
|
63
66
|
it 'rack' do
|
64
|
-
get :test, file: 'facebook.
|
67
|
+
get :test, file: 'facebook.png'
|
65
68
|
expect(response).to be_success
|
66
69
|
expect(response.body.force_encoding('utf-8')).to be_starts_with("\x89PNG\r\n".force_encoding('utf-8'))
|
67
70
|
expect(response.headers['Content-Type']).to be == 'image/png'
|
@@ -70,8 +73,8 @@ describe 'Sprockets::Svg' do
|
|
70
73
|
it 'compile scss' do
|
71
74
|
get :test, file: 'application.css'
|
72
75
|
expect(response).to be_success
|
73
|
-
expect(response.body).to
|
74
|
-
expect(response.body).to
|
76
|
+
expect(response.body).to match %r{url\(/assets/facebook-\w+\.svg\)}
|
77
|
+
expect(response.body).to match %r{url\(/assets/facebook-\w+\.png\)}
|
75
78
|
end
|
76
79
|
|
77
80
|
end
|
data/sprockets-svg.gemspec
CHANGED
@@ -19,7 +19,7 @@ Gem::Specification.new do |spec|
|
|
19
19
|
spec.require_paths = ['lib']
|
20
20
|
|
21
21
|
spec.add_development_dependency 'bundler', '~> 1.5'
|
22
|
-
spec.add_dependency 'sprockets'
|
22
|
+
spec.add_dependency 'sprockets', '>= 3'
|
23
23
|
spec.add_dependency 'nokogiri'
|
24
24
|
spec.add_dependency 'rmagick'
|
25
25
|
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.
|
4
|
+
version: 1.0.0.beta1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jean Boussier
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-08-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -30,14 +30,14 @@ dependencies:
|
|
30
30
|
requirements:
|
31
31
|
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
33
|
+
version: '3'
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
40
|
+
version: '3'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: nokogiri
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -83,10 +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/proxy_asset.rb
|
87
86
|
- lib/sprockets/svg/railtie.rb
|
88
|
-
- lib/sprockets/svg/sass_functions.rb
|
89
|
-
- lib/sprockets/svg/server.rb
|
90
87
|
- lib/sprockets/svg/version.rb
|
91
88
|
- spec/app/.gitignore
|
92
89
|
- spec/app/Rakefile
|
@@ -122,9 +119,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
122
119
|
version: '0'
|
123
120
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
124
121
|
requirements:
|
125
|
-
- - "
|
122
|
+
- - ">"
|
126
123
|
- !ruby/object:Gem::Version
|
127
|
-
version:
|
124
|
+
version: 1.3.1
|
128
125
|
requirements: []
|
129
126
|
rubyforge_project:
|
130
127
|
rubygems_version: 2.4.6
|
@@ -150,4 +147,3 @@ test_files:
|
|
150
147
|
- spec/fixtures/source.svg
|
151
148
|
- spec/rails_spec.rb
|
152
149
|
- spec/spec_helper.rb
|
153
|
-
has_rdoc:
|
@@ -1,93 +0,0 @@
|
|
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
|
-
::Sprockets::Svg.png_path(@original_asset.digest_path)
|
11
|
-
end
|
12
|
-
|
13
|
-
def logical_path
|
14
|
-
@original_asset.logical_path + '.png'
|
15
|
-
end
|
16
|
-
|
17
|
-
def length
|
18
|
-
# If length isn't already set we default to original asset length.
|
19
|
-
# It is most certainly wrong, but at least it will be bigger than the real size, so it is unlikely to create any issue.
|
20
|
-
# And most importantly it prevent useless on the fly compilation
|
21
|
-
@length || @original_asset.length
|
22
|
-
end
|
23
|
-
|
24
|
-
def content_type
|
25
|
-
'image/png'
|
26
|
-
end
|
27
|
-
|
28
|
-
def write_to(filename, options = {})
|
29
|
-
# Gzip contents if filename has '.gz'
|
30
|
-
options[:compress] ||= File.extname(filename) == '.gz'
|
31
|
-
|
32
|
-
file = Tempfile.new(['svg2png', '.svg'])
|
33
|
-
tmp_path = file.path
|
34
|
-
png_path = tmp_path + '.png'
|
35
|
-
|
36
|
-
@original_asset.write_to(tmp_path, options.merge(compress: false))
|
37
|
-
Svg.convert(tmp_path, png_path)
|
38
|
-
|
39
|
-
FileUtils.mkdir_p File.dirname(filename)
|
40
|
-
|
41
|
-
if options[:compress]
|
42
|
-
# Open file and run it through `Zlib`
|
43
|
-
File.open(png_path, 'rb') do |rd|
|
44
|
-
File.open("#{filename}+", 'wb') do |wr|
|
45
|
-
gz = Zlib::GzipWriter.new(wr, Zlib::BEST_COMPRESSION)
|
46
|
-
gz.mtime = mtime.to_i
|
47
|
-
buf = ""
|
48
|
-
while rd.read(16384, buf)
|
49
|
-
gz.write(buf)
|
50
|
-
end
|
51
|
-
gz.close
|
52
|
-
end
|
53
|
-
end
|
54
|
-
else
|
55
|
-
# If no compression needs to be done, we can just copy it into place.
|
56
|
-
FileUtils.cp(png_path, "#{filename}+")
|
57
|
-
end
|
58
|
-
|
59
|
-
# Atomic write
|
60
|
-
FileUtils.mv("#{filename}+", filename)
|
61
|
-
|
62
|
-
# Set mtime correctly
|
63
|
-
File.utime(mtime, mtime, filename)
|
64
|
-
|
65
|
-
@length = File.stat(filename).size
|
66
|
-
|
67
|
-
nil
|
68
|
-
ensure
|
69
|
-
# Ensure tmp file gets cleaned up
|
70
|
-
FileUtils.rm("#{filename}+") if File.exist?("#{filename}+")
|
71
|
-
end
|
72
|
-
|
73
|
-
def to_s
|
74
|
-
@source ||= begin
|
75
|
-
tmp_path = Tempfile.new(['png-cache', '.png']).path
|
76
|
-
write_to(tmp_path)
|
77
|
-
File.read(tmp_path)
|
78
|
-
end
|
79
|
-
end
|
80
|
-
|
81
|
-
private
|
82
|
-
|
83
|
-
def method_missing(name, *args, &block)
|
84
|
-
if @original_asset.respond_to?(name)
|
85
|
-
@original_asset.public_send(name, *args, &block)
|
86
|
-
else
|
87
|
-
super
|
88
|
-
end
|
89
|
-
end
|
90
|
-
|
91
|
-
end
|
92
|
-
end
|
93
|
-
end
|
@@ -1,38 +0,0 @@
|
|
1
|
-
module Sprockets
|
2
|
-
module Svg
|
3
|
-
module SassFunctions
|
4
|
-
|
5
|
-
def image_path(path)
|
6
|
-
Sass::Script::String.new(svg2png_image_path(path.value), :string)
|
7
|
-
end
|
8
|
-
|
9
|
-
def image_url(path)
|
10
|
-
Sass::Script::String.new("url(" + svg2png_image_path(path.value) + ")")
|
11
|
-
end
|
12
|
-
|
13
|
-
def svg2png_image_path(path)
|
14
|
-
convert = false
|
15
|
-
if path.ends_with?('.svg.png')
|
16
|
-
path = path.gsub(/\.png$/, '')
|
17
|
-
convert = true
|
18
|
-
end
|
19
|
-
|
20
|
-
url = sprockets_context.image_path(path)
|
21
|
-
convert ? ::Sprockets::Svg.png_path(url) : url
|
22
|
-
end
|
23
|
-
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
Sprockets::SassTemplate.class_eval do
|
29
|
-
|
30
|
-
def initialize_engine_with_png_conversion
|
31
|
-
initialize_engine_without_png_conversion
|
32
|
-
::Sass::Script::Functions.send :include, Sprockets::Svg::SassFunctions
|
33
|
-
end
|
34
|
-
|
35
|
-
alias_method :initialize_engine_without_png_conversion, :initialize_engine
|
36
|
-
alias_method :initialize_engine, :initialize_engine_with_png_conversion
|
37
|
-
|
38
|
-
end
|
data/lib/sprockets/svg/server.rb
DELETED
@@ -1,41 +0,0 @@
|
|
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_conversion, :find_asset)
|
10
|
-
base.send(:alias_method, :find_asset, :find_asset_with_conversion)
|
11
|
-
end
|
12
|
-
|
13
|
-
def find_asset_with_conversion(path, options = {})
|
14
|
-
convert = false
|
15
|
-
if path.to_s.ends_with?('.svg.png')
|
16
|
-
path = path.gsub(/\.png/, '')
|
17
|
-
convert = true
|
18
|
-
end
|
19
|
-
asset = find_asset_without_conversion(path, options)
|
20
|
-
|
21
|
-
if asset && convert
|
22
|
-
asset = ProxyAsset.new(asset)
|
23
|
-
end
|
24
|
-
|
25
|
-
asset
|
26
|
-
end
|
27
|
-
|
28
|
-
def each_file(*args)
|
29
|
-
return to_enum(__method__) unless block_given?
|
30
|
-
|
31
|
-
super do |path|
|
32
|
-
yield path
|
33
|
-
if Svg.image?(path.to_s)
|
34
|
-
yield Pathname.new(path.to_s + '.png')
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|