Class: Roadie::Document

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

Overview

The main entry point for Roadie. A document represents a working unit and is built with the input HTML and the configuration options you need.

A Document must never be used from two threads at the same time. Reusing Documents is discouraged.

Stylesheets are added to the HTML from three different sources:

  1. Stylesheets inside the document ( <style> elements)

  2. Stylesheets referenced by the DOM ( <link> elements)

  3. The internal stylesheet (see #add_css)

The internal stylesheet is used last and gets the highest priority. The rest is used in the same order as browsers are supposed to use them.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(html) ⇒ Document

Returns a new instance of Document.

Parameters:

  • html (String)

    the input HTML



31
32
33
34
35
36
37
# File 'lib/roadie/document.rb', line 31

def initialize(html)
  @keep_uninlinable_css = true
  @html = html
  @asset_providers = ProviderList.wrap(FilesystemProvider.new)
  @external_asset_providers = ProviderList.empty
  @css = ""
end

Instance Attribute Details

#after_transformation#call

Callback to call just before #transformation is completed. Will be called with the current DOM tree and the Roadie::Document instance.

Returns:

  • (#call)

    the current value of after_transformation



18
19
20
# File 'lib/roadie/document.rb', line 18

def after_transformation
  @after_transformation
end

#asset_providersObject

Returns the value of attribute asset_providers.



19
20
21
# File 'lib/roadie/document.rb', line 19

def asset_providers
  @asset_providers
end

#before_transformation#call

Callback to call just before #transformation begins. Will be called with the parsed DOM tree and the Roadie::Document instance.

Returns:

  • (#call)

    the current value of before_transformation



18
19
20
# File 'lib/roadie/document.rb', line 18

def before_transformation
  @before_transformation
end

#external_asset_providersObject

Returns the value of attribute external_asset_providers.



19
20
21
# File 'lib/roadie/document.rb', line 19

def external_asset_providers
  @external_asset_providers
end

#htmlObject (readonly)

Returns the value of attribute html.



19
20
21
# File 'lib/roadie/document.rb', line 19

def html
  @html
end

#keep_uninlinable_cssObject

Should CSS that cannot be inlined be kept in a new ‘<style>` element in `<head>`?



28
29
30
# File 'lib/roadie/document.rb', line 28

def keep_uninlinable_css
  @keep_uninlinable_css
end

#url_optionsObject

URL options. If none are given no URL rewriting will take place.



23
24
25
# File 'lib/roadie/document.rb', line 23

def url_options
  @url_options
end

Instance Method Details

#add_css(new_css) ⇒ Object

Append additional CSS to the document’s internal stylesheet.

Parameters:

  • new_css (String)


41
42
43
# File 'lib/roadie/document.rb', line 41

def add_css(new_css)
  @css << "\n\n" << new_css
end

#transformString

Transform the input HTML and returns the processed HTML.

Before the transformation begins, the #before_transformation callback will be called with the parsed HTML tree and the Roadie::Document instance, and after all work is complete the #after_transformation callback will be invoked in the same way.

Most of the work is delegated to other classes. A list of them can be seen below.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/roadie/document.rb', line 59

def transform
  dom = Nokogiri::HTML.parse html

  callback before_transformation, dom

  improve dom
  inline dom
  rewrite_urls dom

  callback after_transformation, dom

  # #dup is called since it fixed a few segfaults in certain versions of Nokogiri
  dom.dup.to_html
end