Class: DocTree::DocTreeNode

Inherits:
Tree::TreeNode
  • Object
show all
Defined in:
lib/doctree.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#attrsObject

Returns the value of attribute attrs.



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

def attrs
  @attrs
end

#nameObject

Returns the value of attribute name.



11
12
13
# File 'lib/doctree.rb', line 11

def name
  @name
end

Class Method Details

.forHTML(html) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/doctree.rb', line 34

def self.forHTML(html)
  return DocTreeNode.new(html.title, html) if html.is_a? Nokogiri::XML::Document

  if html.name == 'a'
    node = DocTreeNode.new(html.text.strip, html)
    node.href = html["href"].to_s
    return node
  end

  raise "Unsupported node \"#{node.name}\", expected a Nokigiri XML/HTML document or <a> element"
end

Instance Method Details

#hrefObject



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

def href
  attrs[:href]
end

#href=(new_value) ⇒ Object



21
22
23
# File 'lib/doctree.rb', line 21

def href=(new_value)
  attrs[:href] = new_value
end

#map_html_tree(html, &block) ⇒ Object



46
47
48
49
# File 'lib/doctree.rb', line 46

def map_html_tree(html, &block)
  parent = if result = block.call(html) then self << result else self end
  html.children.each { |c| parent.map_html_tree(c, &block) }
end

#string_value(depth = 0, indent = "\t", &block) ⇒ Object



29
30
31
32
# File 'lib/doctree.rb', line 29

def string_value(depth = 0, indent = "\t", &block)
  childStr = self.children.count > 0 ? "\n" + children.map{ |node| node.string_value(depth + 1, &block) }.join("\n") : ""
  return [indent * depth, block.call(self), childStr].join("")
end

#to_sObject



25
26
27
# File 'lib/doctree.rb', line 25

def to_s
  self.string_value { |node| node.name + if node.attrs and !node.attrs.empty? then " -- " + node.attrs.to_json.gsub("\n", "") else "" end }
end