Show More
@@ -14,14 +14,11 b' from within Jinja templates.' | |||
|
14 | 14 | # Imports |
|
15 | 15 | #----------------------------------------------------------------------------- |
|
16 | 16 | |
|
17 | from pygments import highlight as pygements_highlight | |
|
18 | from pygments.lexers import get_lexer_by_name | |
|
19 | from pygments.formatters import HtmlFormatter | |
|
20 | from pygments.formatters import LatexFormatter | |
|
21 | ||
|
17 | # pygments must not be imported at the module level | |
|
18 | # because errors should be raised at runtime if it's actually needed, | |
|
19 | # not import time, when it may not be needed. | |
|
22 | 20 | |
|
23 | 21 | # Our own imports |
|
24 | from IPython.nbconvert.utils.lexers import IPythonLexer | |
|
25 | 22 | from IPython.nbconvert.utils.base import NbConvertBase |
|
26 | 23 | |
|
27 | 24 | #----------------------------------------------------------------------------- |
@@ -55,10 +52,11 b' class Highlight2Html(NbConvertBase):' | |||
|
55 | 52 | metadata : NotebookNode cell metadata |
|
56 | 53 | metadata of the cell to highlight |
|
57 | 54 | """ |
|
55 | from pygments.formatters import HtmlFormatter | |
|
58 | 56 | if not language: |
|
59 | 57 | language=self.default_language |
|
60 | 58 | |
|
61 | return _pygment_highlight(source, HtmlFormatter(), language, metadata) | |
|
59 | return _pygments_highlight(source, HtmlFormatter(), language, metadata) | |
|
62 | 60 | |
|
63 | 61 | |
|
64 | 62 | class Highlight2Latex(NbConvertBase): |
@@ -78,10 +76,11 b' class Highlight2Latex(NbConvertBase):' | |||
|
78 | 76 | strip_verbatim : bool |
|
79 | 77 | remove the Verbatim environment that pygments provides by default |
|
80 | 78 | """ |
|
79 | from pygments.formatters import LatexFormatter | |
|
81 | 80 | if not language: |
|
82 | 81 | language=self.default_language |
|
83 | 82 | |
|
84 | latex = _pygment_highlight(source, LatexFormatter(), language, metadata) | |
|
83 | latex = _pygments_highlight(source, LatexFormatter(), language, metadata) | |
|
85 | 84 | if strip_verbatim: |
|
86 | 85 | latex = latex.replace(r'\begin{Verbatim}[commandchars=\\\{\}]' + '\n', '') |
|
87 | 86 | return latex.replace('\n\\end{Verbatim}\n', '') |
@@ -90,7 +89,7 b' class Highlight2Latex(NbConvertBase):' | |||
|
90 | 89 | |
|
91 | 90 | |
|
92 | 91 | |
|
93 | def _pygment_highlight(source, output_formatter, language='ipython', metadata=None): | |
|
92 | def _pygments_highlight(source, output_formatter, language='ipython', metadata=None): | |
|
94 | 93 | """ |
|
95 | 94 | Return a syntax-highlighted version of the input source |
|
96 | 95 | |
@@ -104,6 +103,9 b" def _pygment_highlight(source, output_formatter, language='ipython', metadata=No" | |||
|
104 | 103 | metadata : NotebookNode cell metadata |
|
105 | 104 | metadata of the cell to highlight |
|
106 | 105 | """ |
|
106 | from pygments import highlight | |
|
107 | from pygments.lexers import get_lexer_by_name | |
|
108 | from IPython.nbconvert.utils.lexers import IPythonLexer | |
|
107 | 109 | |
|
108 | 110 | # If the cell uses a magic extension language, |
|
109 | 111 | # use the magic language instead. |
@@ -118,4 +120,4 b" def _pygment_highlight(source, output_formatter, language='ipython', metadata=No" | |||
|
118 | 120 | else: |
|
119 | 121 | lexer = get_lexer_by_name(language, stripall=True) |
|
120 | 122 | |
|
121 |
return |
|
|
123 | return highlight(source, lexer, output_formatter) |
@@ -15,8 +15,6 b'' | |||
|
15 | 15 | import os |
|
16 | 16 | import io |
|
17 | 17 | |
|
18 | from pygments.formatters import HtmlFormatter | |
|
19 | ||
|
20 | 18 | from IPython.utils import path |
|
21 | 19 | |
|
22 | 20 | from .base import Preprocessor |
@@ -83,6 +81,7 b' class CSSHTMLHeaderPreprocessor(Preprocessor):' | |||
|
83 | 81 | Fills self.header with lines of CSS extracted from IPython |
|
84 | 82 | and Pygments. |
|
85 | 83 | """ |
|
84 | from pygments.formatters import HtmlFormatter | |
|
86 | 85 | |
|
87 | 86 | #Clear existing header. |
|
88 | 87 | header = [] |
General Comments 0
You need to be logged in to leave comments.
Login now