Class: Riml::Compiler::IfNodeVisitor

Inherits:
Visitor
  • Object
show all
Defined in:
lib/riml/compiler.rb

Instance Method Summary collapse

Methods inherited from Visitor

#initialize, #visit

Constructor Details

This class inherits a constructor from Riml::Compiler::Visitor

Instance Method Details

#compile(node) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/riml/compiler.rb', line 69

def compile(node)
  condition_visitor = visitor_for_node(node.condition)
  node.condition.parent_node = node
  node.condition.force_newline = true
  node.compiled_output = "if "
  node.compiled_output << "!" if UnlessNode === node

  node.condition.accept(condition_visitor)
  node.body.accept(NodesVisitor.new(:propagate_up_tree => false))

  node.body.compiled_output.each_line do |line|
    outdent = line =~ /\A(\s*)(else\s*|elseif .+)$/
    if outdent && node.non_nested?
      node.compiled_output << node.outdent + line
    else
      node.compiled_output << node.indent + line
    end
  end
  node.compiled_output << "\n" unless node.compiled_output[-1, 1] == "\n"
  node.compiled_output << "endif\n"
end