Class: RubyBBCode::TagCollection

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

Overview

This class holds TagNode instances and helps converting them into code (using the provided template) when the time comes.

Instance Method Summary collapse

Instance Method Details

#to_bbcode(tags) ⇒ Object

Convert nodes to BBCode (with error information)



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

def to_bbcode(tags)
  to_code(tags, RubyBBCode::Templates::BBCodeErrorsTemplate)
end

#to_code(tags, template) ⇒ Object

This method is vulnerable to stack-level-too-deep scenarios where >=1,200 tags are being parsed. But that scenario can be mitigated by splitting up the tags. bbtree = { :nodes => [900tags, 1000tags] }, the work for that bbtree can be split up into two passes, do the each node one at a time. I’m not coding that though, it’s pointless, just a thought though



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/ruby-bbcode/tag_collection.rb', line 20

def to_code(tags, template)
  html_string = ""
  self.each do |node|
    if node.type == :tag
      t = template.new node

      t.inlay_between_text!

      if node.allow_params?
        t.inlay_params!
        t.remove_unused_tokens!
      end

      html_string << t.opening_part

      # invoke "recursive" call if this node contains child nodes
      html_string << node.children.to_code(tags, template) if node.has_children?      # FIXME:  Don't use recursion, it can lead to stack-level-too-deep errors for large volumes?

      t.inlay_closing_part!

      html_string << t.closing_part
    elsif node.type == :text
      html_string << template.convert_text(node)
    end
  end

  html_string
end

#to_html(tags) ⇒ Object

Convert nodes to HTML



8
9
10
# File 'lib/ruby-bbcode/tag_collection.rb', line 8

def to_html(tags)
  to_code(tags, RubyBBCode::Templates::HtmlTemplate)
end