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