Class: Mato::Document

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fragment) ⇒ Document

Returns a new instance of Document.

Parameters:

  • fragment (Nokogiri::HTML4::DocumentFragment)


17
18
19
# File 'lib/mato/document.rb', line 17

def initialize(fragment)
  @fragment = fragment
end

Instance Attribute Details

#fragmentNokogiri::HTML4::DocumentFragment (readonly)

Returns:

  • (Nokogiri::HTML4::DocumentFragment)


10
11
12
# File 'lib/mato/document.rb', line 10

def fragment
  @fragment
end

Class Method Details

.emptyObject



12
13
14
# File 'lib/mato/document.rb', line 12

def self.empty
  new(Nokogiri::HTML4.fragment(''))
end

Instance Method Details

#apply_html_filters(*html_filters) ⇒ Nokogiri::HTML4::DocumentFragment

Returns A copy of fragment that are modified by html_filters.

Returns:

  • (Nokogiri::HTML4::DocumentFragment)

    A copy of fragment that are modified by html_filters



22
23
24
25
26
27
28
# File 'lib/mato/document.rb', line 22

def apply_html_filters(*html_filters)
  new_fragment = fragment.dup
  html_filters.each do |html_filter|
    html_filter.call(new_fragment)
  end
  self.class.new(new_fragment.freeze)
end

#css(selector) ⇒ Nokogiri::XML::NodeSet

Parameters:

  • selector (String)

Returns:

  • (Nokogiri::XML::NodeSet)


32
33
34
# File 'lib/mato/document.rb', line 32

def css(selector)
  fragment.css(selector)
end

#marshal_dumpObject



54
55
56
57
58
# File 'lib/mato/document.rb', line 54

def marshal_dump
  {
    fragment: fragment.to_html(save_with: 0),
  }
end

#marshal_load(data) ⇒ Object



60
61
62
# File 'lib/mato/document.rb', line 60

def marshal_load(data)
  initialize(Nokogiri::HTML4.fragment(data[:fragment]).freeze)
end

#render(renderer) ⇒ Object



42
43
44
# File 'lib/mato/document.rb', line 42

def render(renderer)
  renderer.call(fragment)
end

#render_htmlObject



46
47
48
# File 'lib/mato/document.rb', line 46

def render_html
  render(Mato::Renderers::HtmlRenderer.new)
end

#render_html_tocObject



50
51
52
# File 'lib/mato/document.rb', line 50

def render_html_toc
  render(Mato::Renderers::HtmlTocRenderer.new)
end

#xpath(query) ⇒ Nokogiri::XML::NodeSet

Parameters:

  • query (String)

Returns:

  • (Nokogiri::XML::NodeSet)


38
39
40
# File 'lib/mato/document.rb', line 38

def xpath(query)
  fragment.xpath(query)
end