module Compass::SassExtensions::Functions::InlineImage

Public Instance Methods

inline_font_files(*args) click to toggle source
# File lib/compass/sass_extensions/functions/inline_image.rb, line 9
def inline_font_files(*args)
  raise Sass::SyntaxError, "An even number of arguments must be passed to font_files()" unless args.size % 2 == 0
  files = []
  while args.size > 0
    path = args.shift.value
    real_path = File.join(Compass.configuration.fonts_path, path)
    url = inline_image_string(data(real_path), compute_mime_type(path))
    files << "#{url} format('#{args.shift}')"
  end
  Sass::Script::String.new(files.join(", "))
end
inline_image(path, mime_type = nil) click to toggle source
# File lib/compass/sass_extensions/functions/inline_image.rb, line 3
def inline_image(path, mime_type = nil)
  path = path.value
  real_path = File.join(Compass.configuration.images_path, path)
  inline_image_string(data(real_path), compute_mime_type(path, mime_type))
end

Protected Instance Methods

inline_image_string(data, mime_type) click to toggle source
# File lib/compass/sass_extensions/functions/inline_image.rb, line 22
def inline_image_string(data, mime_type)
  data = [data].flatten.pack('m').gsub("\n","")
  url = "url('data:#{mime_type};base64,#{data}')"
  Sass::Script::String.new(url)
end

Private Instance Methods

compute_mime_type(path, mime_type = nil) click to toggle source
# File lib/compass/sass_extensions/functions/inline_image.rb, line 29
def compute_mime_type(path, mime_type = nil)
  return mime_type if mime_type
  case path
  when /\.png$/
    'image/png'
  when /\.jpe?g$/
    'image/jpeg'
  when /\.gif$/
    'image/gif'
  when /\.svg$/
    'image/svg+xml'
  when /\.otf$/
    'font/opentype'
  when /\.eot$/
    'application/vnd.ms-fontobject'
  when /\.ttf$/
    'font/truetype'
  when /\.woff$/
    'application/x-font-woff'
  when /\.off$/
    '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
data(real_path) click to toggle source
# File lib/compass/sass_extensions/functions/inline_image.rb, line 57
def data(real_path)
  if File.readable?(real_path)
    File.open(real_path, "rb") {|io| io.read}
  else
    raise Compass::Error, "File not found or cannot be read: #{real_path}"
  end
end