Class: RubyBBCode::BBTree
- Inherits:
-
Object
- Object
- RubyBBCode::BBTree
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
#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_node ⇒ Object
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
|
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
|
#nodes ⇒ Object
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
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_tag ⇒ Object
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_text ⇒ Object
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_bbtree ⇒ Object
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
if within_open_tag?
@current_node = TagNode.new(self.nodes.last)
else
@current_node = TagNode.new({:nodes => self.nodes})
end
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
|
#type ⇒ Object
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?
40
41
42
|
# File 'lib/ruby-bbcode-to-md/bbtree.rb', line 40
def within_open_tag?
@tags_list.length > 0
end
|