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