module Sass::Script::Functions

Public Instance Methods

inline_svg(path, repl = nil) click to toggle source
# File lib/sass_inline_svg.rb, line 12
def inline_svg(path, repl = nil)
  assert_type path, :String
  path = path.value.strip()

  # Use Soprockets / Rails asset pipeline if in Rails context (and handle File not found):
  if defined?(Rails)
    asset = (Rails.application.assets || ::Sprockets::Railtie.build_environment(Rails.application)).find_asset(path).to_s
    raise "File not found or cannot be read (Sprockets): #{path}" if asset.nil?
    svg = asset.to_s
  else # otherwise read file:
    svg = _readFile(path).strip
  end

  if repl && repl.respond_to?('to_h')
    repl = repl.to_h
    svg = svg.to_s

    repl.each_pair do |k, v|

      if svg.include? k.value
        svg.gsub!(k.value, v.value)
      end
    end
  end

  encoded = CGI::escape(svg).gsub("+", "%20")
  encoded_url = "url('data:image/svg+xml;charset=utf-8," + encoded + "')"
  Sass::Script::String.new(encoded_url)
  
end
svg_inline(path, repl = nil) click to toggle source

Alias function to comply with old documentation

# File lib/sass_inline_svg.rb, line 8
def svg_inline(path, repl = nil)
  inline_svg(path, repl)
end

Private Instance Methods

_readFile(path) click to toggle source
# File lib/sass_inline_svg.rb, line 46
def _readFile(path)
  if File.readable?(path)
    File.open(path, 'rb') do |f|
      f.read
    end
  else
    raise Sass::SyntaxError, "File not found or cannot be read (native): #{path}"
  end
end