sprockets-svg 0.2.0 → 0.3.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: fc803303d0b7589ee6a223b66943829369763e2d
4
- data.tar.gz: 99cc9b849a270e98fedd58d51436d53f969c155a
3
+ metadata.gz: b586a285d7659583918ac48a90a7e03ae09cbdbb
4
+ data.tar.gz: 5f582344fde414f8a5ec3005b7694e9b0a85bf58
5
5
  SHA512:
6
- metadata.gz: 14c328cc5f480d9714002fc756ac2513202c1ba79237f874d859c63490f9241c58ca77a05599a3e9bcef48aedcd0de20dd05c5a2482fcacf552e9143767a6b0b
7
- data.tar.gz: 4cac1740330bb335efd7e64606ca894ada311f468e190419725094a87e6818a19c7b908f0c7e027e289e51d7a2e64f1de877d3d5aea7de348bb66f391f3e3846
6
+ metadata.gz: b2bef4d6f33651aa2cb7cfccc06ec89aac8a9b06d14794030b84fff2c0af408e396d7e6e59f1ba05dd4a1167de3027e93b7f74ab75289457330ecbc0db2e2b40
7
+ data.tar.gz: 7a1c248d2a2e82b125987c08f7c51c863d1119db9dfba072343191ce40f81b3a4fa8e8444f1825acba2e8973957734a04585b7723a08aecbf0a4abce17169352
data/.travis.yml CHANGED
@@ -1,4 +1,3 @@
1
1
  rvm:
2
- - 1.9.3
3
- - 2.0.0
4
- - 2.1.0
2
+ - 2.0
3
+ - 2.1
data/Gemfile CHANGED
@@ -8,4 +8,7 @@ gem 'rspec'
8
8
  gem 'rspec-rails'
9
9
  gem 'sass-rails'
10
10
  gem 'rails'
11
- gem 'byebug'
11
+
12
+ if RUBY_VERSION >= '2'
13
+ gem 'byebug'
14
+ end
data/lib/sprockets/svg.rb CHANGED
@@ -22,6 +22,14 @@ module Sprockets
22
22
  svg.attribute('height') && svg.attribute('width')
23
23
  end
24
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
31
+ end
32
+
25
33
  def install(assets)
26
34
  assets.register_preprocessor 'image/svg+xml', :svg_min do |context, data|
27
35
  Sprockets::Svg::Cleaner.process(data)
@@ -33,6 +41,7 @@ end
33
41
 
34
42
  require_relative 'svg/cleaner'
35
43
  require_relative 'svg/proxy_asset'
44
+ require_relative 'svg/sass_functions'
36
45
  require_relative 'svg/server'
37
46
 
38
47
  Sprockets::Environment.send(:include, Sprockets::Svg::Server)
@@ -7,13 +7,24 @@ module Sprockets
7
7
  end
8
8
 
9
9
  def digest_path
10
- png_path(@original_asset.digest_path)
10
+ ::Sprockets::Svg.png_path(@original_asset.digest_path)
11
11
  end
12
12
 
13
13
  def logical_path
14
14
  @original_asset.logical_path + '.png'
15
15
  end
16
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
+
17
28
  def write_to(filename, options = {})
18
29
  # Gzip contents if filename has '.gz'
19
30
  options[:compress] ||= File.extname(filename) == '.gz'
@@ -50,6 +61,8 @@ module Sprockets
50
61
  # Set mtime correctly
51
62
  File.utime(mtime, mtime, filename)
52
63
 
64
+ @length = File.stat(filename).size
65
+
53
66
  nil
54
67
  ensure
55
68
  # Ensure tmp file gets cleaned up
@@ -57,21 +70,15 @@ module Sprockets
57
70
  end
58
71
 
59
72
  def to_s
60
- tmp_path = Tempfile.new(['png-cache', '.png']).path
61
- write_to(tmp_path)
62
- File.read(tmp_path)
73
+ @source ||= begin
74
+ tmp_path = Tempfile.new(['png-cache', '.png']).path
75
+ write_to(tmp_path)
76
+ File.read(tmp_path)
77
+ end
63
78
  end
64
79
 
65
80
  private
66
81
 
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
82
  def method_missing(name, *args, &block)
76
83
  if @original_asset.respond_to?(name)
77
84
  @original_asset.public_send(name, *args, &block)
@@ -0,0 +1,38 @@
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
@@ -1,5 +1,5 @@
1
1
  module Sprockets
2
2
  module Svg
3
- VERSION = '0.2.0'
3
+ VERSION = '0.3.0'
4
4
  end
5
5
  end
@@ -0,0 +1,4 @@
1
+ .facebook-icon {
2
+ background-image: image-url('facebook.svg.png');
3
+ background-image: image-url('facebook.svg'), none;
4
+ }
@@ -1,7 +1,8 @@
1
1
  class AssetsController < ApplicationController
2
2
 
3
3
  def test
4
- render text: Rails.application.assets[params[:file]].to_s
4
+ asset = Rails.application.assets[params[:file]]
5
+ send_data asset.to_s, content_type: asset.content_type
5
6
  end
6
7
 
7
8
  end
data/spec/rails_spec.rb CHANGED
@@ -7,24 +7,17 @@ require 'json'
7
7
  COMPILED_ASSETS_PATH = File.expand_path('app/public/assets/', File.dirname(__FILE__))
8
8
  ASSETS = Pathname.new(COMPILED_ASSETS_PATH)
9
9
 
10
- describe 'Rake task' do
11
- before :each do
12
- FileUtils.mkdir_p(COMPILED_ASSETS_PATH)
13
- end
14
-
15
- after :each do
16
- FileUtils.rm_rf(COMPILED_ASSETS_PATH)
17
- end
10
+ describe 'Sprockets::Svg' do
18
11
 
19
- let(:svg_name) { 'facebook-052e5d3070b50e0b3e60014378ece289.svg' }
12
+ let(:svg_name) { 'facebook-213385e4d9304ef93b23578fc4c93440.svg' }
20
13
 
21
- let(:png_name) { 'facebook.svg-052e5d3070b50e0b3e60014378ece289.png' }
14
+ let(:png_name) { 'facebook.svg-213385e4d9304ef93b23578fc4c93440.png' }
22
15
 
23
16
  let(:svg_path) { ASSETS.join(svg_name) }
24
17
 
25
18
  let(:png_path) { ASSETS.join(png_name) }
26
19
 
27
- let(:font_path) { ASSETS.join('fontawesome-webfont-7534bcc67141208ea7a8c9be3a960f17.svg') }
20
+ let(:font_path) { ASSETS.join('fontawesome-webfont-040e9dcf8f420c96cbb5f0985fe09185.svg') }
28
21
 
29
22
  let(:svg_fingerprint) { Digest::MD5.hexdigest(svg_path.read) }
30
23
 
@@ -34,34 +27,53 @@ describe 'Rake task' do
34
27
 
35
28
  let(:png_source) { png_path.read }
36
29
 
37
- it "generate a PNG from the SVG source" do
38
- output = `cd spec/app; bundle exec rake assets:precompile 2>&1`
39
- puts output unless $?.success?
40
- $?.should be_success
30
+ describe 'Rake task' do
31
+ before :each do
32
+ FileUtils.mkdir_p(COMPILED_ASSETS_PATH)
33
+ end
34
+
35
+ after :each do
36
+ FileUtils.rm_rf(COMPILED_ASSETS_PATH)
37
+ end
41
38
 
42
- expect(svg_path).to be_exist
43
- expect(png_path).to be_exist
39
+ it "generate a PNG from the SVG source" do
40
+ output = `cd spec/app; bundle exec rake assets:precompile 2>&1`
41
+ puts output unless $?.success?
42
+ expect($?).to be_success
44
43
 
45
- expect(svg_fingerprint).to be == '85385a2b8357015a4ae6d8ab782d5389'
46
- # Metadata etc are kinda time dependant, so this assertion is the best I can do.
47
- expect(png_source).to be_starts_with("\x89PNG\r\n")
44
+ expect(svg_path).to be_exist
45
+ expect(png_path).to be_exist
48
46
 
49
- expect(manifest['files'][svg_name]).to be_present
50
- expect(manifest['files'][png_name]).to be_present
47
+ expect(svg_fingerprint).to be == '85385a2b8357015a4ae6d8ab782d5389'
48
+ # Metadata etc are kinda time dependant, so this assertion is the best I can do.
49
+ expect(png_source).to be_starts_with("\x89PNG\r\n")
51
50
 
