highlight.py
121 lines
| 3.8 KiB
| text/x-python
|
PythonLexer
Jonathan Frederic
|
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
|
r10630 | |||
Pablo de Oliveira
|
r12571 | from pygments import highlight as pygements_highlight | ||
Jonathan Frederic
|
r10630 | from pygments.lexers import get_lexer_by_name | ||
from pygments.formatters import HtmlFormatter | ||||
from pygments.formatters import LatexFormatter | ||||
Matthias BUSSONNIER
|
r13505 | |||
Jonathan Frederic
|
r10630 | |||
Jonathan Frederic
|
r10485 | # Our own imports | ||
Brian E. Granger
|
r11089 | from IPython.nbconvert.utils.lexers import IPythonLexer | ||
Matthias BUSSONNIER
|
r13505 | from IPython.nbconvert.utils.base import NbConvertBase | ||
Jonathan Frederic
|
r10485 | |||
#----------------------------------------------------------------------------- | ||||
# Globals and constants | ||||
#----------------------------------------------------------------------------- | ||||
Jonathan Frederic
|
r10676 | MULTILINE_OUTPUTS = ['text', 'html', 'svg', 'latex', 'javascript', 'json'] | ||
Jonathan Frederic
|
r10485 | |||
#----------------------------------------------------------------------------- | ||||
# Utility functions | ||||
#----------------------------------------------------------------------------- | ||||
Jonathan Frederic
|
r10676 | |||
Brian E. Granger
|
r11088 | __all__ = [ | ||
Matthias BUSSONNIER
|
r13128 | 'Highlight2Html', | ||
Matthias BUSSONNIER
|
r13505 | 'Highlight2Latex' | ||
Brian E. Granger
|
r11088 | ] | ||
Jonathan Frederic
|
r12717 | |||
Matthias BUSSONNIER
|
r13505 | class Highlight2Html(NbConvertBase): | ||
Matthias BUSSONNIER
|
r13128 | |||
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 | ||||
""" | ||||
if not language: | ||||
Matthias BUSSONNIER
|
r13505 | language=self.default_language | ||
Pablo de Oliveira
|
r12571 | |||
Matthias BUSSONNIER
|
r13128 | return _pygment_highlight(source, HtmlFormatter(), language, metadata) | ||
Jonathan Frederic
|
r10676 | |||
Jonathan Frederic
|
r10485 | |||
Matthias BUSSONNIER
|
r13505 | class Highlight2Latex(NbConvertBase): | ||
Pablo de Oliveira
|
r12571 | |||
Matthias BUSSONNIER
|
r13505 | 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 | ||||
""" | ||||
if not language: | ||||
language=self.default_language | ||||
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
|
r12571 | |||
Pablo de Oliveira
|
r12578 | def _pygment_highlight(source, output_formatter, language='ipython', metadata=None): | ||
Jonathan Frederic
|
r10485 | """ | ||
Return a syntax-highlighted version of the input source | ||||
Pablo de Oliveira
|
r12571 | |||
Jonathan Frederic
|
r10676 | Parameters | ||
---------- | ||||
Pablo de Oliveira
|
r12577 | source : str | ||
Jonathan Frederic
|
r12717 | source of the cell to highlight | ||
Jonathan Frederic
|
r10676 | output_formatter : Pygments formatter | ||
language : str | ||||
Jonathan Frederic
|
r12717 | language to highlight the syntax of | ||
Pablo de Oliveira
|
r12578 | metadata : NotebookNode cell metadata | ||
Jonathan Frederic
|
r12717 | metadata of the cell to highlight | ||
Jonathan Frederic
|
r10485 | """ | ||
Pablo de Oliveira
|
r12571 | |||
Pablo de Oliveira
|
r12572 | # If the cell uses a magic extension language, | ||
# use the magic language instead. | ||||
if language == 'ipython' \ | ||||
Pablo de Oliveira
|
r12577 | and metadata \ | ||
and 'magics_language' in metadata: | ||||
Pablo de Oliveira
|
r12572 | |||
Pablo de Oliveira
|
r12577 | language = metadata['magics_language'] | ||
Pablo de Oliveira
|
r12571 | |||
Jonathan Frederic
|
r10676 | if language == 'ipython': | ||
Jonathan Frederic
|
r10485 | lexer = IPythonLexer() | ||
else: | ||||
Jonathan Frederic
|
r10676 | lexer = get_lexer_by_name(language, stripall=True) | ||
Jonathan Frederic
|
r10485 | |||
Pablo de Oliveira
|
r12577 | return pygements_highlight(source, lexer, output_formatter) | ||