##// END OF EJS Templates
Summary of changes:...
Summary of changes: 1) IPython/core/compilerop.py: IronPython __future__ flags are non-standard, Solution try/except; comment added 2) IPython/core/completer.py: __main__ was undefined, due to local mistake in creating IronPython scope; removed this tweak 3) IPython/core/prompts.py: os.getuid() is not defined (IronPython bug; see: https://mail.python.org/pipermail/ironpython-users/2014-February/016812.html) 4) IPython/lib/inputhook.py: ctypes SystemError; comment added 5) IPython/utils/process.py and IPython/utils/_process_cli.py: adds a new _process_cli.py which would handle the processes under cli; fixed os.pathsep 6) IPython/utils/io.py: devnull opened in append mode; changed to "w" 7) New issue: IPython/external/decorator/_decorator.py: IronPython doesn't have _getframes, unless FullFrames is set to true; comment added

File last commit:

r14042:6e7009d8
r15208:301956c6
Show More
highlight.py
123 lines | 4.0 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
#-----------------------------------------------------------------------------
# pygments must not be imported at the module level
# because errors should be raised at runtime if it's actually needed,
# not import time, when it may not be needed.
# Our own imports
from IPython.nbconvert.utils.base import NbConvertBase
#-----------------------------------------------------------------------------
# Globals and constants
#-----------------------------------------------------------------------------
MULTILINE_OUTPUTS = ['text', 'html', 'svg', 'latex', 'javascript', 'json']
#-----------------------------------------------------------------------------
# Utility functions
#-----------------------------------------------------------------------------
__all__ = [
'Highlight2Html',
'Highlight2Latex'
]
class Highlight2Html(NbConvertBase):
def __call__(self, source, language=None, metadata=None):
"""
Return a syntax-highlighted version of the input source as html output.
Parameters
----------
source : str
source of the cell to highlight
language : str
language to highlight the syntax of
metadata : NotebookNode cell metadata
metadata of the cell to highlight
"""
from pygments.formatters import HtmlFormatter
if not language:
language=self.default_language
return _pygments_highlight(source, HtmlFormatter(), language, metadata)
class Highlight2Latex(NbConvertBase):
def __call__(self, source, language=None, metadata=None, strip_verbatim=False):
"""
Return a syntax-highlighted version of the input source as latex output.
Parameters
----------
source : str
source of the cell to highlight
language : str
language to highlight the syntax of
metadata : NotebookNode cell metadata
metadata of the cell to highlight
strip_verbatim : bool
remove the Verbatim environment that pygments provides by default
"""
from pygments.formatters import LatexFormatter
if not language:
language=self.default_language
latex = _pygments_highlight(source, LatexFormatter(), language, metadata)
if strip_verbatim:
latex = latex.replace(r'\begin{Verbatim}[commandchars=\\\{\}]' + '\n', '')
return latex.replace('\n\\end{Verbatim}\n', '')
else:
return latex
def _pygments_highlight(source, output_formatter, language='ipython', metadata=None):
"""
Return a syntax-highlighted version of the input source
Parameters
----------
source : str
source of the cell to highlight
output_formatter : Pygments formatter
language : str
language to highlight the syntax of
metadata : NotebookNode cell metadata
metadata of the cell to highlight
"""
from pygments import highlight
from pygments.lexers import get_lexer_by_name
from IPython.nbconvert.utils.lexers import IPythonLexer
# If the cell uses a magic extension language,
# use the magic language instead.
if language == 'ipython' \
and metadata \
and 'magics_language' in metadata:
language = metadata['magics_language']
if language == 'ipython':
lexer = IPythonLexer()
else:
lexer = get_lexer_by_name(language, stripall=True)
return highlight(source, lexer, output_formatter)