Skip to content

Conversation

@andycox
Copy link

@andycox andycox commented Oct 25, 2013

Tilt introduced better encoding support in version 1.4.0. In particular, this allows Stasis to correctly handle UTF-8 encoded template files (e.g., CoffeeScript) with extended Unicode characters.

For example, when a CoffeeScript file contains a string with a ↑ character (U+2191, UTF-8: E2 86 91), Stasis 0.2.0 fails with the following error:

[2013-10-24 15:01:54] Error: "\xE2" from ASCII-8BIT to UTF-8

Stasis::Options.set_template_option will pass options to Tilt for a given template type and force UTF-8 encoding in CoffeeScript templates, but Tilt 1.3.3 does not support any template encoding options. A magic comment (e.g., # coding: utf-8) does not appear to force UTF-8 encoding in a CoffeeScript file through Tilt.

However, the following was added to the Tilt::Template base class constructor in 1.4.0:

if @data.respond_to?(:force_encoding)
  @data.force_encoding(default_encoding) if default_encoding

  if !@data.valid_encoding?
    raise Encoding::InvalidByteSequenceError, "#{eval_file} is not valid #{@data.encoding}"
  end
end

This means that I can add this to a controller:

Stasis::Options.set_template_option 'coffee', { :default_encoding => 'UTF-8' }

which would then make Tilt call force_encoding('UTF-8') on all CoffeeScript templates it processes.

The only catch I found to bumping Stasis to Tilt 1.4.1 is that Tilt 1.3.4 introduced a no-op "template" for files ending in .html. This breaks some of the default copying behavior (and 4 of the tests). The simplest way to address this is to reach into Tilt and remove the template.

If I've completely overlooked a much simpler way to include Unicode characters in CoffeeScript via Stasis, please let me know. I couldn't find anything obvious and this seemed to be the most flexible solution.

@hughbien hughbien mentioned this pull request Jan 14, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant