sprockets-svg 0.0.4 → 0.1.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: 20ce718493c386f82a692df0b2164270e68d1646
4
- data.tar.gz: edf737ce5bdb7e0a450b07db04a0ca74f1e0723e
3
+ metadata.gz: 920dbc50372c4ed37c7ae51dde3e0b559ab618ea
4
+ data.tar.gz: c1d1bed364e46f9d17e53e95f53558da4e450079
5
5
  SHA512:
6
- metadata.gz: a6d6859848b0ee077d6a79a4c770168609797e181f3ac351e8fba884daf6a08e9a0b6d48b1cbd1f73788190fb5622204e04626f591c7150a9695aff93fb1be8c
7
- data.tar.gz: c8426d7080ef64fc5f41e6178ebf7aac49e844ce23116df4767d32be349b453e2b673e02a2a89f40a923f37bea666dc34a41e5fb6fcba1494a60c33c3b9e080f
6
+ metadata.gz: 2d25bb9d9a0b5533196d2f4a9450c75f87be8391fd21965b208ec92e51a7f155b0859db77d554e2cb067c1bace026f8a47fe3ab5e63388597cd1dfd5f684a118
7
+ data.tar.gz: e361fd605239e6decd33387bcad7368db8dfc43ad9d55a0c4bcd5b21ca20988c28332c9ef5ffc21ced771b0e7eaf21afb02e9647875cfe303fac8ac3ab25606c
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ rvm:
2
+ - 1.9.3
3
+ - 2.0.0
4
+ - 2.1.0
data/Gemfile CHANGED
@@ -2,3 +2,10 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in sprockets-svg.gemspec
4
4
  gemspec
5
+
6
+ gem 'rake'
7
+ gem 'rspec'
8
+ gem 'rspec-rails'
9
+ gem 'sass-rails'
10
+ gem 'rails'
11
+ gem 'byebug'
data/README.md CHANGED
@@ -1,3 +1,7 @@
1
+ [![Build Status](https://travis-ci.org/byroot/sprockets-svg.svg)](https://travis-ci.org/byroot/sprockets-svg)
2
+ [![Code Climate](https://codeclimate.com/github/byroot/sprockets-svg.png)](https://codeclimate.com/github/byroot/sprockets-svg)
3
+ [![Gem Version](https://badge.fury.io/rb/sprockets-svg.png)](http://badge.fury.io/rb/sprockets-svg)
4
+
1
5
  # Sprockets::Svg
2
6
 
3
7
  SVG toolchain for sprockets
data/Rakefile CHANGED
@@ -1 +1,7 @@
1
1
  require 'bundler/gem_tasks'
2
+
3
+ require 'rspec/core/rake_task'
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task :default => :spec
@@ -6,7 +6,6 @@ module Sprockets
6
6
  # TODO: integrate svgo instead: https://github.com/svg/svgo
7
7
  # See https://github.com/lautis/uglifier on how to integrate a npm package as a gem.
8
8
  def self.process(svg)
9
- p :preprocess_svg
10
9
  document = Nokogiri::XML(svg)
11
10
  document.css('[display=none]').remove()
12
11
  document.to_s
@@ -10,16 +10,18 @@ module Sprockets
10
10
 
11
11
  def write_to_with_png_conversion(path, options={})
12
12
  write_to_without_png_conversion(path, options)
13
- if path.ends_with?('.svg') && image?(path)
14
- Png.convert(path, path + '.png')
13
+ if Svg.image?(path)
14
+ Png.convert(path, png_path(path))
15
15
  end
16
16
  nil
17
17
  end
18
18
 
19
- def image?(path)
20
- document = Nokogiri::XML(File.read(path))
21
- svg = document.css('svg')
22
- svg.attribute('height') && svg.attribute('width')
19
+ def png_path(svg_path)
20
+ if svg_path =~ /^(.*)\-([0-9a-f]+)\.svg$/
21
+ "#{$1}.svg-#{$2}.png"
22
+ else
23
+ "#{svg_path}.png"
24
+ end
23
25
  end
24
26
 
25
27
  # TODO: integrate svgo instead: https://github.com/svg/svgo
@@ -12,13 +12,13 @@ module Sprockets
12
12
 
13
13
  def find_asset_with_conversion(path, options = {})
14
14
  convert = false
15
- if path.ends_with?('.svg.png')
15
+ if path.to_s.ends_with?('.svg.png')
16
16
  path = path.gsub(/\.png/, '')
17
17
  convert = true
18
18
  end
19
19
  asset = find_asset_without_conversion(path, options)
20
20
 
21
- if convert
21
+ if asset && convert
22
22
  asset = svg_asset_to_static_png(asset)
23
23
  end
24
24
 
@@ -32,7 +32,20 @@ module Sprockets
32
32
  def svg_asset_to_static_png(svg_asset)
33
33
  tmp_path = Tempfile.new(['svg2png', '.svg']).path
34
34
  svg_asset.write_to(tmp_path)
35
- ::Sprockets::StaticAsset.new(self, svg_asset.logical_path + '.png', Pathname.new(tmp_path + '.png'))
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
+ def each_file(*args)
41
+ return to_enum(__method__) unless block_given?
42
+
43
+ super do |path|
44
+ yield path
45
+ if Svg.image?(path.to_s)
46
+ yield Pathname.new(path.to_s + '.png')
47
+ end
48
+ end
36
49
  end
37
50
 
38
51
  end
@@ -1,5 +1,5 @@
1
1
  module Sprockets
2
2
  module Svg
3
- VERSION = '0.0.4'
3
+ VERSION = '0.1.0'
4
4
  end
5
5
  end
data/lib/sprockets/svg.rb CHANGED
@@ -1,9 +1,19 @@
1
1
  require 'sprockets/svg/version'
2
2
 
3
+ require 'nokogiri'
4
+
3
5
  module Sprockets
4
6
  module Svg
5
7
  extend self
6
8
 
9
+ def self.image?(path)
10
+ return false unless path.ends_with?('.svg')
11
+
12
+ document = Nokogiri::XML(File.read(path))
13
+ svg = document.css('svg')
14
+ svg.attribute('height') && svg.attribute('width')
15
+ end
16
+
7
17
  def install(assets)
8
18
  assets.register_preprocessor 'image/svg+xml', :svg_min do |context, data|
9
19
  Sprockets::Svg::Cleaner.process(data)
@@ -20,5 +30,6 @@ require_relative 'svg/server'
20
30
  Sprockets::Asset.send(:include, Sprockets::Svg::Png)
21
31
  Sprockets::StaticAsset.send(:include, Sprockets::Svg::Png)
22
32
  Sprockets::Environment.send(:include, Sprockets::Svg::Server)
33
+ Sprockets::Index.send(:include, Sprockets::Svg::Server)
23
34
 
24
35
  require_relative 'svg/railtie' if defined?(Rails)
@@ -0,0 +1,2 @@
1
+ /log
2
+ /tmp
data/spec/app/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require File.expand_path('../config/application', __FILE__)
2
+ App::Application.load_tasks