module Sprockets::Svg

Constants

USELESS_PNG_METADATA
VERSION

Public Class Methods

convert(svg_blob) click to toggle source

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

# 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')
  strip_png_metadata(image.to_blob)
end

Public Instance Methods

install(assets) click to toggle source
# 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) click to toggle source
# File lib/sprockets/svg.rb, line 20
def strip_png_metadata(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