Class: Opal::ERB::Compiler

Inherits:
Object show all
Defined in:
opal/lib/opal/erb.rb

Constant Summary

BLOCK_EXPR =
/\s+(do|\{)(\s*\|[^|]*\|)?\s*\Z/

Instance Method Summary collapse

Instance Method Details

#compile(source, file_name = '(erb)') ⇒ Object



10
11
12
13
14
15
16
17
18
19
# File 'opal/lib/opal/erb.rb', line 10

def compile(source, file_name = '(erb)')
  @source, @file_name, @result = source, file_name, source

  self.fix_quotes
  self.find_contents
  self.find_code
  self.wrap_compiled

  Opal.compile @result
end

#find_codeObject



39
40
41
42
43
# File 'opal/lib/opal/erb.rb', line 39

def find_code
  @result = @result.gsub(/<%([\s\S]+?)%>/) do
    "\")\n#{ $1 }\noutput_buffer.append(\""
  end
end

#find_contentsObject



27
28
29
30
31
32
33
34
35
36
37
# File 'opal/lib/opal/erb.rb', line 27

def find_contents
  @result = @result.gsub(/<%=([\s\S]+?)%>/) do
    inner = $1.gsub(/\\'/, "'").gsub(/\\"/, '"')

    if inner =~ BLOCK_EXPR
      "\")\noutput_buffer.append= #{ inner }\noutput_buffer.append(\""
    else
      "\")\noutput_buffer.append=(#{ inner })\noutput_buffer.append(\""
    end
  end
end

#fix_quotesObject



21
22
23
# File 'opal/lib/opal/erb.rb', line 21

def fix_quotes
  @result = @result.gsub '"', '\\"'
end

#wrap_compiledObject



45
46
47
# File 'opal/lib/opal/erb.rb', line 45

def wrap_compiled
  @result = "Template.new('#@file_name') do |output_buffer|\noutput_buffer.append(\"#@result\")\noutput_buffer.join\nend\n"
end