Class: Infoboxer::Tree::Node

Inherits:
Object
  • Object
show all
Includes:
Navigation::Lookup::Node, Navigation::Sections::Node, Navigation::Shortcuts::Node, Navigation::Wikipath
Defined in:
lib/infoboxer/tree/node.rb,
lib/infoboxer/navigation.rb

Overview

This is the base class for all parse tree nodes.

Basically, you'll never create instances of this class or its descendants by yourself, you will receive it from tree and use for navigations.

Direct Known Subclasses

Compound, HR, HTMLClosingTag, HTMLOpeningTag, Image, Text

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Navigation::Wikipath

#wikipath

Methods included from Navigation::Sections::Node

#in_sections

Methods included from Navigation::Shortcuts::Node

#bold?, #categories, #external_links, #heading?, #headings, #images, #infobox, #infoboxes, #italic?, #lists, #paragraphs, #tables, #templates, #wikilinks

Methods included from Navigation::Lookup::Node

#_lookup, #_lookup_children, #_lookup_next_siblings, #_lookup_parents, #_lookup_prev_sibling, #_lookup_prev_siblings, #_lookup_siblings, #_matches?, #lookup, #lookup_children, #lookup_next_siblings, #lookup_parents, #lookup_prev_sibling, #lookup_prev_siblings, #lookup_siblings, #matches?, #parent?

Constructor Details

#initialize(**params) ⇒ Node

Returns a new instance of Node.



14
15
16
# File 'lib/infoboxer/tree/node.rb', line 14

def initialize(**params)
  @params = params
end

Instance Attribute Details

#paramsHash (readonly)

Hash of node "params".

Params notin is roughly the same as tag attributes in HTML. This is actual for complex nodes like images, tables, raw HTML tags and so on.

The most actual params are typically exposed by node as instance methods (like Heading#level).

Returns:

  • (Hash)


28
29
30
# File 'lib/infoboxer/tree/node.rb', line 28

def params
  @params
end

#parentNode

Node's parent in tree

Returns:



32
33
34
# File 'lib/infoboxer/tree/node.rb', line 32

def parent
  @parent
end

Class Method Details

.coderObject

Internal: HTML entities decoder.



181
182
183
# File 'lib/infoboxer/tree/node.rb', line 181

def coder
  @coder ||= HTMLEntities.new
end

.def_readers(*keys) ⇒ Object

Internal: descendandts DSL



174
175
176
177
178
# File 'lib/infoboxer/tree/node.rb', line 174

def def_readers(*keys)
  keys.each do |k|
    define_method(k) { params[k] }
  end
end

Instance Method Details

#==(other) ⇒ Object



34
35
36
# File 'lib/infoboxer/tree/node.rb', line 34

def ==(other)
  self.class == other.class && _eq(other)
end

#childrenObject

Node children list



63
64
65
# File 'lib/infoboxer/tree/node.rb', line 63

def children
  Nodes[] # redefined in descendants
end

#first?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/infoboxer/tree/node.rb', line 43

def first?
  index.zero?
end

#indexObject

Position in parent's children array (zero-based)



39
40
41
# File 'lib/infoboxer/tree/node.rb', line 39

def index
  parent ? parent.index_of(self) : 0
end

#inspectObject



100
101
102
# File 'lib/infoboxer/tree/node.rb', line 100

def inspect
  text.empty? ? "#<#{descr}>" : "#<#{descr}: #{shorten_text}>"
end

#next_siblingsObject

List of siblings after this one



58
59
60
# File 'lib/infoboxer/tree/node.rb', line 58

def next_siblings
  siblings.select { |n| n.index > index }
end

#prev_siblingsObject

List of siblings before this one



53
54
55
# File 'lib/infoboxer/tree/node.rb', line 53

def prev_siblings
  siblings.select { |n| n.index < index }
end

#siblingsObject

List of all sibling nodes (children of same parent)



48
49
50
# File 'lib/infoboxer/tree/node.rb', line 48

def siblings
  parent ? parent.children - [self] : Nodes[]
end

#textObject

Node text representation. It is defined for all nodes so, that entire Document#text produce readable text-only representation of Wiki page. Therefore, rules are those:

  • inline-formatting nodes (text, bold, italics) just return the text;
  • paragraph-level nodes (headings, paragraphs, lists) add "\n\n" after text;
  • list items add marker before text;
  • nodes, not belonging to "main" text flow (references, templates) produce empty text.

If you want just the text of some heading or list item (without "formatting" quircks), you can use #text_ method.



118
119
120
# File 'lib/infoboxer/tree/node.rb', line 118

def text
  '' # redefined in descendants
end

#text_Object

"Clean" version of node text: without trailing linefeeds, list markers and other things added for formatting.



125
126
127
# File 'lib/infoboxer/tree/node.rb', line 125

def text_
  text.strip
end

#to_sObject

See #text_



130
131
132
133
# File 'lib/infoboxer/tree/node.rb', line 130

def to_s
  # just aliases will not work when #text will be redefined in subclasses
  text_
end

#to_tree(level = 0) ⇒ Object

Textual representation of this node and its children, ready for pretty-printing. Use it like this:

puts page.lookup(:Paragraph).first.to_tree
# Prints something like
# <Paragraph>
#   This <Italic>
#   is <Text>
#   <Wikilink(link: "Argentina")>
#     pretty <Italic>
#     complicated <Text>

Useful for understanding page structure, and Infoboxer's representation of this structure



96
97
98
# File 'lib/infoboxer/tree/node.rb', line 96

def to_tree(level = 0)
  indent(level) + "<#{descr}>\n"
end