##// END OF EJS Templates
Backport PR #4163: Fix for incorrect default encoding on Windows....
Backport PR #4163: Fix for incorrect default encoding on Windows. Whilst trying out rendering notebooks in a flask app under Apache on Windows I got the below error when simply trying to import `SlidesExporter` ```python mod_wsgi (pid=6260): Exception occurred processing WSGI script 'flask_test.wsgi'. Traceback (most recent call last): File "flask_test.py", line 81, in render_notebook from IPython.nbconvert.exporters import SlidesExporter File "c:\\dev\\code\\ipython\\IPython\\__init__.py", line 47, in <module> from .terminal.embed import embed File "c:\\dev\\code\\ipython\\IPython\\terminal\\embed.py", line 32, in <module> from IPython.terminal.interactiveshell import TerminalInteractiveShell File "c:\\dev\\code\\ipython\\IPython\\terminal\\interactiveshell.py", line 25, in <module> from IPython.core.interactiveshell import InteractiveShell, InteractiveShellABC File "c:\\dev\\code\\ipython\\IPython\\core\\interactiveshell.py", line 59, in <module> from IPython.core.prompts import PromptManager File "c:\\dev\\code\\ipython\\IPython\\core\\prompts.py", line 138, in <module> HOME = py3compat.str_to_unicode(os.environ.get("HOME","//////:::::ZZZZZ,,,~~~")) File "c:\\dev\\code\\ipython\\IPython\\utils\\py3compat.py", line 18, in decode return s.decode(encoding, "replace") LookupError: unknown encoding: cp0 ``` A little bit of [googling](http://bugs.python.org/issue6501) suggests that Windows returns 'cp0' to indicate there is no code page. This fix simply looks for this invalid value and replaces it with something valid. With this change it works for me.

File last commit:

r12080:aae5320a
r12463:516353d0
Show More
latex.py
123 lines | 3.7 KiB | text/x-python | PythonLexer
"""Latex filters.
Module of useful filters for processing Latex within Jinja latex templates.
"""
#-----------------------------------------------------------------------------
# Copyright (c) 2013, the IPython Development Team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file COPYING.txt, distributed with this software.
#-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
import re
#-----------------------------------------------------------------------------
# Globals and constants
#-----------------------------------------------------------------------------
LATEX_RE_SUBS = (
(re.compile(r'\.\.\.+'), r'\\ldots'),
)
# Latex substitutions for escaping latex.
# see: http://stackoverflow.com/questions/16259923/how-can-i-escape-latex-special-characters-inside-django-templates
LATEX_SUBS = {
'&': r'\&',
'%': r'\%',
'$': r'\$',
'#': r'\#',
'_': r'\_',
'{': r'\{',
'}': r'\}',
'~': r'\textasciitilde{}',
'^': r'\^{}',
'\\': r'\textbackslash{}',
}
#-----------------------------------------------------------------------------
# Functions
#-----------------------------------------------------------------------------
__all__ = ['escape_latex',
'strip_math_space']
def escape_latex(text):
"""
Escape characters that may conflict with latex.
Parameters
----------
text : str
Text containing characters that may conflict with Latex
"""
text = ''.join(LATEX_SUBS.get(c, c) for c in text)
for pattern, replacement in LATEX_RE_SUBS:
text = pattern.sub(replacement, text)
return text
def strip_math_space(text):
"""
Remove the space between latex math commands and enclosing $ symbols.
This filter is important because latex isn't as flexible as the notebook
front end when it comes to flagging math using ampersand symbols.
Parameters
----------
text : str
Text to filter.
"""
# First, scan through the markdown looking for $. If
# a $ symbol is found, without a preceding \, assume
# it is the start of a math block. UNLESS that $ is
# not followed by another within two math_lines.
math_regions = []
math_lines = 0
within_math = False
math_start_index = 0
ptext = ''
last_character = ""
skip = False
for index, char in enumerate(text):
#Make sure the character isn't preceeded by a backslash
if (char == "$" and last_character != "\\"):
# Close the math region if this is an ending $
if within_math:
within_math = False
skip = True
ptext = ptext+'$'+text[math_start_index+1:index].strip()+'$'
math_regions.append([math_start_index, index+1])
else:
# Start a new math region
within_math = True
math_start_index = index
math_lines = 0
# If we are in a math region, count the number of lines parsed.
# Cancel the math region if we find two line breaks!
elif char == "\n":
if within_math:
math_lines += 1
if math_lines > 1:
within_math = False
ptext = ptext+text[math_start_index:index]
# Remember the last character so we can easily watch
# for backslashes
last_character = char
if not within_math and not skip:
ptext = ptext+char
if skip:
skip = False
return ptext