Class: WithEmbeddedAssets::Processor

Inherits:
Sprockets::Processor
  • Object
show all
Defined in:
lib/with_embedded_assets/processor.rb

Overview

CSS asset processor that embeds images and other files in the code. It subclass Sprockets::Processor to achieve this, as a registered Sprockets post-processor.

The processor registration on Sprockets is handle by a railtie, on WithEmbeddedAssets::Railtie.

This class was written using parts of the embed-assets-rails gem. Please read the file under license/embed-assets-rails for the original license. If you plan to use or modify it, and then distribute it, please include the license file together in the software bundle.

The original source can be found at: github.com/saulabs/embed-assets-rails

Instance Method Summary collapse

Instance Method Details

#evaluate(context, locals, &block) ⇒ Object

Method called by Sprockets to post-process the CSS code.

See the original implementation here.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/with_embedded_assets/processor.rb', line 26

def evaluate(context, locals, &block)
  # Use this variable as cache to avoid reprocessing the same asset during
  # this run.
  @encoded_asset_contents = {}

  return data unless WithEmbeddedAssets.enabled?

  # process each CSS asset
  data.gsub(EMBED_DETECTOR) do |url|
    asset = Rails.application.assets[File.basename $1]
    if embeddable?(asset)
      encoded_body = encoded_contents(asset)
      "url(\"data:#{mime_type($1)};charset=utf-8;base64,#{encoded_body}\")"
    else
      "url(#{$1})"
    end
  end
end