Class: Roadie::Inliner

Inherits:
Object
  • Object
show all
Defined in:
lib/roadie/inliner.rb

Overview

This class is the core of Roadie as it does all the actual work. You just give it the CSS rules, the HTML and the url_options for rewriting URLs and let it go on doing all the heavy lifting and building.

Constant Summary collapse

CSS_URL_REGEXP =

Regexp matching all the url() declarations in CSS

It matches without any quotes and with both single and double quotes inside the parenthesis. There’s much room for improvement, of course.

%r{
  url\(
    (
      (?:["']|%22)?    # Optional opening quote
    )
    (
      [^(]*            # Text leading up to before opening parens
      (?:\([^)]*\))*   # Texts containing parens pairs
      [^(]+            # Texts without parens - required
    )
    \1                 # Closing quote
  \)
}x

Instance Method Summary collapse

Constructor Details

#initialize(assets, targets, html, url_options, after_inlining_handler = nil) ⇒ Inliner

Initialize a new Inliner with the given Provider, CSS targets, HTML, and ‘url_options`.

Parameters:

  • assets (AssetProvider)
  • targets (Array)

    List of CSS files to load via the provider

  • html (String)
  • url_options (Hash)

    Supported keys: :host, :port, :protocol

  • after_inlining_handler (lambda) (defaults to: nil)

    A lambda that accepts one parameter or an object that responds to the call method with one parameter.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/roadie/inliner.rb', line 33

def initialize(assets, targets, html, url_options, after_inlining_handler=nil)
  @assets = assets
  @css = assets.all(targets)
  @html = html
  @inline_css = []
  @url_options = url_options
  @after_inlining_handler = after_inlining_handler

  if url_options and url_options[:asset_path_prefix]
    raise ArgumentError, "The asset_path_prefix URL option is not working anymore. You need to add the following configuration to your application.rb:\n" +
                         "    config.roadie.provider = AssetPipelineProvider.new(#{url_options[:asset_path_prefix].inspect})\n" +
                         "Note that the prefix \"/assets\" is the default one, so you do not need to configure anything in that case."
  end
end

Instance Method Details

#executeString

Start the inlining and return the final HTML output

Returns:

  • (String)


50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/roadie/inliner.rb', line 50

def execute
  adjust_html do |document|
    @document = document
    add_missing_structure
    extract_link_elements
    extract_inline_style_elements
    inline_css_rules
    make_image_urls_absolute
    make_style_urls_absolute
    after_inlining_handler.call(document) if after_inlining_handler.respond_to?(:call)
    @document = nil
  end
end