Sha256: bd6ff30ef4807baf34124e4e898f5e6e9684e7ffad8d3cbed1824588e7e09495

Contents?: true

Size: 1.23 KB

Versions: 8

Compression:

Stored size: 1.23 KB

Contents

require 'opal/compiler'

module Opal
  module ERB
    def self.compile(source, file_name = '(erb)')
      Compiler.new.compile source, file_name
    end

    class Compiler
      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

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

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

      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

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

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

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
opal-0.6.3 lib/opal/erb.rb
opal-0.6.2 lib/opal/erb.rb
opal-0.6.1 lib/opal/erb.rb
opal-0.6.0 lib/opal/erb.rb
opal-0.5.5 lib/opal/erb.rb
opal-0.5.4 lib/opal/erb.rb
opal-0.5.2 lib/opal/erb.rb
opal-0.5.0 lib/opal/erb.rb