class Roadie::Document
@attr [#call] after_transformation Callback to call just before {#transform}ation is completed. Will be called with the current DOM tree and the {Document} instance.
@attr [#call] before_transformation Callback to call just before {#transform}ation begins. Will be called with the parsed DOM tree and the {Document} instance.
The execution methods are {#transform} and {#transform_partial}.
rest is used in the same order as browsers are supposed to use them.
The internal stylesheet is used last and gets the highest priority. The
3. The internal stylesheet (see {#add_css})
2. Stylesheets referenced by the DOM ( +<link>+ elements)
1. Stylesheets inside the document ( +<style>+ elements)
Stylesheets are added to the HTML from three different sources:
Documents is discouraged.
A Document must never be used from two threads at the same time. Reusing
is built with the input HTML and the configuration options you need.
The main entry point for Roadie. A document represents a working unit and
def add_css(new_css)
-
new_css
(String
) --
def add_css(new_css) @css << "\n\n" << new_css end
def asset_providers=(list)
def asset_providers=(list) @asset_providers = ProviderList.wrap(list) end
def callback(callable, dom)
def callback(callable, dom) if callable.respond_to?(:call) callable.call(dom, self) end end
def external_asset_providers=(list)
def external_asset_providers=(list) @external_asset_providers = ProviderList.wrap(list) end
def improve(dom)
def improve(dom) MarkupImprover.new(dom, html).improve end
def initialize(html)
-
html
(String
) -- the input HTML
def initialize(html) @keep_uninlinable_css = true @merge_media_queries = true @serialization_options = Nokogiri::XML::Node::SaveOptions::NO_DECLARATION | Nokogiri::XML::Node::SaveOptions::NO_EMPTY_TAGS @html = html @asset_providers = ProviderList.wrap(FilesystemProvider.new) @external_asset_providers = ProviderList.empty @css = +"" @mode = :html end
def inline(dom, options = {})
def inline(dom, options = {}) keep_uninlinable_in = options.fetch(:keep_uninlinable_in) dom_stylesheets = AssetScanner.new(dom, asset_providers, external_asset_providers).extract_css Inliner.new(dom_stylesheets + [stylesheet], dom).inline( keep_uninlinable_css: keep_uninlinable_css, keep_uninlinable_in: keep_uninlinable_in, merge_media_queries: merge_media_queries ) end
def make_url_rewriter
def make_url_rewriter if url_options UrlRewriter.new(UrlGenerator.new(url_options)) else NullUrlRewriter.new end end
def mode=(mode)
`:xhtml`
`:html` (default)
Valid modes:
Change the mode. The mode affects how the resulting markup is generated.
def mode=(mode) if VALID_MODES.include?(mode) @mode = mode else raise ArgumentError, "Invalid mode #{mode.inspect}. Valid modes are: #{VALID_MODES.inspect}" end end
def remove_ignore_markers(dom)
def remove_ignore_markers(dom) dom.css("[data-roadie-ignore]").each do |node| node.remove_attribute "data-roadie-ignore" end end
def rewrite_urls(dom)
def rewrite_urls(dom) make_url_rewriter.transform_dom(dom) end
def serialization_options=(options)
For the complete set of available options look into +Nokogiri::XML::Node::SaveOptions+.
Integer representing a bitmap set of options used by Nokogiri during serialization.
def serialization_options=(options) @serialization_options = options || 0 end
def serialize_document(dom)
def serialize_document(dom) # #dup is called since it fixed a few segfaults in certain versions of Nokogiri save_options = Nokogiri::XML::Node::SaveOptions format = { html: save_options::AS_HTML, xhtml: save_options::AS_XHTML, xml: save_options::AS_XML }.fetch(mode) dom.dup.to_html(save_with: (serialization_options | format)) end
def stylesheet
def stylesheet Stylesheet.new "(Document styles)", @css end
def transform
-
(String)
- the transformed HTML
Other tags:
- See: #transform_partial - Transforms partial documents (fragments)
See: UrlRewriter - UrlRewriter (rewrites URLs and makes them absolute)
See: Inliner - Inliner (inlines the stylesheets)
See: MarkupImprover - MarkupImprover (improves the markup of the DOM)
def transform dom = Nokogiri::HTML.parse html callback before_transformation, dom improve dom inline dom, keep_uninlinable_in: :head rewrite_urls dom callback after_transformation, dom remove_ignore_markers dom serialize_document dom end
def transform_partial
-
(String)
- the transformed HTML
Other tags:
- See: #transform - Transforms full documents
See: UrlRewriter - UrlRewriter (rewrites URLs and makes them absolute)
See: Inliner - Inliner (inlines the stylesheets)
def transform_partial dom = Nokogiri::HTML.fragment html callback before_transformation, dom inline dom, keep_uninlinable_in: :root rewrite_urls dom callback after_transformation, dom serialize_document dom end