Class: Compiler

Inherits:
HamlToStar::Compiler
  • Object
show all
Defined in:
lib/haml_to_js/compiler.rb

Instance Method Summary collapse

Instance Method Details

#add_code(str, line, inside) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/haml_to_js/compiler.rb', line 37

def add_code(str, line, inside)
  processed_line = process_code_line(line[1..line.size - 1])
  str << processed_line[:processed_line] + (inside.size > 0 ? '' : ';')
  if (inside.size > 0)
    if (!processed_line[:changed])
      str << '{'
    end
    str << inside.join("\n")
    str << '}'
  end
end

#add_content(str, content) ⇒ Object



33
34
35
# File 'lib/haml_to_js/compiler.rb', line 33

def add_content(str, content)
  str << "#{@variable_name} += " + content + ';'
end

#evaluate(line) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'lib/haml_to_js/compiler.rb', line 49

def evaluate(line)
  if (line[0] == '=')
    return 'escape(' + line[1..line.size - 1] + ')'
  elsif (line[0..1] == '!=')
    return line[2..line.size - 1]
  else
    return line
  end
end

#initialize_content(str, content) ⇒ Object



27
28
29
30
31
# File 'lib/haml_to_js/compiler.rb', line 27

def initialize_content(str, content)
  str << "var #{@variable_name} = '';"
  str << "var #{@variable_line_name} = 0;"
  str << content.join("\n")
end

#process_code_line(line) ⇒ Object



20
21
22
23
24
25
# File 'lib/haml_to_js/compiler.rb', line 20

def process_code_line(line)
  processed_line = line;
  processed_line = processed_line.gsub(%r{each *([^ ]*) *: *([^ ]*) *in *([^ ]*)}, 'for (var \1 in \3) { var \2 = \3[\1];')
  processed_line = processed_line.gsub(%r{each *([^ ]*) *in *([^ ]*)}, 'for (var _$key in \2) { var \1 = \2[_$key];')
  return {:processed_line => processed_line, :changed => line != processed_line}
end

#process_code_line_number(str, code_line_number) ⇒ Object



59
60
61
# File 'lib/haml_to_js/compiler.rb', line 59

def process_code_line_number(str, code_line_number)
  str << "#{@variable_line_name} = #{code_line_number};";
end

#process_dom_params(dom_params) ⇒ Object



16
17
18
# File 'lib/haml_to_js/compiler.rb', line 16

def process_dom_params(dom_params)
  return dom_params.gsub(%r{(for|name|id|class) *:}, '\'\1\':')
end

#process_inline_code(str, content) ⇒ Object



63
64
65
# File 'lib/haml_to_js/compiler.rb', line 63

def process_inline_code(str, content)
  str << "#{@variable_name} += " + evaluate(content) + ';'
end