Module: Sprockets::Svg

Extended by:
Svg
Included in:
Svg
Defined in:
lib/sprockets/svg.rb,
lib/sprockets/svg/cleaner.rb,
lib/sprockets/svg/version.rb

Defined Under Namespace

Classes: Cleaner

Constant Summary collapse

USELESS_PNG_METADATA =
%w(svg:base-uri date:create date:modify).freeze
VERSION =
'1.3.0'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.convert(svg_blob) ⇒ Object

TODO: integrate svgo instead: github.com/svg/svgo See github.com/lautis/uglifier on how to integrate a npm package as a gem.



13
14
15
16
17
18
# File 'lib/sprockets/svg.rb', line 13

def self.convert(svg_blob)
  stream = StringIO.new(svg_blob)
  image = MiniMagick::Image.create('.svg', false) { |file| IO.copy_stream(stream, file) }
  image.format('png')
  (image.to_blob)
end

Instance Method Details

#install(assets) ⇒ Object



33
34
35
36
37
38
# File 'lib/sprockets/svg.rb', line 33

def install(assets)
  assets.register_preprocessor 'image/svg+xml', Sprockets::Svg::Cleaner
  assets.register_transformer 'image/svg+xml', 'image/png', -> (input) {
    Sprockets::Svg.convert(input[:data])
  }
end

#strip_png_metadata(png_blob) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/sprockets/svg.rb', line 20

def (png_blob)
  image = ChunkyPNG::Datastream.from_blob(png_blob)

  image.other_chunks.reject! do |chunk|
    chunk.type == "tIME" ||
      (chunk.respond_to?(:keyword) && USELESS_PNG_METADATA.include?(chunk.keyword))
  end

  str = StringIO.new
  image.write(str)
  str.string
end