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

params[R]

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]

parent[RW]

Node's parent in tree @return {Node}

Public Class Methods

new(**params) click to toggle source
# File lib/infoboxer/tree/node.rb, line 14
def initialize(**params)
  @params = params
end

Private Class Methods

coder() click to toggle source

Internal: HTML entities decoder.

# File lib/infoboxer/tree/node.rb, line 181
def coder
  @coder ||= HTMLEntities.new
end
def_readers(*keys) click to toggle source

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

==(other) click to toggle source
# File lib/infoboxer/tree/node.rb, line 34
def ==(other)
  self.class == other.class && _eq(other)
end
can_merge?(_other) click to toggle source

@private Used only during tree construction in {Parser}.

# File lib/infoboxer/tree/node.rb, line 69
def can_merge?(_other)
  false
end
children() click to toggle source

Node children list

# File lib/infoboxer/tree/node.rb, line 63
def children
  Nodes[] # redefined in descendants
end
empty?() click to toggle source

@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
first?() click to toggle source
# File lib/infoboxer/tree/node.rb, line 43
def first?
  index.zero?
end
index() click to toggle source

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
inspect() click to toggle source
# File lib/infoboxer/tree/node.rb, line 100
def inspect
  text.empty? ? "#<#{descr}>" : "#<#{descr}: #{shorten_text}>"
end
next_siblings() click to toggle source

List of siblings after this one

# File lib/infoboxer/tree/node.rb, line 58
def next_siblings
  siblings.select { |n| n.index > index }
end
prev_siblings() click to toggle source

List of siblings before this one

# File lib/infoboxer/tree/node.rb, line 53
def prev_siblings
  siblings.select { |n| n.index < index }
end
siblings() click to toggle source

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
text() click to toggle source

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
text_() click to toggle source

“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
to_s() click to toggle source

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
to_tree(level = 0) click to toggle source

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

_eq(_other) click to toggle source
# File lib/infoboxer/tree/node.rb, line 164
def _eq(_other)
  false
end
clean_class() click to toggle source
# File lib/infoboxer/tree/node.rb, line 144
def clean_class
  self.class.name.sub(/^.*::/, '')
end
decode(str) click to toggle source
# File lib/infoboxer/tree/node.rb, line 168
def decode(str)
  Node.coder.decode(str)
end
descr() click to toggle source
# 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
indent(level) click to toggle source
# File lib/infoboxer/tree/node.rb, line 160
def indent(level)
  '  ' * level
end
shorten_text() click to toggle source
# 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
show_params(prms = nil) click to toggle source
# File lib/infoboxer/tree/node.rb, line 156
def show_params(prms = nil)
  (prms || params).compact.map { |k, v| "#{k}: #{v.inspect}" }.join(', ')
end