Sha256: ea83f1ea956fcf8b692856693dce6e69779933d0fbcdc52e7e6f5254884f3436

Contents?: true

Size: 812 Bytes

Versions: 6

Compression:

Stored size: 812 Bytes

Contents

module Sinatra
  module AssetPack
    module Css
      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

      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
    end
  end
end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
sinatra-assetpack-0.2.0 lib/sinatra/assetpack/css.rb
sinatra-assetpack-0.1.7 lib/sinatra/assetpack/css.rb
sundawg-sinatra-assetpack-fork-0.1.6 lib/sinatra/assetpack/css.rb
sinatra-assetpack-0.1.6 lib/sinatra/assetpack/css.rb
sinatra-assetpack-0.1.5 lib/sinatra/assetpack/css.rb
sinatra-assetpack-0.1.4 lib/sinatra/assetpack/css.rb