Class: Infoboxer::Tree::Node
- Inherits:
-
Object
- Object
- Infoboxer::Tree::Node
- 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
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.
-
#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::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_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
#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).
29 30 31 |
# File 'lib/infoboxer/tree/node.rb', line 29 def params @params end |
#parent ⇒ Node
Node's parent in tree
33 34 35 |
# File 'lib/infoboxer/tree/node.rb', line 33 def parent @parent end |
Class Method Details
.coder ⇒ Object
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 |
#children ⇒ Object
Node children list
60 61 62 |
# File 'lib/infoboxer/tree/node.rb', line 60 def children Nodes[] # redefined in descendants end |
#index ⇒ Object
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 |
#inspect ⇒ Object
97 98 99 |
# File 'lib/infoboxer/tree/node.rb', line 97 def inspect text.empty? ? "#<#{descr}>" : "#<#{descr}: #{shorten_text}>" end |
#next_siblings ⇒ Object
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_siblings ⇒ Object
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 |
#siblings ⇒ Object
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 |
#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.
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_s ⇒ Object
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 |