Class: RubyBBCode::BBTree
- Inherits:
-
Object
- Object
- RubyBBCode::BBTree
- 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 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 “ITALIC”
The closing of the nodes seems to be implied which is fine by me –less to keep track of.
Instance Attribute Summary collapse
-
#current_node ⇒ Object
Returns the value of attribute current_node.
-
#tags_list ⇒ Object
Returns the value of attribute tags_list.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #build_up_new_tag(element) ⇒ Object
-
#escalate_bbtree(element) ⇒ Object
Advance to next level (the node we just added).
-
#initialize(hash = { :nodes => TagCollection.new }, dictionary) ⇒ BBTree
constructor
A new instance of BBTree.
- #nodes ⇒ Object (also: #children)
- #parent_has_constraints_on_children? ⇒ Boolean
- #parent_tag ⇒ Object
- #redefine_parent_tag_as_text ⇒ Object
-
#retrogress_bbtree ⇒ Object
Step down the bbtree a notch because we’ve reached a closing tag.
- #to_html(tags = {}) ⇒ Object
- #type ⇒ Object
- #within_open_tag? ⇒ Boolean (also: #expecting_a_closing_tag?)
Constructor Details
#initialize(hash = { :nodes => TagCollection.new }, dictionary) ⇒ BBTree
17 18 19 20 21 22 |
# File 'lib/ruby-bbcode/bbtree.rb', line 17 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.
15 16 17 |
# File 'lib/ruby-bbcode/bbtree.rb', line 15 def current_node @current_node end |
#tags_list ⇒ Object
Returns the value of attribute tags_list.
15 16 17 |
# File 'lib/ruby-bbcode/bbtree.rb', line 15 def @tags_list end |
Instance Method Details
#[](key) ⇒ Object
24 25 26 |
# File 'lib/ruby-bbcode/bbtree.rb', line 24 def [](key) @bbtree[key] end |
#[]=(key, value) ⇒ Object
28 29 30 |
# File 'lib/ruby-bbcode/bbtree.rb', line 28 def []=(key, value) @bbtree[key] = value end |
#build_up_new_tag(element) ⇒ Object
85 86 87 |
# File 'lib/ruby-bbcode/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)
56 57 58 59 |
# File 'lib/ruby-bbcode/bbtree.rb', line 56 def escalate_bbtree(element) @tags_list.push element[:tag] @current_node = TagNode.new(element) end |
#nodes ⇒ Object Also known as: children
32 33 34 |
# File 'lib/ruby-bbcode/bbtree.rb', line 32 def nodes @bbtree[:nodes] end |
#parent_has_constraints_on_children? ⇒ Boolean
51 52 53 |
# File 'lib/ruby-bbcode/bbtree.rb', line 51 def parent_has_constraints_on_children? @dictionary[parent_tag][:only_allow] != nil end |
#parent_tag ⇒ Object
46 47 48 49 |
# File 'lib/ruby-bbcode/bbtree.rb', line 46 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/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/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}) # Note: just passing in self works too... 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/bbtree.rb', line 89 def to_html( = {}) self.nodes.to_html() end |
#type ⇒ Object
37 38 39 |
# File 'lib/ruby-bbcode/bbtree.rb', line 37 def type :bbtree end |
#within_open_tag? ⇒ Boolean Also known as: expecting_a_closing_tag?
41 42 43 |
# File 'lib/ruby-bbcode/bbtree.rb', line 41 def within_open_tag? @tags_list.length > 0 end |