highlight.py
135 lines
| 4.5 KiB
| text/x-python
|
PythonLexer
Jonathan Frederic
|
r10676 | """ | ||
Module containing filter functions that allow code to be highlighted | ||||
from within Jinja templates. | ||||
""" | ||||
MinRK
|
r16498 | # Copyright (c) IPython Development Team. | ||
# Distributed under the terms of the Modified BSD License. | ||||
Jonathan Frederic
|
r10630 | |||
MinRK
|
r14042 | # 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. | ||||
Jonathan Frederic
|
r10630 | |||
Matthias BUSSONNIER
|
r13505 | from IPython.nbconvert.utils.base import NbConvertBase | ||
Thomas Kluyver
|
r18381 | from warnings import warn | ||
Jonathan Frederic
|
r10485 | |||
Jonathan Frederic
|
r10676 | MULTILINE_OUTPUTS = ['text', 'html', 'svg', 'latex', 'javascript', 'json'] | ||
Jonathan Frederic
|
r10485 | |||
Brian E. Granger
|
r11088 | __all__ = [ | ||
MinRK
|
r15767 | 'Highlight2HTML', | ||
Matthias BUSSONNIER
|
r13505 | 'Highlight2Latex' | ||
Brian E. Granger
|
r11088 | ] | ||
MinRK
|
r15767 | class Highlight2HTML(NbConvertBase): | ||
Thomas Kluyver
|
r18381 | def __init__(self, pygments_lexer=None, **kwargs): | ||
self.pygments_lexer = pygments_lexer or 'ipython3' | ||||
super(Highlight2HTML, self).__init__(**kwargs) | ||||
def _default_language_changed(self, name, old, new): | ||||
warn('Setting default_language in config is deprecated, ' | ||||
'please use kernelspecs instead.') | ||||
self.pygments_lexer = new | ||||
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 | ||||
""" | ||||
MinRK
|
r14042 | from pygments.formatters import HtmlFormatter | ||
Thomas Kluyver
|
r18381 | |||
Matthias BUSSONNIER
|
r13128 | if not language: | ||
Thomas Kluyver
|
r18381 | language=self.pygments_lexer | ||
Pablo de Oliveira
|
r12571 | |||
Daniel B. Vasquez
|
r16436 | return _pygments_highlight(source if len(source) > 0 else ' ', | ||
# needed to help post processors: | ||||
MinRK
|
r16498 | HtmlFormatter(cssclass=" highlight hl-"+language), | ||
Daniel B. Vasquez
|
r16436 | language, metadata) | ||
Jonathan Frederic
|
r10676 | |||
Jonathan Frederic
|
r10485 | |||
Matthias BUSSONNIER
|
r13505 | class Highlight2Latex(NbConvertBase): | ||
Thomas Kluyver
|
r18381 | def __init__(self, pygments_lexer=None, **kwargs): | ||
self.pygments_lexer = pygments_lexer or 'ipython3' | ||||
super(Highlight2Latex, self).__init__(**kwargs) | ||||
def _default_language_changed(self, name, old, new): | ||||
warn('Setting default_language in config is deprecated, ' | ||||
'please use kernelspecs instead.') | ||||
self.pygments_lexer = new | ||||
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 | ||||
""" | ||||
MinRK
|
r14042 | from pygments.formatters import LatexFormatter | ||
Matthias BUSSONNIER
|
r13505 | if not language: | ||
Thomas Kluyver
|
r18381 | language=self.pygments_lexer | ||
Matthias BUSSONNIER
|
r13505 | |||
MinRK
|
r14042 | latex = _pygments_highlight(source, LatexFormatter(), language, metadata) | ||
Matthias BUSSONNIER
|
r13505 | 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 | |||
MinRK
|
r14042 | def _pygments_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 | """ | ||
MinRK
|
r14042 | from pygments import highlight | ||
from pygments.lexers import get_lexer_by_name | ||||
Thomas Kluyver
|
r18399 | from pygments.util import ClassNotFound | ||
Thomas Kluyver
|
r18381 | from IPython.nbconvert.utils.lexers import IPythonLexer, IPython3Lexer | ||
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 | |||
Thomas Kluyver
|
r18381 | if language == 'ipython2': | ||
Jonathan Frederic
|
r10485 | lexer = IPythonLexer() | ||
Thomas Kluyver
|
r18381 | elif language == 'ipython3': | ||
lexer = IPython3Lexer() | ||||
Jonathan Frederic
|
r10485 | else: | ||
Thomas Kluyver
|
r18399 | try: | ||
lexer = get_lexer_by_name(language, stripall=True) | ||||
except ClassNotFound: | ||||
warn("No lexer found for language %r. Treating as plain text." % language) | ||||
from pygments.lexers.special import TextLexer | ||||
lexer = TextLexer() | ||||
Jonathan Frederic
|
r10485 | |||
MinRK
|
r14042 | return highlight(source, lexer, output_formatter) | ||