Sha256: c26c07a52598f1da722157f5dc448d9d013eb694d7c67575896147d672a1ba81

Contents?: true

Size: 1.59 KB

Versions: 25

Compression:

Stored size: 1.59 KB

Contents

require 'base64'
module Compass::SassExtensions::Functions::InlineImage

  def inline_image(path, mime_type = nil)
    path = path.value
    real_path = File.join(Compass.configuration.images_path, path)
    url = "url('data:#{compute_mime_type(path,mime_type)};base64,#{data(real_path)}')"
    Sass::Script::String.new(url)
  end

  def inline_font_files(*args)
    raise Sass::SyntaxError, "An even number of arguments must be passed to font_files()" unless args.size % 2 == 0
    path = path.value
    files = []
    while args.size > 0
      path = args.shift.value
      real_path = File.join(Compass.configuration.fonts_path, path)
      url = "url('data:#{compute_mime_type(path,mime_type)};base64,#{data(real_path)}')"
      files << "#{url} format('#{args.shift}')"
    end
    Sass::Script::String.new(files.join(", "))
  end

private
  def compute_mime_type(path, mime_type)
    return mime_type if mime_type
    case path
    when /\.png$/i
      'image/png'
    when /\.jpe?g$/i
      'image/jpeg'
    when /\.gif$/i
      'image/gif'
    when /\.otf$/i
      'font/opentype'
    when /\.ttf$/i
      'font/truetype'
    when /\.woff$/i
      'font/woff'
    when /\.off$/i
      'font/openfont'
    when /\.([a-zA-Z]+)$/
      "image/#{Regexp.last_match(1).downcase}"
    else
      raise Compass::Error, "A mime type could not be determined for #{path}, please specify one explicitly."
    end
  end

  def data(real_path)
    if File.readable?(real_path)
      Base64.encode64(File.read(real_path)).gsub("\n","")
    else
      raise Compass::Error, "File not found or cannot be read: #{real_path}"
    end
  end
end

Version data entries

25 entries across 24 versions & 3 rubygems

Version Path
compass-0.10.2 lib/compass/sass_extensions/functions/inline_image.rb
compass-0.10.1 lib/compass/sass_extensions/functions/inline_image.rb
compass-0.10.0 lib/compass/sass_extensions/functions/inline_image.rb
compass-0.10.0.rc6 lib/compass/sass_extensions/functions/inline_image.rb
compass-0.10.0.rc5 lib/compass/sass_extensions/functions/inline_image.rb
compass-0.10.0.rc4 lib/compass/sass_extensions/functions/inline_image.rb
compass-0.10.0.rc3 lib/compass/sass_extensions/functions/inline_image.rb
compass-0.10.0.rc2 lib/compass/sass_extensions/functions/inline_image.rb
compass-0.10.0.rc1 lib/compass/sass_extensions/functions/inline_image.rb
compass-0.10.0.pre9 lib/compass/sass_extensions/functions/inline_image.rb
compass-0.10.0.pre8 lib/compass/sass_extensions/functions/inline_image.rb
compass-0.10.0.pre7 lib/compass/sass_extensions/functions/inline_image.rb
compass-0.10.0.pre6 lib/compass/sass_extensions/functions/inline_image.rb
middleman-0.13.1 lib/middleman/vendor/gems/ruby/1.9.1/gems/compass-0.10.0.pre4/lib/compass/sass_extensions/functions/inline_image.rb
middleman-0.13.1 lib/middleman/vendor/gems/ruby/1.8/gems/compass-0.10.0.pre5/lib/compass/sass_extensions/functions/inline_image.rb
compass-0.10.0.pre5 lib/compass/sass_extensions/functions/inline_image.rb
compass-0.10.0.pre4 lib/compass/sass_extensions/functions/inline_image.rb
compass-0.10.0.pre3 lib/compass/sass_extensions/functions/inline_image.rb
middleman-0.12.2 lib/middleman/vendor/gems/gems/compass-0.10.0.pre2/lib/compass/sass_extensions/functions/inline_image.rb
middleman-0.12.1 lib/middleman/vendor/gems/gems/compass-0.10.0.pre2/lib/compass/sass_extensions/functions/inline_image.rb