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



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

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

Instance Attribute Details

#after_transformation#call

Callback to call just before #transformation is completed. Will be called with the current DOM tree.

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 is begun. Will be called with the parsed DOM tree.

Returns:

  • (#call)

    the current value of before_transformation



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

def before_transformation
  @before_transformation
end

#htmlObject (readonly)

Returns the value of attribute html.



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

def html
  @html
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)


36
37
38
# File 'lib/roadie/document.rb', line 36

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 after all work is complete the #after_transformation callback will be invoked.

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



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/roadie/document.rb', line 53

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