Browse Source

Merge pull request #550 from ulikoehler/utf8fix

Fix non-ASCII code with include_code causing errors
Justin Mayer 8 years ago
parent
commit
ec6f25a396
1 changed files with 13 additions and 2 deletions
  1. 13 2
      liquid_tags/include_code.py

+ 13 - 2
liquid_tags/include_code.py

@@ -6,7 +6,7 @@ based on the octopress video tag [1]_
 
 Syntax
 ------
-{% include_code path/to/code [lang:python] [Title text] %}
+{% include_code path/to/code [lang:python] [Title text] [codec:utf8] %}
 
 The "path to code" is specified relative to the ``code`` subdirectory of
 the content directory  Optionally, this subdirectory can be specified in the
@@ -14,6 +14,14 @@ config file:
 
     CODE_DIR = 'code'
 
+If your input file is not ASCII/UTF-8 encoded, you need to specify the
+appropriate input codec by using the ``codec`` option.
+Example ``codec:iso-8859-1``
+Using this option does not affect the output encoding.
+
+For a list of valid codec identifiers, see
+https://docs.python.org/2/library/codecs.html#standard-encodings
+
 Example
 -------
 {% include_code myscript.py %}
@@ -45,6 +53,8 @@ FORMAT = re.compile(r"""
 (?:\s+)?                           # Whitespace
 (?P<hidefilename>:hidefilename:)?  # Hidefilename flag
 (?:\s+)?                           # Whitespace
+(?:(?:codec:)(?P<codec>\S+))?        # Optional language
+(?:\s+)?                           # Whitespace
 (?P<title>.+)?$                    # Optional title
 """, re.VERBOSE)
 
@@ -61,6 +71,7 @@ def include_code(preprocessor, tag, markup):
         argdict = match.groupdict()
         title = argdict['title'] or ""
         lang = argdict['lang']
+        codec = argdict['codec'] or "utf8"
         lines = argdict['lines']
         hide_filename = bool(argdict['hidefilename'])
         if lines:
@@ -116,7 +127,7 @@ def include_code(preprocessor, tag, markup):
     source = (open_tag
               + '\n\n    '
               + lang_include
-              + '\n    '.join(code.split('\n')) + '\n\n'
+              + '\n    '.join(code.decode(codec).split('\n')) + '\n\n'
               + close_tag + '\n')
 
     return source