##// END OF EJS Templates
Merge pull request #4682 from dbarbeau/syntax-highlight...
Min RK -
r16439:2444facf merge
parent child Browse files
Show More
@@ -1,123 +1,126 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) 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 if len(source) > 0 else ' ', HtmlFormatter(), language, metadata)
59 return _pygments_highlight(source if len(source) > 0 else ' ',
60 # needed to help post processors:
61 HtmlFormatter(cssclass="hl-"+language),
62 language, metadata)
60
63
61
64
62 class Highlight2Latex(NbConvertBase):
65 class Highlight2Latex(NbConvertBase):
63
66
64 def __call__(self, source, language=None, metadata=None, strip_verbatim=False):
67 def __call__(self, source, language=None, metadata=None, strip_verbatim=False):
65 """
68 """
66 Return a syntax-highlighted version of the input source as latex output.
69 Return a syntax-highlighted version of the input source as latex output.
67
70
68 Parameters
71 Parameters
69 ----------
72 ----------
70 source : str
73 source : str
71 source of the cell to highlight
74 source of the cell to highlight
72 language : str
75 language : str
73 language to highlight the syntax of
76 language to highlight the syntax of
74 metadata : NotebookNode cell metadata
77 metadata : NotebookNode cell metadata
75 metadata of the cell to highlight
78 metadata of the cell to highlight
76 strip_verbatim : bool
79 strip_verbatim : bool
77 remove the Verbatim environment that pygments provides by default
80 remove the Verbatim environment that pygments provides by default
78 """
81 """
79 from pygments.formatters import LatexFormatter
82 from pygments.formatters import LatexFormatter
80 if not language:
83 if not language:
81 language=self.default_language
84 language=self.default_language
82
85
83 latex = _pygments_highlight(source, LatexFormatter(), language, metadata)
86 latex = _pygments_highlight(source, LatexFormatter(), language, metadata)
84 if strip_verbatim:
87 if strip_verbatim:
85 latex = latex.replace(r'\begin{Verbatim}[commandchars=\\\{\}]' + '\n', '')
88 latex = latex.replace(r'\begin{Verbatim}[commandchars=\\\{\}]' + '\n', '')
86 return latex.replace('\n\\end{Verbatim}\n', '')
89 return latex.replace('\n\\end{Verbatim}\n', '')
87 else:
90 else:
88 return latex
91 return latex
89
92
90
93
91
94
92 def _pygments_highlight(source, output_formatter, language='ipython', metadata=None):
95 def _pygments_highlight(source, output_formatter, language='ipython', metadata=None):
93 """
96 """
94 Return a syntax-highlighted version of the input source
97 Return a syntax-highlighted version of the input source
95
98
96 Parameters
99 Parameters
97 ----------
100 ----------
98 source : str
101 source : str
99 source of the cell to highlight
102 source of the cell to highlight
100 output_formatter : Pygments formatter
103 output_formatter : Pygments formatter
101 language : str
104 language : str
102 language to highlight the syntax of
105 language to highlight the syntax of
103 metadata : NotebookNode cell metadata
106 metadata : NotebookNode cell metadata
104 metadata of the cell to highlight
107 metadata of the cell to highlight
105 """
108 """
106 from pygments import highlight
109 from pygments import highlight
107 from pygments.lexers import get_lexer_by_name
110 from pygments.lexers import get_lexer_by_name
108 from IPython.nbconvert.utils.lexers import IPythonLexer
111 from IPython.nbconvert.utils.lexers import IPythonLexer
109
112
110 # If the cell uses a magic extension language,
113 # If the cell uses a magic extension language,
111 # use the magic language instead.
114 # use the magic language instead.
112 if language == 'ipython' \
115 if language == 'ipython' \
113 and metadata \
116 and metadata \
114 and 'magics_language' in metadata:
117 and 'magics_language' in metadata:
115
118
116 language = metadata['magics_language']
119 language = metadata['magics_language']
117
120
118 if language == 'ipython':
121 if language == 'ipython':
119 lexer = IPythonLexer()
122 lexer = IPythonLexer()
120 else:
123 else:
121 lexer = get_lexer_by_name(language, stripall=True)
124 lexer = get_lexer_by_name(language, stripall=True)
122
125
123 return highlight(source, lexer, output_formatter)
126 return highlight(source, lexer, output_formatter)
General Comments 0
You need to be logged in to leave comments. Login now