Class: HamlJsxEngine

Inherits:
Object
  • Object
show all
Defined in:
lib/engine.rb

Constant Summary collapse

MARKER_START =
"(~"
MARKER_END =
"~)"

Class Method Summary collapse

Class Method Details

.evaluate(data) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/engine.rb', line 7

def self.evaluate(data)
  result = ""
  offset = 0

  while true
    idx_start = data.index(MARKER_START, offset)
    if idx_start
      idx_start += 1
      idx_end = data.index(MARKER_END, idx_start + MARKER_END.length)-1

      result += data[offset..(idx_start-1)]
      offset = idx_end + MARKER_END.length

      haml = data[(idx_start + MARKER_START.length)..(idx_end-1)]
      
      # Escape { } so they'll survive the HAML render
      haml.gsub!(/\{/,"'{")
      haml.gsub!(/\}/,"}'")

      # Remove spaces offset
      first_indent = haml.match(/\s*/)[0]
      haml.gsub!(/^#{first_indent}/,"")

      # Dot optimization
      haml.gsub!(/^(\s*)\.(\(|$)/,'\1%div\2')

      # Remove comments
      haml.gsub!(/^\s*\/\/.*$/,'')
      haml.gsub!(/\/\*.*?\*\//,'')

      html = Haml::Engine.new(haml).render
      html.gsub!("class=","className=")
      html.gsub!(/\'{/,"{")
      html.gsub!(/\}\'/,"}")

      result += html
    else
      result += data[offset..-1]
      break
    end
  end

  result
end