Module: RbbCode::RecursiveConversion

Included in:
BlockquoteNode, ListItemNode, ListNode, ParagraphNode, TagNode
Defined in:
lib/rbbcode/node_extensions.rb

Instance Method Summary collapse

Instance Method Details

#recursively_convert(node, output_method, depth = 0) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rbbcode/node_extensions.rb', line 10

def recursively_convert(node, output_method, depth = 0)
  if node.terminal?
    if node.respond_to?(output_method)
      # This is a terminal node with a custom implementation of the output
      # method (e.g. #to_html).
      node.send(output_method)
    else
      # This is a terminal node without a custom implementation of the
      # output method. If the node consists solely of whitespace, emit the
      # empty string. Otherwise, emit the node's text value.
      node.text_value.match(/\A[\n\t]+\Z/) ? '' : node.text_value
    end
  else
    if node.respond_to?(output_method)
      # This is a non-terminal node with a custom implementation of the
      # output method.
      node.send(output_method)
    else
      # This is a non-terminal node without a custom implementation of the
      # output method. Convert all its child nodes and concatenate the results.
      node.elements.collect do |sub_node|
        recursively_convert(sub_node, output_method, depth + 1)
      end.join
    end
  end
end