##// END OF EJS Templates
Merge pull request #4295 from mgaitan/1093_boundary_load_magic...
Merge pull request #4295 from mgaitan/1093_boundary_load_magic Implement boundary option for load magic `%load -r 1-10` now does what one should expect. closes #1093

File last commit:

r12717:1d3685fa
r12838:9bfd8553 merge
Show More
highlight.py
110 lines | 3.4 KiB | text/x-python | PythonLexer
Jonathan Frederic
Cleanup and refactor of filters
r10676 """
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
#-----------------------------------------------------------------------------
Jonathan Frederic
Almost have nbconvert working again...
r10630
Pablo de Oliveira
Select adequate highlighter for cell magic languages...
r12571 from pygments import highlight as pygements_highlight
Jonathan Frederic
Almost have nbconvert working again...
r10630 from pygments.lexers import get_lexer_by_name
from pygments.formatters import HtmlFormatter
from pygments.formatters import LatexFormatter
Jonathan Frederic
Post code-review, extended refactor.
r10485 # Our own imports
Brian E. Granger
Fixing import for nbconvert.
r11089 from IPython.nbconvert.utils.lexers import IPythonLexer
Jonathan Frederic
Post code-review, extended refactor.
r10485
#-----------------------------------------------------------------------------
# Globals and constants
#-----------------------------------------------------------------------------
Jonathan Frederic
Cleanup and refactor of filters
r10676 MULTILINE_OUTPUTS = ['text', 'html', 'svg', 'latex', 'javascript', 'json']
Jonathan Frederic
Post code-review, extended refactor.
r10485
#-----------------------------------------------------------------------------
# Utility functions
#-----------------------------------------------------------------------------
Jonathan Frederic
Cleanup and refactor of filters
r10676
Brian E. Granger
Fixing import logic.
r11088 __all__ = [
Jonathan Frederic
Filter names cleanup
r11685 'highlight2html',
Brian E. Granger
Fixing import logic.
r11088 'highlight2latex'
]
Jonathan Frederic
Add strip_verbatim to latex highlight
r12717
Pablo de Oliveira
highlight: Swap metadata and language keyword arguments
r12578 def highlight2html(source, language='ipython', metadata=None):
Jonathan Frederic
Post code-review, extended refactor.
r10485 """
Return a syntax-highlighted version of the input source as html output.
Pablo de Oliveira
Select adequate highlighter for cell magic languages...
r12571
Jonathan Frederic
Cleanup and refactor of filters
r10676 Parameters
----------
Pablo de Oliveira
highlight filter: pass metadata as an optional argument
r12577 source : str
Jonathan Frederic
Add strip_verbatim to latex highlight
r12717 source of the cell to highlight
Jonathan Frederic
Cleanup and refactor of filters
r10676 language : str
Jonathan Frederic
Add strip_verbatim to latex highlight
r12717 language to highlight the syntax of
Pablo de Oliveira
highlight: Swap metadata and language keyword arguments
r12578 metadata : NotebookNode cell metadata
Jonathan Frederic
Add strip_verbatim to latex highlight
r12717 metadata of the cell to highlight
Jonathan Frederic
Post code-review, extended refactor.
r10485 """
Pablo de Oliveira
Select adequate highlighter for cell magic languages...
r12571
Pablo de Oliveira
highlight: Swap metadata and language keyword arguments
r12578 return _pygment_highlight(source, HtmlFormatter(), language, metadata)
Jonathan Frederic
Cleanup and refactor of filters
r10676
Jonathan Frederic
Post code-review, extended refactor.
r10485
Jonathan Frederic
Add strip_verbatim to latex highlight
r12717 def highlight2latex(source, language='ipython', metadata=None, strip_verbatim=False):
Jonathan Frederic
Post code-review, extended refactor.
r10485 """
Return a syntax-highlighted version of the input source as latex output.
Pablo de Oliveira
Select adequate highlighter for cell magic languages...
r12571
Jonathan Frederic
Cleanup and refactor of filters
r10676 Parameters
----------
Pablo de Oliveira
highlight filter: pass metadata as an optional argument
r12577 source : str
Jonathan Frederic
Add strip_verbatim to latex highlight
r12717 source of the cell to highlight
Jonathan Frederic
Cleanup and refactor of filters
r10676 language : str
Jonathan Frederic
Add strip_verbatim to latex highlight
r12717 language to highlight the syntax of
Pablo de Oliveira
highlight: Swap metadata and language keyword arguments
r12578 metadata : NotebookNode cell metadata
Jonathan Frederic
Add strip_verbatim to latex highlight
r12717 metadata of the cell to highlight
strip_verbatim : bool
remove the Verbatim environment that pygments provides by default
Jonathan Frederic
Post code-review, extended refactor.
r10485 """
Jonathan Frederic
Add strip_verbatim to latex highlight
r12717 latex = _pygment_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
Pablo de Oliveira
Select adequate highlighter for cell magic languages...
r12571
Pablo de Oliveira
highlight: Swap metadata and language keyword arguments
r12578 def _pygment_highlight(source, output_formatter, language='ipython', metadata=None):
Jonathan Frederic
Post code-review, extended refactor.
r10485 """
Return a syntax-highlighted version of the input source
Pablo de Oliveira
Select adequate highlighter for cell magic languages...
r12571
Jonathan Frederic
Cleanup and refactor of filters
r10676 Parameters
----------
Pablo de Oliveira
highlight filter: pass metadata as an optional argument
r12577 source : str
Jonathan Frederic
Add strip_verbatim to latex highlight
r12717 source of the cell to highlight
Jonathan Frederic
Cleanup and refactor of filters
r10676 output_formatter : Pygments formatter
language : str
Jonathan Frederic
Add strip_verbatim to latex highlight
r12717 language to highlight the syntax of
Pablo de Oliveira
highlight: Swap metadata and language keyword arguments
r12578 metadata : NotebookNode cell metadata
Jonathan Frederic
Add strip_verbatim to latex highlight
r12717 metadata of the cell to highlight
Jonathan Frederic
Post code-review, extended refactor.
r10485 """
Pablo de Oliveira
Select adequate highlighter for cell magic languages...
r12571
Pablo de Oliveira
highlight: cell metadata to decide how to highlight foreign languages...
r12572 # If the cell uses a magic extension language,
# use the magic language instead.
if language == 'ipython' \
Pablo de Oliveira
highlight filter: pass metadata as an optional argument
r12577 and metadata \
and 'magics_language' in metadata:
Pablo de Oliveira
highlight: cell metadata to decide how to highlight foreign languages...
r12572
Pablo de Oliveira
highlight filter: pass metadata as an optional argument
r12577 language = metadata['magics_language']
Pablo de Oliveira
Select adequate highlighter for cell magic languages...
r12571
Jonathan Frederic
Cleanup and refactor of filters
r10676 if language == 'ipython':
Jonathan Frederic
Post code-review, extended refactor.
r10485 lexer = IPythonLexer()
else:
Jonathan Frederic
Cleanup and refactor of filters
r10676 lexer = get_lexer_by_name(language, stripall=True)
Jonathan Frederic
Post code-review, extended refactor.
r10485
Pablo de Oliveira
highlight filter: pass metadata as an optional argument
r12577 return pygements_highlight(source, lexer, output_formatter)