Permalink
Browse files

Ruby 1.9: ERB template encoding using a magic comment at the top of t…

…he file
  • Loading branch information...
1 parent 457a546 commit 81e06075b707ac07a65701fcd667ffc93580e736 @jeremy jeremy committed May 28, 2009
View
@@ -1,4 +1,7 @@
-*Edge*
+*2.3.6 (unreleased)*
+
+* Ruby 1.9: ERB template encoding using a magic comment at the top of the file. [Jeremy Kemper]
+ <%# encoding: utf-8 %>
* Fixed that default locale templates should be used if the current locale template is missing [DHH]
@@ -65,16 +65,26 @@ def compile(local_assigns)
def compile!(render_symbol, local_assigns)
locals_code = local_assigns.keys.map { |key| "#{key} = local_assigns[:#{key}];" }.join
+ code = compiled_source
+ encoding_comment = $1 if code.sub!(/\A(#.*coding.*)\n/, '')
+
source = <<-end_src
def #{render_symbol}(local_assigns)
- old_output_buffer = output_buffer;#{locals_code};#{compiled_source}
+ old_output_buffer = output_buffer;#{locals_code};#{code}
ensure
self.output_buffer = old_output_buffer
end
end_src
+ if encoding_comment
+ source = "#{encoding_comment}\n#{source}"
+ line = -1
+ else
+ line = 0
+ end
+
begin
- ActionView::Base::CompiledTemplates.module_eval(source, filename, 0)
+ ActionView::Base::CompiledTemplates.module_eval(source, filename, line)
rescue Errno::ENOENT => e
raise e # Missing template file, re-raise for Base to rescue
rescue Exception => e # errors from template code
@@ -11,11 +11,7 @@ class ERB < TemplateHandler
self.erb_trim_mode = '-'
def compile(template)
- src = ::ERB.new("<% __in_erb_template=true %>#{template.source}", nil, erb_trim_mode, '@output_buffer').src
-
- # Ruby 1.9 prepends an encoding to the source. However this is
- # useless because you can only set an encoding on the first line
- RUBY_VERSION >= '1.9' ? src.sub(/\A#coding:.*\n/, '') : src
+ ::ERB.new("<% __in_erb_template=true %>#{template.source}", nil, erb_trim_mode, '@output_buffer').src
end
end
end
@@ -1,2 +1,5 @@
+<%# encoding: utf-8 -%>
Русский текст
-日本語のテキスト
+<%= "".encoding %>
+<%= @output_buffer.encoding %>
+<%= __ENCODING__ %>
@@ -251,7 +251,7 @@ def test_render_with_nested_layout
if '1.9'.respond_to?(:force_encoding)
def test_render_utf8_template
result = @view.render(:file => "test/utf8.html.erb", :layouts => "layouts/yield")
- assert_equal "Русский текст\n日本語のテキスト", result
+ assert_equal "Русский текст\nUTF-8\nUTF-8\nUTF-8\n", result
assert_equal Encoding::UTF_8, result.encoding
end
end

0 comments on commit 81e0607

Please sign in to comment.