Class: Roadie::Document
- Inherits:
-
Object
- Object
- Roadie::Document
- 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:
-
Stylesheets inside the document ( <style> elements)
-
Stylesheets referenced by the DOM ( <link> elements)
-
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
-
#after_transformation ⇒ #call
Callback to call just before #transformation is completed.
-
#asset_providers ⇒ Object
Returns the value of attribute asset_providers.
-
#before_transformation ⇒ #call
Callback to call just before #transformation is begun.
-
#html ⇒ Object
readonly
Returns the value of attribute html.
-
#url_options ⇒ Object
URL options.
Instance Method Summary collapse
-
#add_css(new_css) ⇒ Object
Append additional CSS to the document’s internal stylesheet.
-
#initialize(html) ⇒ Document
constructor
A new instance of Document.
-
#transform ⇒ String
Transform the input HTML and returns the processed HTML.
Constructor Details
#initialize(html) ⇒ Document
Returns a new instance of Document.
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.
18 19 20 |
# File 'lib/roadie/document.rb', line 18 def after_transformation @after_transformation end |
#asset_providers ⇒ Object
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.
18 19 20 |
# File 'lib/roadie/document.rb', line 18 def before_transformation @before_transformation end |
#html ⇒ Object (readonly)
Returns the value of attribute html.
19 20 21 |
# File 'lib/roadie/document.rb', line 19 def html @html end |
#url_options ⇒ Object
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 end |
Instance Method Details
#add_css(new_css) ⇒ Object
Append additional CSS to the document’s internal stylesheet.
36 37 38 |
# File 'lib/roadie/document.rb', line 36 def add_css(new_css) @css << "\n\n" << new_css end |
#transform ⇒ String
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 |