52
- expect(manifest['assets']['facebook.svg.png']).to be == png_name
51
+ expect(manifest['files'][svg_name]).to be_present
52
+ expect(manifest['files'][png_name]).to be_present
53
53
 
54
- expect(font_path).to be_exist
55
- expect(Dir[ASSETS.join('fontawesome-webfont-*.png')]).to be_empty
54
+ expect(manifest['assets']['facebook.svg.png']).to be == png_name
55
+
56
+ expect(font_path).to be_exist
57
+ expect(Dir[ASSETS.join('fontawesome-webfont-*.png')]).to be_empty
58
+ end
56
59
  end
57
- end
58
60
 
59
- describe AssetsController, type: :controller do
61
+ describe AssetsController, type: :controller do
62
+
63
+ it 'rack' do
64
+ get :test, file: 'facebook.svg.png'
65
+ expect(response).to be_success
66
+ expect(response.body.force_encoding('utf-8')).to be_starts_with("\x89PNG\r\n".force_encoding('utf-8'))
67
+ expect(response.headers['Content-Type']).to be == 'image/png'
68
+ end
69
+
70
+ it 'compile scss' do
71
+ get :test, file: 'application.css'
72
+ expect(response).to be_success
73
+ expect(response.body).to include svg_name
74
+ expect(response.body).to include png_name
75
+ end
60
76
 
61
- it 'rack' do
62
- get :test, file: 'facebook.svg.png'
63
- expect(response).to be_success
64
- expect(response.body.force_encoding('utf-8')).to be_starts_with("\x89PNG\r\n")
65
77
  end
66
78
 
67
79
  end
data/spec/spec_helper.rb CHANGED
@@ -4,15 +4,6 @@ require_relative 'app/config/environment'
4
4
  require 'rspec/rails'
5
5
  require_relative '../lib/sprockets-svg'
6
6
 
7
-
8
- RSpec.configure do |c|
9
- c.filter_run_excluding not_jruby: RUBY_PLATFORM == 'java'
10
- end
11
-
12
-
13
7
  RSpec.configure do |config|
14
- config.treat_symbols_as_metadata_keys_with_true_values = true
15
- config.run_all_when_everything_filtered = true
16
- config.filter_run :focus
17
8
  config.order = 'random'
18
9
  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.2.0
4
+ version: 0.3.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-17 00:00:00.000000000 Z
11
+ date: 2014-07-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -85,12 +85,14 @@ files:
85
85
  - lib/sprockets/svg/cleaner.rb
86
86
  - lib/sprockets/svg/proxy_asset.rb
87
87
  - lib/sprockets/svg/railtie.rb
88
+ - lib/sprockets/svg/sass_functions.rb
88
89
  - lib/sprockets/svg/server.rb
89
90
  - lib/sprockets/svg/version.rb
90
91
  - spec/app/.gitignore
91
92
  - spec/app/Rakefile
92
93
  - spec/app/app/assets/fonts/fontawesome-webfont.svg
93
94
  - spec/app/app/assets/images/facebook.svg
95
+ - spec/app/app/assets/stylesheets/application.css.scss
94
96
  - spec/app/app/controllers/application_controller.rb
95
97
  - spec/app/app/controllers/assets_controller.rb
96
98
  - spec/app/config.ru
@@ -123,7 +125,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
123
125
  version: '0'
124
126
  requirements: []
125
127
  rubyforge_project:
126
- rubygems_version: 2.2.1
128
+ rubygems_version: 2.2.2
127
129
  signing_key:
128
130
  specification_version: 4
129
131
  summary: SVG toolchain for sprockets
@@ -132,6 +134,7 @@ test_files:
132
134
  - spec/app/Rakefile
133
135
  - spec/app/app/assets/fonts/fontawesome-webfont.svg
134
136
  - spec/app/app/assets/images/facebook.svg
137
+ - spec/app/app/assets/stylesheets/application.css.scss
135
138
  - spec/app/app/controllers/application_controller.rb
136
139
  - spec/app/app/controllers/assets_controller.rb
137
140
  - spec/app/config.ru