1
require 'action_dispatch/http/mime_type'
2
require 'active_support/core_ext/class/attribute'
8
class Erubis < ::Erubis::Eruby
10
src << "@output_buffer = output_buffer || ActionView::OutputBuffer.new;"
13
def add_text(src, text)
15
src << "@output_buffer.safe_concat('" << escape_text(text) << "');"
18
BLOCK_EXPR = /\s+(do|\{)(\s*\|[^|]*\|)?\s*\Z/
20
def add_expr_literal(src, code)
22
src << '@output_buffer.append= ' << code
24
src << '@output_buffer.append= (' << code << ');'
28
def add_expr_escaped(src, code)
30
src << "@output_buffer.safe_append= " << code
32
src << "@output_buffer.safe_concat((" << code << ").to_s);"
36
def add_postamble(src)
37
src << '@output_buffer.to_s'
42
# Specify trim mode for the ERB compiler. Defaults to '-'.
43
# See ERB documentation for suitable values.
44
class_attribute :erb_trim_mode
45
self.erb_trim_mode = '-'
47
# Default implementation used.
48
class_attribute :erb_implementation
49
self.erb_implementation = Erubis
51
ENCODING_TAG = Regexp.new("\\A(<%#{ENCODING_FLAG}-?%>)[ \\t]*")
53
def self.call(template)
57
def supports_streaming?
66
if template.source.encoding_aware?
67
# First, convert to BINARY, so in case the encoding is
68
# wrong, we can still find an encoding tag
69
# (<%# encoding %>) inside the String using a regular
71
template_source = template.source.dup.force_encoding("BINARY")
73
erb = template_source.gsub(ENCODING_TAG, '')
76
erb.force_encoding valid_encoding(template.source.dup, encoding)
78
# Always make sure we return a String in the default_internal
81
erb = template.source.dup
84
self.class.erb_implementation.new(
86
:trim => (self.class.erb_trim_mode == "-")
92
def valid_encoding(string, encoding)
93
# If a magic encoding comment was found, tag the
94
# String with this encoding. This is for a case
95
# where the original String was assumed to be,
96
# for instance, UTF-8, but a magic comment
98
string.force_encoding(encoding) if encoding
100
# If the String is valid, return the encoding we found
101
return string.encoding if string.valid_encoding?
103
# Otherwise, raise an exception
104
raise WrongEncodingError.new(string, string.encoding)