Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

rails shim to replace SafeBuffer with ContextualBuffer

  • Loading branch information...
commit 4dcc9122f642db138cfeedff9cef0b02b6b834b3 1 parent 9cca5a4
@igrigorik authored
Showing with 50 additions and 1 deletion.
  1. +1 −1  lib/contextual.rb
  2. +49 −0 lib/contextual/rails_erubis.rb
View
2  lib/contextual.rb
@@ -1,3 +1,3 @@
require "contextual/version"
require "contextual/contextual"
-
+require "contextual/rails_erubis" if defined? Rails
View
49 lib/contextual/rails_erubis.rb
@@ -0,0 +1,49 @@
+module ActionView
+ class Template
+ module Handlers
+ class Erubis < ::Erubis::Eruby
+ def add_preamble(src)
+ src << "@output_buffer = output_buffer || Erubis::ContextualBuffer.new; "
+ end
+
+ def add_text(src, text)
+ if !text.empty?
+
+ src << "@output_buffer.writeSafe '" << text.to_s.gsub("'", "\\\\'") << "';"
+ end
+ end
+
+ def add_stmt(src, code)
+ src << code
+ src << ';' unless code[-1] == ?\n
+ end
+
+ def add_expr_escaped(src, code)
+ src << " @output_buffer.write((" << code << ').to_s);'
+ end
+
+ def add_expr_literal(src, code)
+ if code =~ BLOCK_EXPR
+ src << '@output_buffer.writeSafe ' << code
+ else
+ src << '@output_buffer.writeSafe (' << code << ');'
+ end
+ end
+
+ def add_expr_escaped(src, code)
+ if code =~ BLOCK_EXPR
+ src << "@output_buffer.write " << code
+ else
+ src << "@output_buffer.write (" << code << ");"
+ end
+ end
+
+ def add_postamble(src)
+ src << "@output_buffer.close \n"
+ # src << "p [:CONTEXTUAL @output_buffer.to_s]\n"
+ src << "@output_buffer.to_s"
+ end
+ end
+ end
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.