Class: Hamlit::HamlCompiler

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

Instance Attribute Summary collapse

Class Method 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

#options ⇒ Object

Returns the value of attribute options.



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

def options
  @options
end

Class Method Details

.build_attributes(is_html, attr_wrapper, escape_attrs, hyphenate_data_attrs, attributes = {}) ⇒ Object

This is a class method so it can be accessed from Buffer.



418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
# File 'lib/hamlit/parser/haml_compiler.rb', line 418

def self.build_attributes(is_html, attr_wrapper, escape_attrs, hyphenate_data_attrs, attributes = {})
    # @TODO this is an absolutely ridiculous amount of arguments. At least
  # some of this needs to be moved into an instance method.
  quote_escape     = attr_wrapper == '"' ? """ : "'"
  other_quote_char = attr_wrapper == '"' ? "'" : '"'
  join_char        = hyphenate_data_attrs ? '-' : '_'

  attributes.each do |key, value|
    if value.is_a?(Hash)
      data_attributes = attributes.delete(key)
      data_attributes = flatten_data_attributes(data_attributes, '', join_char)
      data_attributes = build_data_keys(data_attributes, hyphenate_data_attrs, key)
      attributes = data_attributes.merge(attributes)
    end
  end

  result = attributes.collect do |attr, value|
    next if value.nil?

    value = filter_and_join(value, ' ') if attr == 'class'
    value = filter_and_join(value, '_') if attr == 'id'

    if value == true
      next " #{attr}" if is_html
      next " #{attr}=#{attr_wrapper}#{attr}#{attr_wrapper}"
    elsif value == false
      next
    end

    escaped =
      if escape_attrs == :once
        ::Hamlit::HamlHelpers.escape_once(value.to_s)
      elsif escape_attrs
        ::Hamlit::HamlHelpers.html_escape(value.to_s)
      else
        value.to_s
      end
    value = ::Hamlit::HamlHelpers.preserve(escaped)
    if escape_attrs
      # We want to decide whether or not to escape quotes
      value.gsub!(/"|"/, '"')
      this_attr_wrapper = attr_wrapper
      if value.include? attr_wrapper
        if value.include? other_quote_char
          value.gsub!(attr_wrapper, quote_escape)
        else
          this_attr_wrapper = other_quote_char
        end
      end
    else
      this_attr_wrapper = attr_wrapper
    end
    " #{attr}=#{this_attr_wrapper}#{value}#{this_attr_wrapper}"
  end
  result.compact!
  result.sort!
  result.join
end

.build_data_keys(data_hash, hyphenate, attr_name = "data") ⇒ Object



491
492
493
494
495
496
497
498
499
500
501
# File 'lib/hamlit/parser/haml_compiler.rb', line 491

def self.build_data_keys(data_hash, hyphenate, attr_name="data")
  Hash[data_hash.map do |name, value|
    if name == nil
      [attr_name, value]
    elsif hyphenate
      ["#{attr_name}-#{name.to_s.tr('_', '-')}", value]
    else
      ["#{attr_name}-#{name}", value]
    end
  end]
end

.filter_and_join(value, separator) ⇒ Object



477
478
479
480
481
482
483
484
485
486
487
488
489
# File 'lib/hamlit/parser/haml_compiler.rb', line 477

def self.filter_and_join(value, separator)
  return '' if (value.respond_to?(:empty?) && value.empty?)

  if value.is_a?(Array)
    value.flatten!
    value.map! {|item| item ? item.to_s : nil}
    value.compact!
    value = value.join(separator)
  else
    value = value ? value.to_s : nil
  end
  !value.nil? && !value.empty? && value
end

.flatten_data_attributes(data, key, join_char, seen = []) ⇒ Object



503
504
505
506
507
508
509
510
511
512
513
# File 'lib/hamlit/parser/haml_compiler.rb', line 503

def self.flatten_data_attributes(data, key, join_char, seen = [])
  return {key => data} unless data.is_a?(Hash)

  return {key => nil} if seen.include? data.object_id
  seen << data.object_id

  data.sort {|x, y| x[0].to_s <=> y[0].to_s}.inject({}) do |hash, (k, v)|
    joined = key == '' ? k : [key, k].join(join_char)
    hash.merge! flatten_data_attributes(v, joined, join_char, seen)
  end
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

#precompiled ⇒ String

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 = "begin\nextend ::Hamlit::HamlHelpers\n_hamlout = @haml_buffer = ::Hamlit::HamlBuffer.new(haml_buffer, \#{options.for_buffer.inspect})\n_erbout = _hamlout.buffer\n@output_buffer = output_buffer ||= ActionView::OutputBuffer.new rescue nil\n".tr!("\n", ';')
  postamble = "\#{precompiled_method_return_value}\nensure\n@haml_buffer = @haml_buffer.upper if @haml_buffer\nend\n".tr!("\n", ';')
  "#{preamble}#{locals_code(local_names)}#{precompiled}#{postamble}"
end

#precompiled_with_return_value ⇒ Object



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

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