##// END OF EJS Templates
Add strip_verbatim to latex highlight
Jonathan Frederic -
Show More
@@ -1,102 +1,110 b''
1 """
1 """
2 Module containing filter functions that allow code to be highlighted
2 Module containing filter functions that allow code to be highlighted
3 from within Jinja templates.
3 from within Jinja templates.
4 """
4 """
5 #-----------------------------------------------------------------------------
5 #-----------------------------------------------------------------------------
6 # Copyright (c) 2013, the IPython Development Team.
6 # Copyright (c) 2013, the IPython Development Team.
7 #
7 #
8 # Distributed under the terms of the Modified BSD License.
8 # Distributed under the terms of the Modified BSD License.
9 #
9 #
10 # The full license is in the file COPYING.txt, distributed with this software.
10 # The full license is in the file COPYING.txt, distributed with this software.
11 #-----------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
12
12
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14 # Imports
14 # Imports
15 #-----------------------------------------------------------------------------
15 #-----------------------------------------------------------------------------
16
16
17 from pygments import highlight as pygements_highlight
17 from pygments import highlight as pygements_highlight
18 from pygments.lexers import get_lexer_by_name
18 from pygments.lexers import get_lexer_by_name
19 from pygments.formatters import HtmlFormatter
19 from pygments.formatters import HtmlFormatter
20 from pygments.formatters import LatexFormatter
20 from pygments.formatters import LatexFormatter
21
21
22 # Our own imports
22 # Our own imports
23 from IPython.nbconvert.utils.lexers import IPythonLexer
23 from IPython.nbconvert.utils.lexers import IPythonLexer
24
24
25 #-----------------------------------------------------------------------------
25 #-----------------------------------------------------------------------------
26 # Globals and constants
26 # Globals and constants
27 #-----------------------------------------------------------------------------
27 #-----------------------------------------------------------------------------
28
28
29 MULTILINE_OUTPUTS = ['text', 'html', 'svg', 'latex', 'javascript', 'json']
29 MULTILINE_OUTPUTS = ['text', 'html', 'svg', 'latex', 'javascript', 'json']
30
30
31 #-----------------------------------------------------------------------------
31 #-----------------------------------------------------------------------------
32 # Utility functions
32 # Utility functions
33 #-----------------------------------------------------------------------------
33 #-----------------------------------------------------------------------------
34
34
35 __all__ = [
35 __all__ = [
36 'highlight2html',
36 'highlight2html',
37 'highlight2latex'
37 'highlight2latex'
38 ]
38 ]
39
39
40
40 def highlight2html(source, language='ipython', metadata=None):
41 def highlight2html(source, language='ipython', metadata=None):
41 """
42 """
42 Return a syntax-highlighted version of the input source as html output.
43 Return a syntax-highlighted version of the input source as html output.
43
44
44 Parameters
45 Parameters
45 ----------
46 ----------
46 source : str
47 source : str
47 source of the cell to highlight.
48 source of the cell to highlight
48 language : str
49 language : str
49 Language to highlight the syntax of.
50 language to highlight the syntax of
50 metadata : NotebookNode cell metadata
51 metadata : NotebookNode cell metadata
51 metadata of the cell to highlight.
52 metadata of the cell to highlight
52 """
53 """
53
54
54 return _pygment_highlight(source, HtmlFormatter(), language, metadata)
55 return _pygment_highlight(source, HtmlFormatter(), language, metadata)
55
56
56
57
57 def highlight2latex(source, language='ipython', metadata=None):
58 def highlight2latex(source, language='ipython', metadata=None, strip_verbatim=False):
58 """
59 """
59 Return a syntax-highlighted version of the input source as latex output.
60 Return a syntax-highlighted version of the input source as latex output.
60
61
61 Parameters
62 Parameters
62 ----------
63 ----------
63 source : str
64 source : str
64 source of the cell to highlight.
65 source of the cell to highlight
65 language : str
66 language : str
66 Language to highlight the syntax of.
67 language to highlight the syntax of
67 metadata : NotebookNode cell metadata
68 metadata : NotebookNode cell metadata
68 metadata of the cell to highlight.
69 metadata of the cell to highlight
70 strip_verbatim : bool
71 remove the Verbatim environment that pygments provides by default
69 """
72 """
70 return _pygment_highlight(source, LatexFormatter(), language, metadata)
73 latex = _pygment_highlight(source, LatexFormatter(), language, metadata)
74 if strip_verbatim:
75 latex = latex.replace(r'\begin{Verbatim}[commandchars=\\\{\}]' + '\n', '')
76 return latex.replace('\n\\end{Verbatim}\n', '')
77 else:
78 return latex
71
79
72
80
73
81
74 def _pygment_highlight(source, output_formatter, language='ipython', metadata=None):
82 def _pygment_highlight(source, output_formatter, language='ipython', metadata=None):
75 """
83 """
76 Return a syntax-highlighted version of the input source
84 Return a syntax-highlighted version of the input source
77
85
78 Parameters
86 Parameters
79 ----------
87 ----------
80 source : str
88 source : str
81 source of the cell to highlight.
89 source of the cell to highlight
82 output_formatter : Pygments formatter
90 output_formatter : Pygments formatter
83 language : str
91 language : str
84 Language to highlight the syntax of.
92 language to highlight the syntax of
85 metadata : NotebookNode cell metadata
93 metadata : NotebookNode cell metadata
86 metadata of the cell to highlight.
94 metadata of the cell to highlight
87 """
95 """
88
96
89 # If the cell uses a magic extension language,
97 # If the cell uses a magic extension language,
90 # use the magic language instead.
98 # use the magic language instead.
91 if language == 'ipython' \
99 if language == 'ipython' \
92 and metadata \
100 and metadata \
93 and 'magics_language' in metadata:
101 and 'magics_language' in metadata:
94
102
95 language = metadata['magics_language']
103 language = metadata['magics_language']
96
104
97 if language == 'ipython':
105 if language == 'ipython':
98 lexer = IPythonLexer()
106 lexer = IPythonLexer()
99 else:
107 else:
100 lexer = get_lexer_by_name(language, stripall=True)
108 lexer = get_lexer_by_name(language, stripall=True)
101
109
102 return pygements_highlight(source, lexer, output_formatter)
110 return pygements_highlight(source, lexer, output_formatter)
@@ -1,21 +1,21 b''
1 ((= Python input/output style =))
1 ((= Python input/output style =))
2
2
3 ((*- extends 'latex_base.tplx' -*))
3 ((*- extends 'latex_base.tplx' -*))
4
4
5 % Custom definitions
5 % Custom definitions
6 ((* block definitions *))
6 ((* block definitions *))
7 ((( super() )))
7 ((( super() )))
8
8
9 % Pygments definitions
9 % Pygments definitions
10 ((( resources.latex.pygment_definitions )))
10 ((( resources.latex.pygment_definitions )))
11 ((* endblock definitions *))
11 ((* endblock definitions *))
12
12
13 %===============================================================================
13 %===============================================================================
14 % Input
14 % Input
15 %===============================================================================
15 %===============================================================================
16
16
17 ((* block input scoped *))
17 ((* block input scoped *))
18 \begin{Verbatim}[commandchars=\\\{\}]
18 \begin{Verbatim}[commandchars=\\\{\}]
19 ((( cell.input | highlight2latex | replace('\\begin{Verbatim}[commandchars=\\\\\\{\\}]\n', '') | replace('\n\\end{Verbatim}\n', '') | add_prompts )))
19 ((( cell.input | highlight2latex(strip_verbatim=True) | add_prompts )))
20 \end{Verbatim}
20 \end{Verbatim}
21 ((* endblock input *))
21 ((* endblock input *))
General Comments 0
You need to be logged in to leave comments. Login now