Skip to content

Encoding problem in coffee-script #559

Closed
whitetrefoil opened this Issue Aug 15, 2012 · 2 comments

2 participants

@whitetrefoil

OS: Windows XP in English
Ruby: 1.9.3-p194
Sinatra: 1.3.2

All files are saved in utf-8 without BOM

# asdf.coffee

"something 中文 something"
# asdf.rb

# ...
  get "/js/:name.js" do
    coffee :"#{params[:name]}", :views => "public/js/coffee"
  end
#...

Then open http://localhost:4567/js/asdf.js in the browser:

(function() {
  "something ������ something";



}).call(this);

Whether set settings.default_encoding => 'utf-8' or leave it as default will see the same result.

If use coffee -c . in CMD directly, the output is good.

The same input will output good result in Slim, Sass, etc.

More information:

I found that only if i require "gravatar-ultimate" it will output such result, otherwise it will raise a error.

Simply run this:

# encoding: utf-8
require "sinatra/base"
require "coffee_script"
class CoffeeChs < Sinatra::Base
  get "/" do
    coffee :coffee_chs, :views => "."
  end
  run! if app_file == $0
end

and

# coffee_chs.coffee
alert "中文"

Will raise Encoding::UndefinedConversionError - "\xE4" from ASCII-8BIT to UTF-8:

Find a similar issue?

@whitetrefoil

Finally I add below in my own application to avoid this problem:

# encoding: utf-8

# Refer https://github.com/padrino/padrino-framework/issues/857
module Tilt
  class CoffeeScriptTemplate < Template
    alias_method :original_prepare, :prepare

    def prepare
      @data.force_encoding 'UTF-8'
      original_prepare
    end
  end
end
@rkh
Sinatra member
rkh commented Aug 31, 2012

discuss here: rtomayko/tilt#75

@rkh rkh closed this Aug 31, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.