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 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
-
#current_node ⇒ Object
Returns the value of attribute current_node.
-
#tags_list ⇒ Object
Returns the value of attribute tags_list.
Instance Method Summary collapse
-
#build_up_new_tag(element) ⇒ Object
Create a new node and adds it to the current node as a child node.
-
#escalate_bbtree(element) ⇒ Object
Advance to next level (the node we just added).
-
#initialize(hash = { :nodes => TagCollection.new }) ⇒ BBTree
constructor
A new instance of BBTree.
- #nodes ⇒ Object
-
#parent_has_constraints_on_children? ⇒ Boolean
Return true if the parent tag only allows certain child tags.
-
#parent_tag ⇒ Object
Returns the parent tag, if suitable/available.
-
#retrogress_bbtree ⇒ Object
Step down the bbtree a notch because we’ve reached a closing tag.
- #to_bbcode(tags = {}) ⇒ Object
- #to_html(tags = {}) ⇒ Object
- #within_open_tag? ⇒ Boolean (also: #expecting_a_closing_tag?)
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_node ⇒ Object
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_list ⇒ Object
Returns the value of attribute tags_list.
13 14 15 |
# File 'lib/ruby-bbcode/bbtree.rb', line 13 def @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
67 68 69 |
# File 'lib/ruby-bbcode/bbtree.rb', line 67 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)
42 43 44 45 |
# File 'lib/ruby-bbcode/bbtree.rb', line 42 def escalate_bbtree(element) @current_node = TagNode.new(element) @tags_list.push @current_node end |
#nodes ⇒ Object
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
37 38 39 |
# File 'lib/ruby-bbcode/bbtree.rb', line 37 def parent_has_constraints_on_children? parent_tag[:definition][:only_allow] != nil end |
#parent_tag ⇒ Object
Returns the parent tag, if suitable/available
31 32 33 34 |
# File 'lib/ruby-bbcode/bbtree.rb', line 31 def parent_tag return nil unless within_open_tag? @tags_list.last end |
#retrogress_bbtree ⇒ Object
Step down the bbtree a notch because we’ve reached a closing tag
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/ruby-bbcode/bbtree.rb', line 48 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! unless @tags_list[-1][:nodes][0][:text].nil? @tags_list[-2][:nodes][0][:text].chomp! unless @tags_list.length < 2 or @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 if within_open_tag? @current_node = @tags_list[-1] 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 end |
#to_bbcode(tags = {}) ⇒ Object
75 76 77 |
# File 'lib/ruby-bbcode/bbtree.rb', line 75 def to_bbcode( = {}) self.nodes.to_bbcode() end |
#to_html(tags = {}) ⇒ Object
71 72 73 |
# File 'lib/ruby-bbcode/bbtree.rb', line 71 def to_html( = {}) self.nodes.to_html() end |
#within_open_tag? ⇒ Boolean Also known as: expecting_a_closing_tag?
25 26 27 |
# File 'lib/ruby-bbcode/bbtree.rb', line 25 def within_open_tag? @tags_list.length > 0 end |