##// END OF EJS Templates
Fix blank cell collapsing.
Jonathan Frederic -
Show More
@@ -1,123 +1,123 b''
1 1 """
2 2 Module containing filter functions that allow code to be highlighted
3 3 from within Jinja templates.
4 4 """
5 5 #-----------------------------------------------------------------------------
6 6 # Copyright (c) 2013, the IPython Development Team.
7 7 #
8 8 # Distributed under the terms of the Modified BSD License.
9 9 #
10 10 # The full license is in the file COPYING.txt, distributed with this software.
11 11 #-----------------------------------------------------------------------------
12 12
13 13 #-----------------------------------------------------------------------------
14 14 # Imports
15 15 #-----------------------------------------------------------------------------
16 16
17 17 # pygments must not be imported at the module level
18 18 # because errors should be raised at runtime if it's actually needed,
19 19 # not import time, when it may not be needed.
20 20
21 21 # Our own imports
22 22 from IPython.nbconvert.utils.base import NbConvertBase
23 23
24 24 #-----------------------------------------------------------------------------
25 25 # Globals and constants
26 26 #-----------------------------------------------------------------------------
27 27
28 28 MULTILINE_OUTPUTS = ['text', 'html', 'svg', 'latex', 'javascript', 'json']
29 29
30 30 #-----------------------------------------------------------------------------
31 31 # Utility functions
32 32 #-----------------------------------------------------------------------------
33 33
34 34 __all__ = [
35 35 'Highlight2Html',
36 36 'Highlight2Latex'
37 37 ]
38 38
39 39
40 40 class Highlight2Html(NbConvertBase):
41 41
42 42 def __call__(self, source, language=None, metadata=None):
43 43 """
44 44 Return a syntax-highlighted version of the input source as html output.
45 45
46 46 Parameters
47 47 ----------
48 48 source : str
49 49 source of the cell to highlight
50 50 language : str
51 51 language to highlight the syntax of
52 52 metadata : NotebookNode cell metadata
53 53 metadata of the cell to highlight
54 54 """
55 55 from pygments.formatters import HtmlFormatter
56 56 if not language:
57 57 language=self.default_language
58 58
59 return _pygments_highlight(source, HtmlFormatter(), language, metadata)
59 return _pygments_highlight(source if len(source) > 0 else ' ', HtmlFormatter(), language, metadata)
60 60
61 61
62 62 class Highlight2Latex(NbConvertBase):
63 63
64 64 def __call__(self, source, language=None, metadata=None, strip_verbatim=False):
65 65 """
66 66 Return a syntax-highlighted version of the input source as latex output.
67 67
68 68 Parameters
69 69 ----------
70 70 source : str
71 71 source of the cell to highlight
72 72 language : str
73 73 language to highlight the syntax of
74 74 metadata : NotebookNode cell metadata
75 75 metadata of the cell to highlight
76 76 strip_verbatim : bool
77 77 remove the Verbatim environment that pygments provides by default
78 78 """
79 79 from pygments.formatters import LatexFormatter
80 80 if not language:
81 81 language=self.default_language
82 82
83 83 latex = _pygments_highlight(source, LatexFormatter(), language, metadata)
84 84 if strip_verbatim:
85 85 latex = latex.replace(r'\begin{Verbatim}[commandchars=\\\{\}]' + '\n', '')
86 86 return latex.replace('\n\\end{Verbatim}\n', '')
87 87 else:
88 88 return latex
89 89
90 90
91 91
92 92 def _pygments_highlight(source, output_formatter, language='ipython', metadata=None):
93 93 """
94 94 Return a syntax-highlighted version of the input source
95 95
96 96 Parameters
97 97 ----------
98 98 source : str
99 99 source of the cell to highlight
100 100 output_formatter : Pygments formatter
101 101 language : str
102 102 language to highlight the syntax of
103 103 metadata : NotebookNode cell metadata
104 104 metadata of the cell to highlight
105 105 """
106 106 from pygments import highlight
107 107 from pygments.lexers import get_lexer_by_name
108 108 from IPython.nbconvert.utils.lexers import IPythonLexer
109 109
110 110 # If the cell uses a magic extension language,
111 111 # use the magic language instead.
112 112 if language == 'ipython' \
113 113 and metadata \
114 114 and 'magics_language' in metadata:
115 115
116 116 language = metadata['magics_language']
117 117
118 118 if language == 'ipython':
119 119 lexer = IPythonLexer()
120 120 else:
121 121 lexer = get_lexer_by_name(language, stripall=True)
122 122
123 123 return highlight(source, lexer, output_formatter)
General Comments 0
You need to be logged in to leave comments. Login now