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