Module: ExpressTemplates::Compiler

Included in:
ExpressTemplates
Defined in:
lib/express_templates/compiler.rb

Instance Method Summary collapse

Instance Method Details

#compile(template_or_src = nil, &block) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/express_templates/compiler.rb', line 3

def compile(template_or_src=nil, &block)
  template, src = _normalize(template_or_src)

  expander = Expander.new(template)

  Thread.current[:first_whitepace_removed] ||= 0
  Thread.current[:first_whitepace_removed] += 1
  begin
    compiled = expander.expand(src, &block).map(&:compile)
    compiled.first.sub!(/^"\n+/, '"') if Thread.current[:first_whitepace_removed].eql?(1)
    Thread.current[:first_whitepace_removed] -= 1
  ensure
    Thread.current[:first_whitepace_removed] = nil if Thread.current[:first_whitepace_removed].eql?(0)
  end

  return Interpolator.transform(compiled.join("+").gsub('"+"', '')).tap do |s|
    puts("\n"+template.inspect+"\nSource:\n#{template.try(:source)}\nInterpolated:\n#{s}\n") if ENV['DEBUG'].eql?('true')
  end
end