class Emcee::Document
Document
is responsible for parsing HTML and handling interaction with the resulting document.
Constants
- ENCODING
Public Class Methods
new(data)
click to toggle source
# File lib/emcee/document.rb, line 10 def initialize(data) @doc = Nokogiri::HTML5.parse("<html><body>#{data}</body></html>") end
Public Instance Methods
html_imports()
click to toggle source
# File lib/emcee/document.rb, line 22 def html_imports wrap_nodes(@doc.css("link[rel='import']")) end
script_references()
click to toggle source
# File lib/emcee/document.rb, line 26 def script_references wrap_nodes(@doc.css("script[src]")) end
style_references()
click to toggle source
# File lib/emcee/document.rb, line 30 def style_references wrap_nodes(@doc.css("link[rel='stylesheet']")) end
to_s()
click to toggle source
# File lib/emcee/document.rb, line 14 def to_s # Make an html string. The parser does weird things with certain # attributes, so turn those nodes into xhtml. xhtml_nodes = nodes_with_selected_attribute + nodes_with_src_attribute html = htmlify_except(xhtml_nodes) unescape(html).encode(ENCODING) end
Private Instance Methods
htmlify_except(nodes)
click to toggle source
Generate an html string for the current document, but replace the provided nodes with their xhtml strings.
# File lib/emcee/document.rb, line 59 def htmlify_except(nodes) nodes.reduce(to_html) do |output, node| output.gsub(node.to_html, node.to_xhtml) end end
nodes_with_selected_attribute()
click to toggle source
# File lib/emcee/document.rb, line 41 def nodes_with_selected_attribute @doc.css("*[selected]") end
nodes_with_src_attribute()
click to toggle source
# File lib/emcee/document.rb, line 45 def nodes_with_src_attribute @doc.css("*[src]:not(script)") end
to_html()
click to toggle source
Get the html content of the document as a string.
# File lib/emcee/document.rb, line 37 def to_html @doc.at("body").children.to_html(encoding: ENCODING).lstrip end
unescape(content)
click to toggle source
# File lib/emcee/document.rb, line 53 def unescape(content) content.gsub("&", "&") end
wrap_nodes(nodes)
click to toggle source
# File lib/emcee/document.rb, line 49 def wrap_nodes(nodes) nodes.map { |node| Emcee::Node.new(node) } end