Class: Infoboxer::Tree::Node

Inherits:
Object
  • Object
show all
Includes:
Navigation::Lookup::Node, Navigation::Sections::Node, Navigation::Shortcuts::Node, ProcMe
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::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_siblings, #_lookup_siblings, #_matches?, #has_parent?, #lookup, #lookup_children, #lookup_next_siblings, #lookup_parents, #lookup_prev_siblings, #lookup_siblings, #matches?

Constructor Details

#initialize(params = {}) ⇒ Node

Returns a new instance of Node.



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

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)


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

def params
  @params
end

#parentNode

Node's parent in tree

Returns:



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

def parent
  @parent
end

Class Method Details

.coderObject

Internal: HTML entities decoder.



178
179
180
# File 'lib/infoboxer/tree/node.rb', line 178

def coder
  @coder ||= HTMLEntities.new
end

.def_readers(*keys) ⇒ Object

Internal: descendandts DSL



171
172
173
174
175
# File 'lib/infoboxer/tree/node.rb', line 171

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

Instance Method Details

#==(other) ⇒ Object



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

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

#childrenObject

Node children list



60
61
62
# File 'lib/infoboxer/tree/node.rb', line 60

def children
  Nodes[] # redefined in descendants
end

#indexObject

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



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

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

#inspectObject



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

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

#next_siblingsObject

List of siblings after this one



55
56
57
# File 'lib/infoboxer/tree/node.rb', line 55

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

#prev_siblingsObject

List of siblings before this one



50
51
52
# File 'lib/infoboxer/tree/node.rb', line 50

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

#siblingsObject

List of all sibling nodes (children of same parent)



45
46
47
# File 'lib/infoboxer/tree/node.rb', line 45

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.



115
116
117
# File 'lib/infoboxer/tree/node.rb', line 115

def text
  '' # redefined in descendants
end

#text_Object

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



122
123
124
# File 'lib/infoboxer/tree/node.rb', line 122

def text_
  text.strip
end

#to_sObject

See #text_



127
128
129
130
# File 'lib/infoboxer/tree/node.rb', line 127

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



93
94
95
# File 'lib/infoboxer/tree/node.rb', line 93

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