Module: Sinatra::AssetPack::Css

Defined in:
lib/sinatra/assetpack/css.rb

Class Method Summary collapse

Class Method Details

.preproc(str, assets) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/sinatra/assetpack/css.rb', line 4

def self.preproc(str, assets)
  str.gsub(/url\(["']?(.*?)["']?\)/) { |url|
    path = $1
    file, options = path.split('?')
    local = assets.local_file_for file

    url = if local
      if options.to_s.include?('embed')
        to_data_uri(local)
      else
        HtmlHelpers.get_file_uri(file, assets)
      end
    else
      path
    end

    "url(#{url})"
  }
end

.to_data_uri(file) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/sinatra/assetpack/css.rb', line 24

def self.to_data_uri(file)
  require 'base64'

  data = File.read(file)
  ext  = File.extname(file)
  mime = Sinatra::Base.mime_type(ext)
  b64  = Base64.encode64(data).gsub("\n", '')

  "data:#{mime};base64,#{b64}"
end