Class: Hamlit::HamlCompiler

Inherits:
Object show all
Includes:
HamlUtil
Defined in:
lib/hamlit/parser/haml_compiler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from HamlUtil

#balance, #check_encoding, #check_haml_encoding, #contains_interpolation?, #handle_interpolation, #html_safe, #human_indentation, #inspect_obj, #rails_xss_safe?, #silence_warnings, #slow_unescape_interpolation, #unescape_interpolation

Constructor Details

#initialize(options) ⇒ HamlCompiler

Returns a new instance of HamlCompiler.



10
11
12
13
14
15
16
# File 'lib/hamlit/parser/haml_compiler.rb', line 10

def initialize(options)
  @options     = options
  @output_tabs = 0
  @to_merge    = []
  @precompiled = ''
  @node        = nil
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



8
9
10
# File 'lib/hamlit/parser/haml_compiler.rb', line 8

def options
  @options
end

Instance Method Details

#compile(node) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/hamlit/parser/haml_compiler.rb', line 18

def compile(node)
  parent, @node = @node, node
  if node.children.empty?
    send(:"compile_#{node.type}")
  else
    send(:"compile_#{node.type}") {node.children.each {|c| compile c}}
  end
ensure
  @node = parent
end

#precompiledString

The source code that is evaluated to produce the Haml document.

This is automatically converted to the correct encoding (see the ‘:encoding` option).

Returns:

  • (String)


35
36
37
38
39
# File 'lib/hamlit/parser/haml_compiler.rb', line 35

def precompiled
  encoding = Encoding.find(@options.encoding)
  return @precompiled.force_encoding(encoding) if encoding == Encoding::ASCII_8BIT
  return @precompiled.encode(encoding)
end

#precompiled_with_ambles(local_names) ⇒ Object

Returns the precompiled string with the preamble and postamble.

Initializes to ActionView::OutputBuffer when available; this is necessary to avoid ordering issues with partial layouts in Rails. If not available, initializes to nil.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/hamlit/parser/haml_compiler.rb', line 50

def precompiled_with_ambles(local_names)
  preamble = <<END.tr!("\n", ';')
begin
extend ::Hamlit::HamlHelpers
_hamlout = @haml_buffer = ::Hamlit::HamlBuffer.new(haml_buffer, #{options.for_buffer.inspect})
_erbout = _hamlout.buffer
@output_buffer = output_buffer ||= ActionView::OutputBuffer.new rescue nil
END
  postamble = <<END.tr!("\n", ';')
#{precompiled_method_return_value}
ensure
@haml_buffer = @haml_buffer.upper if @haml_buffer
end
END
  "#{preamble}#{locals_code(local_names)}#{precompiled}#{postamble}"
end

#precompiled_with_return_valueObject



41
42
43
# File 'lib/hamlit/parser/haml_compiler.rb', line 41

def precompiled_with_return_value
  "#{precompiled};#{precompiled_method_return_value}"
end