Module: Sinatra::AssetPack::Css

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

Class Method Summary collapse

Class Method Details

.build_data_uri(file) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/sinatra/assetpack/css.rb', line 39

def self.build_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

.build_url(assets, local, uri) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/sinatra/assetpack/css.rb', line 28

def self.build_url(assets, local, uri)
  if uri.query && uri.query.include?('embed')
    build_data_uri(local)
  else
    serve = URI(HtmlHelpers.get_file_uri(uri.path, assets))
    serve.query = uri.query
    serve.fragment = uri.fragment
    serve.to_s
  end
end

.preproc(source, assets) ⇒ Object



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

def self.preproc(source, assets)
  source.gsub(/url\((["']?)(.*?)(["']?)\)/) do |match|

    # Not parsable by URI.parse
    begin
      uri = URI.parse($2)
    rescue URI::InvalidURIError
      next match
    end

    # Not a valid complete url
    next match if uri.path.nil?

    # Not found in served assets
    local = assets.local_file_for(uri.path)
    next match if local.nil?

    asset_url = build_url(assets, local, uri)
    "url(#{$1}#{asset_url}#{$3})"
  end
end