2

How to reproduce

Gemfile

gem 'haml'
gem 'sass'
gem 'coffee-script'

app/app.rb

get '/javascripts/rus_test.js' do
  content_type 'text/javascript', charset: 'utf-8'
  coffee :"../../public/javascripts/rus_test"
end

public/javascripts/rus_test.coffee (it's work)

alert 'Hello World!'

localhost:3000/javascripts/rus_test.js

(function() {

  alert('Hello World!');

}).call(this);

public/javascripts/rus_test.coffee (doesn't work)

alert 'Привет!'

GIVES ERROR

Encoding::UndefinedConversionError at /javascripts/rus_test.js "\xD0" from ASCII-8BIT to UTF-8

I tried to set several variants of Encoding.default in config/boot.rb.

one of variants of boot initializing

Padrino.before_load do
  Encoding.default_internal = nil
  # Encoding.default_external = 'ASCII-8BIT'
end

It's work for HAML with russian text, but doesn't work for SASS and COFFEE

What should I do to fix it?

the-teacher
  • 589
  • 8
  • 19
  • I'm using Sinatra (which Padrino is based on) + [Sprockets](https://github.com/sstephenson/sprockets) to serve the assets and can process `alert 'Привет!'` with no problem. Is the file correctly encoded as UTF-8? Can you compile it with the `coffee` command? – epidemian May 31 '12 at 13:51
  • we are complilled with pure coffee. Works fine! Code in Sinatra works fine. Probably Padrino needs for some Magick comment some where? – the-teacher May 31 '12 at 14:01
  • Iam use Sublime Text Editor with utf-8. – the-teacher May 31 '12 at 14:03

1 Answers1

2

Remove ridiculous Encoding.default_internal = nil and add this code somewhere to monkey patch Tilt:

# this makes tilt to treat templates as properly encoded (respect Encoding.default_external)
module Tilt
  class CoffeeScriptTemplate
    def prepare
      @data.force_encoding Encoding.default_external
      if !options.key?(:bare) and !options.key?(:no_wrap)
        options[:bare] = self.class.default_bare
      end
    end
  end
end
ujifgc
  • 2,215
  • 2
  • 19
  • 21