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