Sha256: 9ebd14e0f829b7ed6a8fdb9af8fdeb5a046d2541df22e67dd2d4188ba7450710

Contents?: true

Size: 826 Bytes

Versions: 2

Compression:

Stored size: 826 Bytes

Contents

require 'cgi'
require 'mime-types'

module RailsSassImages::Sass
  # Inline asset file to CSS by data-uri. Can be used for images and fonts.
  #
  #   .icon
  #     background: inline("icon.png")
  #
  #   @font-face
  #     font-family: "MyFont"
  #     src: inline("my.woff") format('woff')
  def inline(path)
    asset = RailsSassImages.asset(path)

    mimes = MIME::Types.type_for(asset.to_s)
    raise "Unknown MIME-type for #{ asset.to_s }" unless mimes.first

    mime = mimes.first.content_type
    file = asset.read

    if mime == 'image/svg+xml'
      file     = CGI::escape(file).gsub('+', '%20')
      encoding = 'charset=utf-8'
    else
      file     = [file].flatten.pack('m').gsub("\n", '')
      encoding = 'base64'
    end

    Sass::Script::String.new("url('data:#{mime};#{encoding},#{file}')")
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rails-sass-images-1.0.3 lib/rails-sass-images/sass/inline.rb
rails-sass-images-1.0.2 lib/rails-sass-images/sass/inline.rb