Module: Sass::Script::Functions

Defined in:
lib/ustyle/sass_functions.rb

Instance Method Summary collapse

Instance Method Details

#base64encode(string) ⇒ Object



36
37
38
39
# File 'lib/ustyle/sass_functions.rb', line 36

def base64encode(string)
  assert_type string, :String
  Sass::Script::String.new(Base64.strict_encode64(string.value))
end

#inline_asset(source) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/ustyle/sass_functions.rb', line 13

def inline_asset(source)
  assert_type source, :String
  if Ustyle.sprockets?
    ::Sass::Script::String.new "url(#{sprockets_context.asset_data_uri(source.value)})"
  else
    path = File.join(::Ustyle.assets_path, "images", source.value)
    asset_data_uri(path)
  end
end

#inline_svg(source) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/ustyle/sass_functions.rb', line 24

def inline_svg(source)
  assert_type source, :String
  if Ustyle.sprockets?
    svg = escaped_svg(sprockets_context.environment.find_asset(source.value).to_s)
    ::Sass::Script::String.new "url(data:#{Ustyle.mime_type_for(source.value)};charset=utf-8,#{svg})"
  else
    path = File.join(::Ustyle.assets_path, "images", source.value)
    asset_data_uri(path, svg = true)
  end
end

#rgba_inline(c, px = 5) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/ustyle/sass_functions.rb', line 60

def rgba_inline(c, px = 5)
  color = ChunkyPNG::Color.rgba(c.red, c.green, c.blue, (c.alpha * 100 * 2.55).round)
  image = ChunkyPNG::Image.new(px.to_i, px.to_i, color)
  data  = Base64.encode64(image.to_blob).gsub("\n", "")

  Sass::Script::String.new("url('data:image/png;base64,#{data}')")
end

#ustyle_asset_path(source, type) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ustyle/sass_functions.rb', line 47

def ustyle_asset_path(source, type)
  url = if Ustyle.sprockets?
    sprockets_context.send(:"#{type}_path", source.value)
  else
    Ustyle.cloudfront_url(source.value, type)
  end

  # sass-only
  url ||= source.value.gsub('"', '')
  Sass::Script::String.new(url, :string)
end

#ustyle_image_path(source) ⇒ Object



42
43
44
# File 'lib/ustyle/sass_functions.rb', line 42

def ustyle_image_path(source)
  ustyle_asset_path source, :image
end

#ustyle_versionObject



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

def ustyle_version
  ::Sass::Script::String.new ::Ustyle::VERSION
end