class Infoboxer::Tree::Node
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.
Constants
- MAX_CHARS
Attributes
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}).
@return [Hash]
Node's parent in tree @return {Node}
Public Class Methods
# File lib/infoboxer/tree/node.rb, line 14 def initialize(**params) @params = params end
Private Class Methods
Internal: HTML entities decoder.
# File lib/infoboxer/tree/node.rb, line 181 def coder @coder ||= HTMLEntities.new end
Internal: descendandts DSL
# File lib/infoboxer/tree/node.rb, line 174 def def_readers(*keys) keys.each do |k| define_method(k) { params[k] } end end
Public Instance Methods
# File lib/infoboxer/tree/node.rb, line 34 def ==(other) self.class == other.class && _eq(other) end
@private Used only during tree construction in {Parser}.
# File lib/infoboxer/tree/node.rb, line 69 def can_merge?(_other) false end
Node
children list
# File lib/infoboxer/tree/node.rb, line 63 def children Nodes[] # redefined in descendants end
@private Whether node is empty (definition of “empty” varies for different kinds of nodes). Used mainly in {Parser}.
# File lib/infoboxer/tree/node.rb, line 76 def empty? false end
# File lib/infoboxer/tree/node.rb, line 43 def first? index.zero? end
Position in parent's children array (zero-based)
# File lib/infoboxer/tree/node.rb, line 39 def index parent ? parent.index_of(self) : 0 end
# File lib/infoboxer/tree/node.rb, line 100 def inspect text.empty? ? "#<#{descr}>" : "#<#{descr}: #{shorten_text}>" end
List
of siblings after this one
# File lib/infoboxer/tree/node.rb, line 58 def next_siblings siblings.select { |n| n.index > index } end
List
of siblings before this one
# File lib/infoboxer/tree/node.rb, line 53 def prev_siblings siblings.select { |n| n.index < index } end
List
of all sibling nodes (children of same parent)
# File lib/infoboxer/tree/node.rb, line 48 def siblings parent ? parent.children - [self] : Nodes[] end
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 `“nn”` 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 {Node#text_} method.
# File lib/infoboxer/tree/node.rb, line 118 def text '' # redefined in descendants end
“Clean” version of node text: without trailing linefeeds, list markers and other things added for formatting.
# File lib/infoboxer/tree/node.rb, line 125 def text_ text.strip end
See {Node#text_}
# 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
Textual representation of this node and its children, ready for pretty-printing. Use it like this:
“`ruby 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
# File lib/infoboxer/tree/node.rb, line 96 def to_tree(level = 0) indent(level) + "<#{descr}>\n" end
Private Instance Methods
# File lib/infoboxer/tree/node.rb, line 164 def _eq(_other) false end
# File lib/infoboxer/tree/node.rb, line 144 def clean_class self.class.name.sub(/^.*::/, '') end
# File lib/infoboxer/tree/node.rb, line 168 def decode(str) Node.coder.decode(str) end
# File lib/infoboxer/tree/node.rb, line 148 def descr if !params || params.empty? clean_class.to_s else "#{clean_class}(#{show_params})" end end
# File lib/infoboxer/tree/node.rb, line 160 def indent(level) ' ' * level end
# File lib/infoboxer/tree/node.rb, line 139 def shorten_text txt = text_.sub(/^([^\n]+)\n.+$/m, '\1...') txt.length > MAX_CHARS ? txt[0..MAX_CHARS] + '...' : txt end
# File lib/infoboxer/tree/node.rb, line 156 def show_params(prms = nil) (prms || params).compact.map { |k, v| "#{k}: #{v.inspect}" }.join(', ') end