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