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\(
    (["']?)
    (
      [^(]*            # 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) ⇒ 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


30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/roadie/inliner.rb', line 30

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

  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)

46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/roadie/inliner.rb', line 46

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
    @document = nil
  end
end