Sha256: 8db55fbf673e606e8002992bac5311a9d2fbb584b7cafce2d0ad725c2ddfb19c

Contents?: true

Size: 860 Bytes

Versions: 1

Compression:

Stored size: 860 Bytes

Contents

module Transit
  module Builders
    class JstBuilder < Tilt::Template
      
      self.default_mime_type = 'application/javascript'
      
      def prepare
      end
      
      def evaluate(scope, locals, &block)
        <<-JST
        (function() {
          try{
              transit.template.add(#{scope.logical_path.inspect}, "#{sanitize_js(data)}");
            }catch(e){};
          }).call();
        JST
      end

      private
      
      def sanitize_js(string)
        js_map = {
          '\\'    => '\\\\',
          '</'    => '<\/',
          "\r\n"  => '\n',
          "\n"    => '\n',
          "\r"    => '\n',
          '"'     => '\\"',
          "'"     => "\\'" 
        }                
        string.gsub(/$(.)/m, "\\1  ").strip.gsub(/(\\|<\/|\r\n|[\n\r"'])/) { |match| js_map[match] }
      end
      
    end
    
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
transit-0.0.2 lib/transit/builders/jst_builder.rb