Class: Infoboxer::Tree::Node
- Inherits:
-
Object
- Object
- Infoboxer::Tree::Node
- 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
Instance Attribute Summary collapse
-
#params ⇒ Hash
readonly
Hash of node "params".
-
#parent ⇒ Node
Node's parent in tree.
Class Method Summary collapse
-
.coder ⇒ Object
Internal: HTML entities decoder.
-
.def_readers(*keys) ⇒ Object
Internal: descendandts DSL.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#children ⇒ Object
Node children list.
- #first? ⇒ Boolean
-
#index ⇒ Object
Position in parent's children array (zero-based).
-
#initialize(**params) ⇒ Node
constructor
A new instance of Node.
- #inspect ⇒ Object
-
#next_siblings ⇒ Object
List of siblings after this one.
-
#prev_siblings ⇒ Object
List of siblings before this one.
-
#siblings ⇒ Object
List of all sibling nodes (children of same parent).
-
#text ⇒ Object
Node text representation.
-
#text_ ⇒ Object
"Clean" version of node text: without trailing linefeeds, list markers and other things added for formatting.
-
#to_s ⇒ Object
See #text_.
-
#to_tree(level = 0) ⇒ Object
Textual representation of this node and its children, ready for pretty-printing.
Methods included from Navigation::Wikipath
Methods included from Navigation::Sections::Node
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.
12 13 14 |
# File 'lib/infoboxer/tree/node.rb', line 12 def initialize(**params) @params = params end |
Instance Attribute Details
#params ⇒ Hash (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).
26 27 28 |
# File 'lib/infoboxer/tree/node.rb', line 26 def params @params end |
#parent ⇒ Node
Node's parent in tree
30 31 32 |
# File 'lib/infoboxer/tree/node.rb', line 30 def parent @parent end |
Class Method Details
.coder ⇒ Object
Internal: HTML entities decoder.
179 180 181 |
# File 'lib/infoboxer/tree/node.rb', line 179 def coder @coder ||= HTMLEntities.new end |
.def_readers(*keys) ⇒ Object
Internal: descendandts DSL
172 173 174 175 176 |
# File 'lib/infoboxer/tree/node.rb', line 172 def def_readers(*keys) keys.each do |k| define_method(k) { params[k] } end end |
Instance Method Details
#==(other) ⇒ Object
32 33 34 |
# File 'lib/infoboxer/tree/node.rb', line 32 def ==(other) self.class == other.class && _eq(other) end |
#children ⇒ Object
Node children list
61 62 63 |
# File 'lib/infoboxer/tree/node.rb', line 61 def children Nodes[] # redefined in descendants end |
#first? ⇒ Boolean
41 42 43 |
# File 'lib/infoboxer/tree/node.rb', line 41 def first? index.zero? end |
#index ⇒ Object
Position in parent's children array (zero-based)
37 38 39 |
# File 'lib/infoboxer/tree/node.rb', line 37 def index parent ? parent.index_of(self) : 0 end |
#inspect ⇒ Object
98 99 100 |
# File 'lib/infoboxer/tree/node.rb', line 98 def inspect text.empty? ? "#<#{descr}>" : "#<#{descr}: #{shorten_text}>" end |
#next_siblings ⇒ Object
List of siblings after this one
56 57 58 |
# File 'lib/infoboxer/tree/node.rb', line 56 def next_siblings siblings.select { |n| n.index > index } end |
#prev_siblings ⇒ Object
List of siblings before this one
51 52 53 |
# File 'lib/infoboxer/tree/node.rb', line 51 def prev_siblings siblings.select { |n| n.index < index } end |
#siblings ⇒ Object
List of all sibling nodes (children of same parent)
46 47 48 |
# File 'lib/infoboxer/tree/node.rb', line 46 def siblings parent ? parent.children - [self] : Nodes[] end |
#text ⇒ Object
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.
116 117 118 |
# File 'lib/infoboxer/tree/node.rb', line 116 def text '' # redefined in descendants end |
#text_ ⇒ Object
"Clean" version of node text: without trailing linefeeds, list markers and other things added for formatting.
123 124 125 |
# File 'lib/infoboxer/tree/node.rb', line 123 def text_ text.strip end |
#to_s ⇒ Object
See #text_
128 129 130 131 |
# File 'lib/infoboxer/tree/node.rb', line 128 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
94 95 96 |
# File 'lib/infoboxer/tree/node.rb', line 94 def to_tree(level = 0) indent(level) + "<#{descr}>\n" end |