class RubyBBCode::BBTree

Tree of nodes containing the parsed BBCode information and the plain texts

As you parse a string of text, say:

"[b]I'm bold and the next word is [i]ITALIC[/i][b]"

…you build up a tree of nodes (@bbtree). The above string is represented by 4 nodes when parsing has completed.

Attributes

current_node[RW]
tags_list[RW]

Public Class Methods

new(hash = { nodes: TagCollection.new }) click to toggle source
# File lib/ruby-bbcode/bbtree.rb, line 15
def initialize(hash = { nodes: TagCollection.new })
  @bbtree = hash
  @current_node = TagNode.new(@bbtree)
  @tags_list = []
end

Public Instance Methods

build_up_new_tag(element) click to toggle source

Create a new node and adds it to the current node as a child node

# File lib/ruby-bbcode/bbtree.rb, line 69
def build_up_new_tag(element)
  @current_node.children << TagNode.new(element)
end
escalate_bbtree(element) click to toggle source

Advance to next level (the node we just added)

# File lib/ruby-bbcode/bbtree.rb, line 43
def escalate_bbtree(element)
  @current_node = TagNode.new(element)
  @tags_list.push @current_node
end
expecting_a_closing_tag?()
Alias for: within_open_tag?
nodes() click to toggle source
# File lib/ruby-bbcode/bbtree.rb, line 21
def nodes
  @bbtree[:nodes]
end
parent_has_constraints_on_children?() click to toggle source

Return true if the parent tag only allows certain child tags

# File lib/ruby-bbcode/bbtree.rb, line 38
def parent_has_constraints_on_children?
  parent_tag[:definition][:only_allow] != nil
end
parent_tag() click to toggle source

Returns the parent tag, if suitable/available

# File lib/ruby-bbcode/bbtree.rb, line 31
def parent_tag
  return nil unless within_open_tag?

  @tags_list.last
end
retrogress_bbtree() click to toggle source

Step down the bbtree a notch because we've reached a closing tag

# File lib/ruby-bbcode/bbtree.rb, line 49
def retrogress_bbtree
  if @tags_list[-1].definition[:self_closable]
    # It is possible that the next (self_closable) tag is on the next line
    # Remove newline of current tag and parent tag as they are (probably) not intented as an actual newline here but as tag separator
    @tags_list[-1][:nodes][0][:text]&.chomp!
    @tags_list[-2][:nodes][0][:text].chomp! unless (@tags_list.length < 2) || @tags_list[-2][:nodes][0][:text].nil?
  end

  @tags_list.pop # remove latest tag in tags_list since it's closed now...
  # The parsed data manifests in @bbtree.current_node.children << TagNode.new(element) which I think is more confusing than needed

  @current_node = if within_open_tag?
                    @tags_list[-1]
                  else
                    # we're still at the root of the BBTree or have returned back to the root via encountering closing tags...
                    TagNode.new(nodes: nodes)
                  end
end
to_bbcode(tags = {}) click to toggle source
# File lib/ruby-bbcode/bbtree.rb, line 77
def to_bbcode(tags = {})
  nodes.to_bbcode(tags)
end
to_html(tags = {}) click to toggle source
# File lib/ruby-bbcode/bbtree.rb, line 73
def to_html(tags = {})
  nodes.to_html(tags)
end
within_open_tag?() click to toggle source
# File lib/ruby-bbcode/bbtree.rb, line 25
def within_open_tag?
  !@tags_list.empty?
end
Also aliased as: expecting_a_closing_tag?