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