Class: RubyBBCode::BBTree

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

Overview

As you parse a string of text, say:

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

…you build up a tree of nodes (@bbtree). The above string converts to 4 nodes when the parse 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 “ITALLICS”

The closing of the nodes seems to be implied which is fine by me –less to keep track of.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DebugBBTree

#count_child_nodes, #to_s, #to_v

Constructor Details

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

Returns a new instance of BBTree.



16
17
18
19
20
21
# File 'lib/ruby-bbcode-to-md/bbtree.rb', line 16

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

Instance Attribute Details

#current_nodeObject

Returns the value of attribute current_node.



14
15
16
# File 'lib/ruby-bbcode-to-md/bbtree.rb', line 14

def current_node
  @current_node
end

#tags_listObject

Returns the value of attribute tags_list.



14
15
16
# File 'lib/ruby-bbcode-to-md/bbtree.rb', line 14

def tags_list
  @tags_list
end

Instance Method Details

#[](key) ⇒ Object



23
24
25
# File 'lib/ruby-bbcode-to-md/bbtree.rb', line 23

def [](key)
  @bbtree[key]
end

#[]=(key, value) ⇒ Object



27
28
29
# File 'lib/ruby-bbcode-to-md/bbtree.rb', line 27

def []=(key, value)
  @bbtree[key] = value
end

#build_up_new_tag(element) ⇒ Object



85
86
87
# File 'lib/ruby-bbcode-to-md/bbtree.rb', line 85

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)



55
56
57
58
59
# File 'lib/ruby-bbcode-to-md/bbtree.rb', line 55

def escalate_bbtree(element)
  element[:parent_tag] = parent_tag
  @tags_list.push element[:tag]
  @current_node = TagNode.new(element)
end

#nodesObject Also known as: children



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

def nodes
  @bbtree[:nodes]
end

#parent_has_constraints_on_children?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/ruby-bbcode-to-md/bbtree.rb', line 50

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

#parent_tagObject



45
46
47
48
# File 'lib/ruby-bbcode-to-md/bbtree.rb', line 45

def parent_tag
  return nil if !within_open_tag?
  @tags_list.last.to_sym
end

#redefine_parent_tag_as_textObject



78
79
80
81
82
83
# File 'lib/ruby-bbcode-to-md/bbtree.rb', line 78

def redefine_parent_tag_as_text
  @tags_list.pop
  @current_node[:is_tag] = false
  @current_node[:closing_tag] = false
  @current_node.element[:text] = "[#{@current_node[:tag].to_s}]"
end

#retrogress_bbtreeObject

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



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/ruby-bbcode-to-md/bbtree.rb', line 62

def retrogress_bbtree
  @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

  if within_open_tag?
    # Set the current node to be the node we've just parsed over which is infact within another node??...
    @current_node = TagNode.new(self.nodes.last)
  else # If we're still at the root of the BBTree or have returned back to the root via encountring closing tags...
    @current_node = TagNode.new({:nodes => self.nodes}) 
  end
  
  # OKOKOK!  
  # Since @bbtree = @current_node, if we ever set @current_node to something, we're actually changing @bbtree...
  # therefore... my brain is now numb
end

#to_html(tags = {}) ⇒ Object



89
90
91
# File 'lib/ruby-bbcode-to-md/bbtree.rb', line 89

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

#typeObject



36
37
38
# File 'lib/ruby-bbcode-to-md/bbtree.rb', line 36

def type
  :bbtree
end

#within_open_tag?Boolean Also known as: expecting_a_closing_tag?

Returns:

  • (Boolean)


40
41
42
# File 'lib/ruby-bbcode-to-md/bbtree.rb', line 40

def within_open_tag?
  @tags_list.length > 0
end