Class: RubyBBCode::BBTree

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-bbcode/bbtree.rb

Overview

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.

  • Node 1) An opening tag node representing “[b]”

  • Node 2) A text node representing “I’m bold and the next word is ”

  • Node 3) An opening tag node representing “[i]”

  • Node 4) A text node representing “ITALIC”

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = { nodes: TagCollection.new }) ⇒ BBTree

Returns a new instance of BBTree.



15
16
17
18
19
# File 'lib/ruby-bbcode/bbtree.rb', line 15

def initialize(hash = { nodes: TagCollection.new })
  @bbtree = hash
  @current_node = TagNode.new(@bbtree)
  @tags_list = []
end

Instance Attribute Details

#current_nodeObject

Returns the value of attribute current_node.



13
14
15
# File 'lib/ruby-bbcode/bbtree.rb', line 13

def current_node
  @current_node
end

#tags_listObject

Returns the value of attribute tags_list.



13
14
15
# File 'lib/ruby-bbcode/bbtree.rb', line 13

def tags_list
  @tags_list
end

Instance Method Details

#build_up_new_tag(element) ⇒ Object

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



69
70
71
# File 'lib/ruby-bbcode/bbtree.rb', line 69

def build_up_new_tag(element)
  @current_node.children << TagNode.new(element)
end

#escalate_bbtree(element) ⇒ Object

Advance to next level (the node we just added)



43
44
45
46
# File 'lib/ruby-bbcode/bbtree.rb', line 43

def escalate_bbtree(element)
  @current_node = TagNode.new(element)
  @tags_list.push @current_node
end

#nodesObject



21
22
23
# File 'lib/ruby-bbcode/bbtree.rb', line 21

def nodes
  @bbtree[:nodes]
end

#parent_has_constraints_on_children?Boolean

Return true if the parent tag only allows certain child tags

Returns:

  • (Boolean)


38
39
40
# File 'lib/ruby-bbcode/bbtree.rb', line 38

def parent_has_constraints_on_children?
  parent_tag[:definition][:only_allow] != nil
end

#parent_tagObject

Returns the parent tag, if suitable/available



31
32
33
34
35
# File 'lib/ruby-bbcode/bbtree.rb', line 31

def parent_tag
  return nil unless within_open_tag?

  @tags_list.last
end

#retrogress_bbtreeObject

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



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# 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 = {}) ⇒ Object



77
78
79
# File 'lib/ruby-bbcode/bbtree.rb', line 77

def to_bbcode(tags = {})
  nodes.to_bbcode(tags)
end

#to_html(tags = {}) ⇒ Object



73
74
75
# File 'lib/ruby-bbcode/bbtree.rb', line 73

def to_html(tags = {})
  nodes.to_html(tags)
end

#within_open_tag?Boolean Also known as: expecting_a_closing_tag?

Returns:

  • (Boolean)


25
26
27
# File 'lib/ruby-bbcode/bbtree.rb', line 25

def within_open_tag?
  !@tags_list.empty?
end