Skip to content

Commit

Permalink
Fixing Encoding::CompatibilityError with Ruby 1.9 + ERB, by copying m…
Browse files Browse the repository at this point in the history
…agic comment to the first line so that it works.
  • Loading branch information
gimite committed Dec 5, 2010
1 parent 27f61ef commit e1100df
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion lib/tilt.rb
Expand Up @@ -189,9 +189,16 @@ def self.evaluate(scope, locals, &block)
# easier and more appropriate.
def precompiled(locals)
preamble = precompiled_preamble(locals)
template = precompiled_template(locals)
magic_comment = extract_magic_comment(template)
if magic_comment
# Magic comment e.g. "# coding: utf-8" has to be in the first line.
# So we copy the magic comment to the first line.
preamble = magic_comment + "\n" + preamble
end
parts = [
preamble,
precompiled_template(locals),
template,
precompiled_postamble(locals)
]
[parts.join("\n"), preamble.count("\n") + 1]
Expand Down Expand Up @@ -282,6 +289,10 @@ def unbind_compiled_method(method_name)
method
end

def extract_magic_comment(script)
script.slice(/\A[ \t]*\#.*coding\s*[=:]\s*([[:alnum:]\-_]+).*$/)
end

# Special case Ruby 1.9.1's broken yield.
#
# http://github.com/rtomayko/tilt/commit/20c01a5
Expand Down

0 comments on commit e1100df

Please sign in to comment.