Sha256: 330c4fda4d73173a84f73312657713f159ceeefd187821731758dcc56b4e32ab

Contents?: true

Size: 1.1 KB

Versions: 4

Compression:

Stored size: 1.1 KB

Contents

require 'uri'

module Sinatra
  module AssetPack
    module Css
      def self.preproc(source, assets)
        source.gsub(/url\((["']?)(.*?)(["']?)\)/) do |match|
          uri = URI.parse($2)

          # 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

      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

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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
sinatra-assetpack-0.3.2 lib/sinatra/assetpack/css.rb
sinatra-assetpack-0.3.1 lib/sinatra/assetpack/css.rb
sinatra-assetpack-0.3.0 lib/sinatra/assetpack/css.rb
sinatra-assetpack-0.2.8 lib/sinatra/assetpack/css.rb