##// END OF EJS Templates
Added 'jpeg' key
Added 'jpeg' key

File last commit:

r11089:45d39d22
r11393:47f5f90a
Show More
highlight.py
88 lines | 2.6 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
from pygments import highlight as pygements_highlight
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__ = [
'highlight',
'highlight2latex'
]
Jonathan Frederic
Cleanup and refactor of filters
r10676 def highlight(source, language='ipython'):
Jonathan Frederic
Post code-review, extended refactor.
r10485 """
Return a syntax-highlighted version of the input source as html output.
Jonathan Frederic
Cleanup and refactor of filters
r10676
Parameters
----------
source : str
Source code to highlight the syntax of.
language : str
Language to highlight the syntax of.
Jonathan Frederic
Post code-review, extended refactor.
r10485 """
Jonathan Frederic
Almost have nbconvert working again...
r10630
Jonathan Frederic
Cleanup and refactor of filters
r10676 return _pygment_highlight(source, HtmlFormatter(), language)
Jonathan Frederic
Post code-review, extended refactor.
r10485
Jonathan Frederic
Cleanup and refactor of filters
r10676 def highlight2latex(source, language='ipython'):
Jonathan Frederic
Post code-review, extended refactor.
r10485 """
Return a syntax-highlighted version of the input source as latex output.
Jonathan Frederic
Cleanup and refactor of filters
r10676
Parameters
----------
source : str
Source code to highlight the syntax of.
language : str
Language to highlight the syntax of.
Jonathan Frederic
Post code-review, extended refactor.
r10485 """
Jonathan Frederic
Cleanup and refactor of filters
r10676 return _pygment_highlight(source, LatexFormatter(), language)
Jonathan Frederic
Post code-review, extended refactor.
r10485
Jonathan Frederic
Cleanup and refactor of filters
r10676
def _pygment_highlight(source, output_formatter, language='ipython'):
Jonathan Frederic
Post code-review, extended refactor.
r10485 """
Return a syntax-highlighted version of the input source
Jonathan Frederic
Cleanup and refactor of filters
r10676
Parameters
----------
source : str
Source code to highlight the syntax of.
output_formatter : Pygments formatter
language : str
Language to highlight the syntax of.
Jonathan Frederic
Post code-review, extended refactor.
r10485 """
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
Jonathan Frederic
Cleanup and refactor of filters
r10676 return pygements_highlight(source, lexer, output_formatter)