##// END OF EJS Templates
Merge pull request #6284 from jasongrout/patch-6...
Merge pull request #6284 from jasongrout/patch-6 Default view should be the base widget view class

File last commit:

r16498:6cab76d8
r17626:4581d8b9 merge
Show More
highlight.py
108 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.
"""
MinRK
keep .highlight css class...
r16498 # Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.
Jonathan Frederic
Almost have nbconvert working again...
r10630
MinRK
never import pigments at module level in nbconvert...
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
Almost have nbconvert working again...
r10630
Matthias BUSSONNIER
LaTeX highlighter configurable, + tests
r13505 from IPython.nbconvert.utils.base import NbConvertBase
Jonathan Frederic
Post code-review, extended refactor.
r10485
Jonathan Frederic
Cleanup and refactor of filters
r10676 MULTILINE_OUTPUTS = ['text', 'html', 'svg', 'latex', 'javascript', 'json']
Jonathan Frederic
Post code-review, extended refactor.
r10485
Brian E. Granger
Fixing import logic.
r11088 __all__ = [
MinRK
fix HTML capitalization in Highlight2HTML...
r15767 'Highlight2HTML',
Matthias BUSSONNIER
LaTeX highlighter configurable, + tests
r13505 'Highlight2Latex'
Brian E. Granger
Fixing import logic.
r11088 ]
MinRK
fix HTML capitalization in Highlight2HTML...
r15767 class Highlight2HTML(NbConvertBase):
Matthias BUSSONNIER
make default language highlight configurable for html
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
never import pigments at module level in nbconvert...
r14042 from pygments.formatters import HtmlFormatter
Matthias BUSSONNIER
make default language highlight configurable for html
r13128 if not language:
Matthias BUSSONNIER
LaTeX highlighter configurable, + tests
r13505 language=self.default_language
Pablo de Oliveira
Select adequate highlighter for cell magic languages...
r12571
Daniel B. Vasquez
Pygment now adds a language-class to the highlighted html so that...
r16436 return _pygments_highlight(source if len(source) > 0 else ' ',
# needed to help post processors:
MinRK
keep .highlight css class...
r16498 HtmlFormatter(cssclass=" highlight hl-"+language),
Daniel B. Vasquez
Pygment now adds a language-class to the highlighted html so that...
r16436 language, metadata)
Jonathan Frederic
Cleanup and refactor of filters
r10676
Jonathan Frederic
Post code-review, extended refactor.
r10485
Matthias BUSSONNIER
LaTeX highlighter configurable, + tests
r13505 class Highlight2Latex(NbConvertBase):
Pablo de Oliveira
Select adequate highlighter for cell magic languages...
r12571
Matthias BUSSONNIER
LaTeX highlighter configurable, + tests
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
never import pigments at module level in nbconvert...
r14042 from pygments.formatters import LatexFormatter
Matthias BUSSONNIER
LaTeX highlighter configurable, + tests
r13505 if not language:
language=self.default_language
MinRK
never import pigments at module level in nbconvert...
r14042 latex = _pygments_highlight(source, LatexFormatter(), language, metadata)
Matthias BUSSONNIER
LaTeX highlighter configurable, + tests
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
Select adequate highlighter for cell magic languages...
r12571
MinRK
never import pigments at module level in nbconvert...
r14042 def _pygments_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 """
MinRK
never import pigments at module level in nbconvert...
r14042 from pygments import highlight
from pygments.lexers import get_lexer_by_name
from IPython.nbconvert.utils.lexers import IPythonLexer
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
MinRK
never import pigments at module level in nbconvert...
r14042 return highlight(source, lexer, output_formatter)