Skip to content
This repository has been archived by the owner on Jun 16, 2022. It is now read-only.

Commit

Permalink
handle unicode better by decoding early and encoding late
Browse files Browse the repository at this point in the history
  • Loading branch information
alanhamlett committed Jul 17, 2014
1 parent f3359d6 commit bce5a78
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 7 deletions.
2 changes: 1 addition & 1 deletion jac/compat.py
Expand Up @@ -11,7 +11,7 @@
if is_py2:

def _u(text):
if type(text) == str:
if isinstance(text, str):
return text.decode('utf-8')
return unicode(text)
u = _u
Expand Down
3 changes: 3 additions & 0 deletions jac/compilers/__init__.py
Expand Up @@ -19,6 +19,9 @@ def __new__(meta, name, bases, attrs):
def compile(what, mimetype, cwd=None, uri_cwd=None, debug=None):
"""
Compile a given text based on mimetype.
The text to compile must be provided as a Unicode object and this
function must return the compiled text as a Unicode object.
"""

try:
Expand Down
5 changes: 3 additions & 2 deletions jac/compilers/less.py
@@ -1,7 +1,7 @@
import subprocess
from six import with_metaclass

from jac.compat import file
from jac.compat import file, u

from . import CompilerMeta

Expand Down Expand Up @@ -35,7 +35,8 @@ def compile(cls, what, mimetype='text/less', cwd=None, uri_cwd=None,

if isinstance(what, file):
what = what.read()
(stdout, stderr) = handler.communicate(input=what)
(stdout, stderr) = handler.communicate(input=what.encode('utf-8'))
stdout = u(stdout)

if handler.returncode == 0:
return stdout
Expand Down
3 changes: 2 additions & 1 deletion jac/compilers/sass.py
@@ -1,7 +1,7 @@
import subprocess
from six import with_metaclass

from jac.compat import file
from jac.compat import file, u

from . import CompilerMeta

Expand All @@ -27,6 +27,7 @@ def compile(cls, what, mimetype='text/sass', cwd=None,
if isinstance(what, file):
what = what.read()
(stdout, stderr) = handler.communicate(input=what)
stdout = u(stdout)

if handler.returncode == 0:
return stdout
Expand Down
5 changes: 2 additions & 3 deletions jac/extension.py
Expand Up @@ -88,9 +88,9 @@ def _make_hash(self, html, compilables):

def _get_contents(self, src):
if isinstance(src, file):
return src.read()
return u(src.read())
else:
return src
return u(src)

def _compile(self, compression_type, caller):
html = caller()
Expand Down Expand Up @@ -143,7 +143,6 @@ def _compile(self, compression_type, caller):
uri_cwd=uri_cwd, debug=debug)
else:
text = self._get_contents(src)
text = u(text)

if not debug:
outfile = cached_file
Expand Down

0 comments on commit bce5a78

Please sign in to comment.