Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Inline templates encoding issue (Ruby 1.9, Sinatra 1.1.0) #136

Closed
mislav opened this Issue Dec 12, 2010 · 1 comment

Comments

Projects
None yet
2 participants

mislav commented Dec 12, 2010

Using Unicode characters in inline templates can prevent Sinatra from loading on some systems, specifically Heroku. Not sure if this is something that has to be solved by Sinatra, so I'm posting here for discussion.

On my development machine, Mac OS X, the value of the "LANG" environment variable is set by default like so:

$ echo $LANG
en_US.UTF-8

This affects Ruby Encoding.default_external to be UTF-8, meaning that externally loaded files, including the main application file which has inline templates, will be interpreted as UTF-8 and therefore can contain Unicode characters.

On a system where LANG is not set, such as Heroku, the default_external encoding will default to ASCII:

$ LANG= ruby -e 'puts Encoding.default_external'
US-ASCII

So when this app containing Unicode chars in the inline template gets deployed on Heroku, it will fail.

The current solution I'm using is to explicitly define external encoding in config.ru:

Encoding.default_external = 'utf-8'

However, the real solution in Sinatra might be to tweak the inline_templates=() method:

app, data =
  ::IO.read(file, nil, 0, encoding: settings.default_encoding).
  gsub("\r\n", "\n").split(/^__END__$/, 2)

Notice the arguments for IO.read. This way, inline templates will work consistently across deployments without the user having to tamper with default_external encoding.

Owner

rkh commented Dec 13, 2010

Set encoding for inline templates correctly.

  • If Ruby version is not encoding aware, do nothing
  • If file contains a magic comment in the first line, use that encoding
  • Otherwise use settings.default_encoding

Allows one to use unicode characters without having to escape them and deploy
on Linux systems, like Heroku, on 1.9.2 (on OSX encodings always default to
UTF-8, which makes it hard to spot this issue before deployment).

Closed by b23a6fe.

7stud pushed a commit to 7stud/sinatra that referenced this issue Aug 27, 2014

Merge pull request #136 from gruiz17/patch-2
add links to actual GitHub profiles in about page

This issue was closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment