Module: Sass::Script::Functions

Defined in:
lib/sass_inline_svg.rb

Instance Method Summary collapse

Instance Method Details

#inline_svg(path, repl = nil) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# 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.find_asset(path)
    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) ⇒ Object

Alias function to comply with old documentation



8
9
10
# File 'lib/sass_inline_svg.rb', line 8

def svg_inline(path, repl = nil)
  inline_svg(path, repl)
end