##// 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:

r11685:1f458eea
r12463:516353d0
Show More
highlight.py
88 lines | 2.6 KiB | text/x-python | PythonLexer
"""
Module containing filter functions that allow code to be highlighted
from within Jinja 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
#-----------------------------------------------------------------------------
from pygments import highlight as pygements_highlight
from pygments.lexers import get_lexer_by_name
from pygments.formatters import HtmlFormatter
from pygments.formatters import LatexFormatter
# Our own imports
from IPython.nbconvert.utils.lexers import IPythonLexer
#-----------------------------------------------------------------------------
# Globals and constants
#-----------------------------------------------------------------------------
MULTILINE_OUTPUTS = ['text', 'html', 'svg', 'latex', 'javascript', 'json']
#-----------------------------------------------------------------------------
# Utility functions
#-----------------------------------------------------------------------------
__all__ = [
'highlight2html',
'highlight2latex'
]
def highlight2html(source, language='ipython'):
"""
Return a syntax-highlighted version of the input source as html output.
Parameters
----------
source : str
Source code to highlight the syntax of.
language : str
Language to highlight the syntax of.
"""
return _pygment_highlight(source, HtmlFormatter(), language)
def highlight2latex(source, language='ipython'):
"""
Return a syntax-highlighted version of the input source as latex output.
Parameters
----------
source : str
Source code to highlight the syntax of.
language : str
Language to highlight the syntax of.
"""
return _pygment_highlight(source, LatexFormatter(), language)
def _pygment_highlight(source, output_formatter, language='ipython'):
"""
Return a syntax-highlighted version of the input source
Parameters
----------
source : str
Source code to highlight the syntax of.
output_formatter : Pygments formatter
language : str
Language to highlight the syntax of.
"""
if language == 'ipython':
lexer = IPythonLexer()
else:
lexer = get_lexer_by_name(language, stripall=True)
return pygements_highlight(source, lexer, output_formatter)