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