class RubyBBCode::TagCollection

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

Public Instance Methods

to_bbcode(tags) click to toggle source

Convert nodes to BBCode (with error information)

# File lib/ruby-bbcode/tag_collection.rb, line 13
def to_bbcode(tags)
  to_code(tags, RubyBBCode::Templates::BBCodeErrorsTemplate)
end
to_code(tags, template, parent_node = nil) click to toggle source

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

# File lib/ruby-bbcode/tag_collection.rb, line 20
def to_code(tags, template, parent_node = nil)
  output_string = ''
  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

      output_string << t.opening_part

      # invoke "recursive" call if this node contains child nodes
      output_string << node.children.to_code(tags, template, node) 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!

      output_string << t.closing_part
    elsif node.type == :text
      output_string << template.convert_text(node, parent_node)
    end
  end

  output_string
end
to_html(tags) click to toggle source

Convert nodes to HTML

# File lib/ruby-bbcode/tag_collection.rb, line 8
def to_html(tags)
  to_code(tags, RubyBBCode::Templates::HtmlTemplate)
end