Sha256: d4c8251b40170489c3fea49b1a3ea3c43c30b9438cd413a1ea4407fa08d368a2

Contents?: true

Size: 1.02 KB

Versions: 3

Compression:

Stored size: 1.02 KB

Contents

require 'tempfile'
require 'pathname'

module Sprockets
  module Svg
    module Server

      def self.included(base)
        base.send(:alias_method, :find_asset_without_conversion, :find_asset)
        base.send(:alias_method, :find_asset, :find_asset_with_conversion)
      end

      def find_asset_with_conversion(path, options = {})
        convert = false
        if path.ends_with?('.svg.png')
          path = path.gsub(/\.png/, '')
          convert = true
        end
        asset = find_asset_without_conversion(path, options)

        if convert
          asset = svg_asset_to_static_png(asset)
        end

        asset
      end

      def svg2png_cache_path
        @cache_path ||= cache.instance_variable_get(:@root).join('svg2png')
      end

      def svg_asset_to_static_png(svg_asset)
        tmp_path = Tempfile.new(['svg2png', '.svg']).path
        svg_asset.write_to(tmp_path)
        ::Sprockets::StaticAsset.new(self, svg_asset.logical_path + '.png', Pathname.new(tmp_path + '.png'))
      end

    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
sprockets-svg-0.0.4 lib/sprockets/svg/server.rb
sprockets-svg-0.0.3 lib/sprockets/svg/server.rb
sprockets-svg-0.0.2 lib/sprockets/svg/server.rb