Show More
@@ -1,532 +1,526 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 | """ |
|
2 | """ | |
3 | Defines a variety of Pygments lexers for highlighting IPython code. |
|
3 | Defines a variety of Pygments lexers for highlighting IPython code. | |
4 |
|
4 | |||
5 | This includes: |
|
5 | This includes: | |
6 |
|
6 | |||
7 | IPythonLexer, IPython3Lexer |
|
7 | IPythonLexer, IPython3Lexer | |
8 | Lexers for pure IPython (python + magic/shell commands) |
|
8 | Lexers for pure IPython (python + magic/shell commands) | |
9 |
|
9 | |||
10 | IPythonPartialTracebackLexer, IPythonTracebackLexer |
|
10 | IPythonPartialTracebackLexer, IPythonTracebackLexer | |
11 | Supports 2.x and 3.x via keyword `python3`. The partial traceback |
|
11 | Supports 2.x and 3.x via keyword `python3`. The partial traceback | |
12 | lexer reads everything but the Python code appearing in a traceback. |
|
12 | lexer reads everything but the Python code appearing in a traceback. | |
13 | The full lexer combines the partial lexer with an IPython lexer. |
|
13 | The full lexer combines the partial lexer with an IPython lexer. | |
14 |
|
14 | |||
15 | IPythonConsoleLexer |
|
15 | IPythonConsoleLexer | |
16 | A lexer for IPython console sessions, with support for tracebacks. |
|
16 | A lexer for IPython console sessions, with support for tracebacks. | |
17 |
|
17 | |||
18 | IPyLexer |
|
18 | IPyLexer | |
19 | A friendly lexer which examines the first line of text and from it, |
|
19 | A friendly lexer which examines the first line of text and from it, | |
20 | decides whether to use an IPython lexer or an IPython console lexer. |
|
20 | decides whether to use an IPython lexer or an IPython console lexer. | |
21 | This is probably the only lexer that needs to be explicitly added |
|
21 | This is probably the only lexer that needs to be explicitly added | |
22 | to Pygments. |
|
22 | to Pygments. | |
23 |
|
23 | |||
24 | """ |
|
24 | """ | |
25 | #----------------------------------------------------------------------------- |
|
25 | #----------------------------------------------------------------------------- | |
26 | # Copyright (c) 2013, the IPython Development Team. |
|
26 | # Copyright (c) 2013, the IPython Development Team. | |
27 | # |
|
27 | # | |
28 | # Distributed under the terms of the Modified BSD License. |
|
28 | # Distributed under the terms of the Modified BSD License. | |
29 | # |
|
29 | # | |
30 | # The full license is in the file COPYING.txt, distributed with this software. |
|
30 | # The full license is in the file COPYING.txt, distributed with this software. | |
31 | #----------------------------------------------------------------------------- |
|
31 | #----------------------------------------------------------------------------- | |
32 |
|
32 | |||
33 | # Standard library |
|
33 | # Standard library | |
34 | import re |
|
34 | import re | |
35 |
|
35 | |||
36 | # Third party |
|
36 | # Third party | |
37 | from pygments.lexers import ( |
|
37 | from pygments.lexers import ( | |
38 | BashLexer, HtmlLexer, JavascriptLexer, RubyLexer, PerlLexer, PythonLexer, |
|
38 | BashLexer, HtmlLexer, JavascriptLexer, RubyLexer, PerlLexer, PythonLexer, | |
39 | Python3Lexer, TexLexer) |
|
39 | Python3Lexer, TexLexer) | |
40 | from pygments.lexer import ( |
|
40 | from pygments.lexer import ( | |
41 | Lexer, DelegatingLexer, RegexLexer, do_insertions, bygroups, using, |
|
41 | Lexer, DelegatingLexer, RegexLexer, do_insertions, bygroups, using, | |
42 | ) |
|
42 | ) | |
43 | from pygments.token import ( |
|
43 | from pygments.token import ( | |
44 | Generic, Keyword, Literal, Name, Operator, Other, Text, Error, |
|
44 | Generic, Keyword, Literal, Name, Operator, Other, Text, Error, | |
45 | ) |
|
45 | ) | |
46 | from pygments.util import get_bool_opt |
|
46 | from pygments.util import get_bool_opt | |
47 |
|
47 | |||
48 | # Local |
|
48 | # Local | |
49 |
|
49 | |||
50 | line_re = re.compile('.*?\n') |
|
50 | line_re = re.compile('.*?\n') | |
51 |
|
51 | |||
52 | __all__ = ['build_ipy_lexer', 'IPython3Lexer', 'IPythonLexer', |
|
52 | __all__ = ['build_ipy_lexer', 'IPython3Lexer', 'IPythonLexer', | |
53 | 'IPythonPartialTracebackLexer', 'IPythonTracebackLexer', |
|
53 | 'IPythonPartialTracebackLexer', 'IPythonTracebackLexer', | |
54 | 'IPythonConsoleLexer', 'IPyLexer'] |
|
54 | 'IPythonConsoleLexer', 'IPyLexer'] | |
55 |
|
55 | |||
56 |
|
56 | |||
57 | def build_ipy_lexer(python3): |
|
57 | def build_ipy_lexer(python3): | |
58 | """Builds IPython lexers depending on the value of `python3`. |
|
58 | """Builds IPython lexers depending on the value of `python3`. | |
59 |
|
59 | |||
60 | The lexer inherits from an appropriate Python lexer and then adds |
|
60 | The lexer inherits from an appropriate Python lexer and then adds | |
61 | information about IPython specific keywords (i.e. magic commands, |
|
61 | information about IPython specific keywords (i.e. magic commands, | |
62 | shell commands, etc.) |
|
62 | shell commands, etc.) | |
63 |
|
63 | |||
64 | Parameters |
|
64 | Parameters | |
65 | ---------- |
|
65 | ---------- | |
66 | python3 : bool |
|
66 | python3 : bool | |
67 | If `True`, then build an IPython lexer from a Python 3 lexer. |
|
67 | If `True`, then build an IPython lexer from a Python 3 lexer. | |
68 |
|
68 | |||
69 | """ |
|
69 | """ | |
70 | # It would be nice to have a single IPython lexer class which takes |
|
70 | # It would be nice to have a single IPython lexer class which takes | |
71 | # a boolean `python3`. But since there are two Python lexer classes, |
|
71 | # a boolean `python3`. But since there are two Python lexer classes, | |
72 | # we will also have two IPython lexer classes. |
|
72 | # we will also have two IPython lexer classes. | |
73 | if python3: |
|
73 | if python3: | |
74 | PyLexer = Python3Lexer |
|
74 | PyLexer = Python3Lexer | |
75 | name = 'IPython3' |
|
75 | name = 'IPython3' | |
76 | aliases = ['ipython3'] |
|
76 | aliases = ['ipython3'] | |
77 | doc = """IPython3 Lexer""" |
|
77 | doc = """IPython3 Lexer""" | |
78 | else: |
|
78 | else: | |
79 | PyLexer = PythonLexer |
|
79 | PyLexer = PythonLexer | |
80 | name = 'IPython' |
|
80 | name = 'IPython' | |
81 | aliases = ['ipython2', 'ipython'] |
|
81 | aliases = ['ipython2', 'ipython'] | |
82 | doc = """IPython Lexer""" |
|
82 | doc = """IPython Lexer""" | |
83 |
|
83 | |||
84 | ipython_tokens = [ |
|
84 | ipython_tokens = [ | |
85 | (r'(?s)(\s*)(%%capture)([^\n]*\n)(.*)', bygroups(Text, Operator, Text, using(PyLexer))), |
|
85 | (r'(?s)(\s*)(%%capture)([^\n]*\n)(.*)', bygroups(Text, Operator, Text, using(PyLexer))), | |
86 | (r'(?s)(\s*)(%%debug)([^\n]*\n)(.*)', bygroups(Text, Operator, Text, using(PyLexer))), |
|
86 | (r'(?s)(\s*)(%%debug)([^\n]*\n)(.*)', bygroups(Text, Operator, Text, using(PyLexer))), | |
87 | (r'(?is)(\s*)(%%html)([^\n]*\n)(.*)', bygroups(Text, Operator, Text, using(HtmlLexer))), |
|
87 | (r'(?is)(\s*)(%%html)([^\n]*\n)(.*)', bygroups(Text, Operator, Text, using(HtmlLexer))), | |
88 | (r'(?s)(\s*)(%%javascript)([^\n]*\n)(.*)', bygroups(Text, Operator, Text, using(JavascriptLexer))), |
|
88 | (r'(?s)(\s*)(%%javascript)([^\n]*\n)(.*)', bygroups(Text, Operator, Text, using(JavascriptLexer))), | |
89 | (r'(?s)(\s*)(%%js)([^\n]*\n)(.*)', bygroups(Text, Operator, Text, using(JavascriptLexer))), |
|
89 | (r'(?s)(\s*)(%%js)([^\n]*\n)(.*)', bygroups(Text, Operator, Text, using(JavascriptLexer))), | |
90 | (r'(?s)(\s*)(%%latex)([^\n]*\n)(.*)', bygroups(Text, Operator, Text, using(TexLexer))), |
|
90 | (r'(?s)(\s*)(%%latex)([^\n]*\n)(.*)', bygroups(Text, Operator, Text, using(TexLexer))), | |
91 | (r'(?s)(\s*)(%%perl)([^\n]*\n)(.*)', bygroups(Text, Operator, Text, using(PerlLexer))), |
|
91 | (r'(?s)(\s*)(%%perl)([^\n]*\n)(.*)', bygroups(Text, Operator, Text, using(PerlLexer))), | |
92 | (r'(?s)(\s*)(%%prun)([^\n]*\n)(.*)', bygroups(Text, Operator, Text, using(PyLexer))), |
|
92 | (r'(?s)(\s*)(%%prun)([^\n]*\n)(.*)', bygroups(Text, Operator, Text, using(PyLexer))), | |
93 | (r'(?s)(\s*)(%%pypy)([^\n]*\n)(.*)', bygroups(Text, Operator, Text, using(PyLexer))), |
|
93 | (r'(?s)(\s*)(%%pypy)([^\n]*\n)(.*)', bygroups(Text, Operator, Text, using(PyLexer))), | |
94 | (r'(?s)(\s*)(%%python)([^\n]*\n)(.*)', bygroups(Text, Operator, Text, using(PyLexer))), |
|
94 | (r'(?s)(\s*)(%%python)([^\n]*\n)(.*)', bygroups(Text, Operator, Text, using(PyLexer))), | |
95 | (r'(?s)(\s*)(%%python2)([^\n]*\n)(.*)', bygroups(Text, Operator, Text, using(PythonLexer))), |
|
95 | (r'(?s)(\s*)(%%python2)([^\n]*\n)(.*)', bygroups(Text, Operator, Text, using(PythonLexer))), | |
96 | (r'(?s)(\s*)(%%python3)([^\n]*\n)(.*)', bygroups(Text, Operator, Text, using(Python3Lexer))), |
|
96 | (r'(?s)(\s*)(%%python3)([^\n]*\n)(.*)', bygroups(Text, Operator, Text, using(Python3Lexer))), | |
97 | (r'(?s)(\s*)(%%ruby)([^\n]*\n)(.*)', bygroups(Text, Operator, Text, using(RubyLexer))), |
|
97 | (r'(?s)(\s*)(%%ruby)([^\n]*\n)(.*)', bygroups(Text, Operator, Text, using(RubyLexer))), | |
98 | (r'(?s)(\s*)(%%time)([^\n]*\n)(.*)', bygroups(Text, Operator, Text, using(PyLexer))), |
|
98 | (r'(?s)(\s*)(%%time)([^\n]*\n)(.*)', bygroups(Text, Operator, Text, using(PyLexer))), | |
99 | (r'(?s)(\s*)(%%timeit)([^\n]*\n)(.*)', bygroups(Text, Operator, Text, using(PyLexer))), |
|
99 | (r'(?s)(\s*)(%%timeit)([^\n]*\n)(.*)', bygroups(Text, Operator, Text, using(PyLexer))), | |
100 | (r'(?s)(\s*)(%%writefile)([^\n]*\n)(.*)', bygroups(Text, Operator, Text, using(PyLexer))), |
|
100 | (r'(?s)(\s*)(%%writefile)([^\n]*\n)(.*)', bygroups(Text, Operator, Text, using(PyLexer))), | |
101 | (r'(?s)(\s*)(%%file)([^\n]*\n)(.*)', bygroups(Text, Operator, Text, using(PyLexer))), |
|
101 | (r'(?s)(\s*)(%%file)([^\n]*\n)(.*)', bygroups(Text, Operator, Text, using(PyLexer))), | |
102 | (r"(?s)(\s*)(%%)(\w+)(.*)", bygroups(Text, Operator, Keyword, Text)), |
|
102 | (r"(?s)(\s*)(%%)(\w+)(.*)", bygroups(Text, Operator, Keyword, Text)), | |
103 | (r'(?s)(^\s*)(%%!)([^\n]*\n)(.*)', bygroups(Text, Operator, Text, using(BashLexer))), |
|
103 | (r'(?s)(^\s*)(%%!)([^\n]*\n)(.*)', bygroups(Text, Operator, Text, using(BashLexer))), | |
104 | (r"(%%?)(\w+)(\?\??)$", bygroups(Operator, Keyword, Operator)), |
|
104 | (r"(%%?)(\w+)(\?\??)$", bygroups(Operator, Keyword, Operator)), | |
105 | (r"\b(\?\??)(\s*)$", bygroups(Operator, Text)), |
|
105 | (r"\b(\?\??)(\s*)$", bygroups(Operator, Text)), | |
106 | (r'(%)(sx|sc|system)(.*)(\n)', bygroups(Operator, Keyword, |
|
106 | (r'(%)(sx|sc|system)(.*)(\n)', bygroups(Operator, Keyword, | |
107 | using(BashLexer), Text)), |
|
107 | using(BashLexer), Text)), | |
108 | (r'(%)(\w+)(.*\n)', bygroups(Operator, Keyword, Text)), |
|
108 | (r'(%)(\w+)(.*\n)', bygroups(Operator, Keyword, Text)), | |
109 | (r'^(!!)(.+)(\n)', bygroups(Operator, using(BashLexer), Text)), |
|
109 | (r'^(!!)(.+)(\n)', bygroups(Operator, using(BashLexer), Text)), | |
110 | (r'(!)(?!=)(.+)(\n)', bygroups(Operator, using(BashLexer), Text)), |
|
110 | (r'(!)(?!=)(.+)(\n)', bygroups(Operator, using(BashLexer), Text)), | |
111 | (r'^(\s*)(\?\??)(\s*%{0,2}[\w\.\*]*)', bygroups(Text, Operator, Text)), |
|
111 | (r'^(\s*)(\?\??)(\s*%{0,2}[\w\.\*]*)', bygroups(Text, Operator, Text)), | |
112 | (r'(\s*%{0,2}[\w\.\*]*)(\?\??)(\s*)$', bygroups(Text, Operator, Text)), |
|
112 | (r'(\s*%{0,2}[\w\.\*]*)(\?\??)(\s*)$', bygroups(Text, Operator, Text)), | |
113 | ] |
|
113 | ] | |
114 |
|
114 | |||
115 | tokens = PyLexer.tokens.copy() |
|
115 | tokens = PyLexer.tokens.copy() | |
116 | tokens['root'] = ipython_tokens + tokens['root'] |
|
116 | tokens['root'] = ipython_tokens + tokens['root'] | |
117 |
|
117 | |||
118 | attrs = {'name': name, 'aliases': aliases, 'filenames': [], |
|
118 | attrs = {'name': name, 'aliases': aliases, 'filenames': [], | |
119 | '__doc__': doc, 'tokens': tokens} |
|
119 | '__doc__': doc, 'tokens': tokens} | |
120 |
|
120 | |||
121 | return type(name, (PyLexer,), attrs) |
|
121 | return type(name, (PyLexer,), attrs) | |
122 |
|
122 | |||
123 |
|
123 | |||
124 | IPython3Lexer = build_ipy_lexer(python3=True) |
|
124 | IPython3Lexer = build_ipy_lexer(python3=True) | |
125 | IPythonLexer = build_ipy_lexer(python3=False) |
|
125 | IPythonLexer = build_ipy_lexer(python3=False) | |
126 |
|
126 | |||
127 |
|
127 | |||
128 | class IPythonPartialTracebackLexer(RegexLexer): |
|
128 | class IPythonPartialTracebackLexer(RegexLexer): | |
129 | """ |
|
129 | """ | |
130 | Partial lexer for IPython tracebacks. |
|
130 | Partial lexer for IPython tracebacks. | |
131 |
|
131 | |||
132 | Handles all the non-python output. |
|
132 | Handles all the non-python output. | |
133 |
|
133 | |||
134 | """ |
|
134 | """ | |
135 | name = 'IPython Partial Traceback' |
|
135 | name = 'IPython Partial Traceback' | |
136 |
|
136 | |||
137 | tokens = { |
|
137 | tokens = { | |
138 | 'root': [ |
|
138 | 'root': [ | |
139 | # Tracebacks for syntax errors have a different style. |
|
139 | # Tracebacks for syntax errors have a different style. | |
140 | # For both types of tracebacks, we mark the first line with |
|
140 | # For both types of tracebacks, we mark the first line with | |
141 | # Generic.Traceback. For syntax errors, we mark the filename |
|
141 | # Generic.Traceback. For syntax errors, we mark the filename | |
142 | # as we mark the filenames for non-syntax tracebacks. |
|
142 | # as we mark the filenames for non-syntax tracebacks. | |
143 | # |
|
143 | # | |
144 | # These two regexps define how IPythonConsoleLexer finds a |
|
144 | # These two regexps define how IPythonConsoleLexer finds a | |
145 | # traceback. |
|
145 | # traceback. | |
146 | # |
|
146 | # | |
147 | ## Non-syntax traceback |
|
147 | ## Non-syntax traceback | |
148 | (r'^(\^C)?(-+\n)', bygroups(Error, Generic.Traceback)), |
|
148 | (r'^(\^C)?(-+\n)', bygroups(Error, Generic.Traceback)), | |
149 | ## Syntax traceback |
|
149 | ## Syntax traceback | |
150 | (r'^( File)(.*)(, line )(\d+\n)', |
|
150 | (r'^( File)(.*)(, line )(\d+\n)', | |
151 | bygroups(Generic.Traceback, Name.Namespace, |
|
151 | bygroups(Generic.Traceback, Name.Namespace, | |
152 | Generic.Traceback, Literal.Number.Integer)), |
|
152 | Generic.Traceback, Literal.Number.Integer)), | |
153 |
|
153 | |||
154 | # (Exception Identifier)(Whitespace)(Traceback Message) |
|
154 | # (Exception Identifier)(Whitespace)(Traceback Message) | |
155 | (r'(?u)(^[^\d\W]\w*)(\s*)(Traceback.*?\n)', |
|
155 | (r'(?u)(^[^\d\W]\w*)(\s*)(Traceback.*?\n)', | |
156 | bygroups(Name.Exception, Generic.Whitespace, Text)), |
|
156 | bygroups(Name.Exception, Generic.Whitespace, Text)), | |
157 | # (Module/Filename)(Text)(Callee)(Function Signature) |
|
157 | # (Module/Filename)(Text)(Callee)(Function Signature) | |
158 | # Better options for callee and function signature? |
|
158 | # Better options for callee and function signature? | |
159 | (r'(.*)( in )(.*)(\(.*\)\n)', |
|
159 | (r'(.*)( in )(.*)(\(.*\)\n)', | |
160 | bygroups(Name.Namespace, Text, Name.Entity, Name.Tag)), |
|
160 | bygroups(Name.Namespace, Text, Name.Entity, Name.Tag)), | |
161 | # Regular line: (Whitespace)(Line Number)(Python Code) |
|
161 | # Regular line: (Whitespace)(Line Number)(Python Code) | |
162 | (r'(\s*?)(\d+)(.*?\n)', |
|
162 | (r'(\s*?)(\d+)(.*?\n)', | |
163 | bygroups(Generic.Whitespace, Literal.Number.Integer, Other)), |
|
163 | bygroups(Generic.Whitespace, Literal.Number.Integer, Other)), | |
164 | # Emphasized line: (Arrow)(Line Number)(Python Code) |
|
164 | # Emphasized line: (Arrow)(Line Number)(Python Code) | |
165 | # Using Exception token so arrow color matches the Exception. |
|
165 | # Using Exception token so arrow color matches the Exception. | |
166 | (r'(-*>?\s?)(\d+)(.*?\n)', |
|
166 | (r'(-*>?\s?)(\d+)(.*?\n)', | |
167 | bygroups(Name.Exception, Literal.Number.Integer, Other)), |
|
167 | bygroups(Name.Exception, Literal.Number.Integer, Other)), | |
168 | # (Exception Identifier)(Message) |
|
168 | # (Exception Identifier)(Message) | |
169 | (r'(?u)(^[^\d\W]\w*)(:.*?\n)', |
|
169 | (r'(?u)(^[^\d\W]\w*)(:.*?\n)', | |
170 | bygroups(Name.Exception, Text)), |
|
170 | bygroups(Name.Exception, Text)), | |
171 | # Tag everything else as Other, will be handled later. |
|
171 | # Tag everything else as Other, will be handled later. | |
172 | (r'.*\n', Other), |
|
172 | (r'.*\n', Other), | |
173 | ], |
|
173 | ], | |
174 | } |
|
174 | } | |
175 |
|
175 | |||
176 |
|
176 | |||
177 | class IPythonTracebackLexer(DelegatingLexer): |
|
177 | class IPythonTracebackLexer(DelegatingLexer): | |
178 | """ |
|
178 | """ | |
179 | IPython traceback lexer. |
|
179 | IPython traceback lexer. | |
180 |
|
180 | |||
181 | For doctests, the tracebacks can be snipped as much as desired with the |
|
181 | For doctests, the tracebacks can be snipped as much as desired with the | |
182 | exception to the lines that designate a traceback. For non-syntax error |
|
182 | exception to the lines that designate a traceback. For non-syntax error | |
183 | tracebacks, this is the line of hyphens. For syntax error tracebacks, |
|
183 | tracebacks, this is the line of hyphens. For syntax error tracebacks, | |
184 | this is the line which lists the File and line number. |
|
184 | this is the line which lists the File and line number. | |
185 |
|
185 | |||
186 | """ |
|
186 | """ | |
187 | # The lexer inherits from DelegatingLexer. The "root" lexer is an |
|
187 | # The lexer inherits from DelegatingLexer. The "root" lexer is an | |
188 | # appropriate IPython lexer, which depends on the value of the boolean |
|
188 | # appropriate IPython lexer, which depends on the value of the boolean | |
189 | # `python3`. First, we parse with the partial IPython traceback lexer. |
|
189 | # `python3`. First, we parse with the partial IPython traceback lexer. | |
190 | # Then, any code marked with the "Other" token is delegated to the root |
|
190 | # Then, any code marked with the "Other" token is delegated to the root | |
191 | # lexer. |
|
191 | # lexer. | |
192 | # |
|
192 | # | |
193 | name = 'IPython Traceback' |
|
193 | name = 'IPython Traceback' | |
194 | aliases = ['ipythontb'] |
|
194 | aliases = ['ipythontb'] | |
195 |
|
195 | |||
196 | def __init__(self, **options): |
|
196 | def __init__(self, **options): | |
197 | self.python3 = get_bool_opt(options, 'python3', False) |
|
197 | self.python3 = get_bool_opt(options, 'python3', False) | |
198 | if self.python3: |
|
198 | if self.python3: | |
199 | self.aliases = ['ipython3tb'] |
|
199 | self.aliases = ['ipython3tb'] | |
200 | else: |
|
200 | else: | |
201 | self.aliases = ['ipython2tb', 'ipythontb'] |
|
201 | self.aliases = ['ipython2tb', 'ipythontb'] | |
202 |
|
202 | |||
203 | if self.python3: |
|
203 | if self.python3: | |
204 | IPyLexer = IPython3Lexer |
|
204 | IPyLexer = IPython3Lexer | |
205 | else: |
|
205 | else: | |
206 | IPyLexer = IPythonLexer |
|
206 | IPyLexer = IPythonLexer | |
207 |
|
207 | |||
208 | DelegatingLexer.__init__(self, IPyLexer, |
|
208 | DelegatingLexer.__init__(self, IPyLexer, | |
209 | IPythonPartialTracebackLexer, **options) |
|
209 | IPythonPartialTracebackLexer, **options) | |
210 |
|
210 | |||
211 | class IPythonConsoleLexer(Lexer): |
|
211 | class IPythonConsoleLexer(Lexer): | |
212 | """ |
|
212 | """ | |
213 | An IPython console lexer for IPython code-blocks and doctests, such as: |
|
213 | An IPython console lexer for IPython code-blocks and doctests, such as: | |
214 |
|
214 | |||
215 | .. code-block:: rst |
|
215 | .. code-block:: rst | |
216 |
|
216 | |||
217 | .. code-block:: ipythonconsole |
|
217 | .. code-block:: ipythonconsole | |
218 |
|
218 | |||
219 | In [1]: a = 'foo' |
|
219 | In [1]: a = 'foo' | |
220 |
|
220 | |||
221 | In [2]: a |
|
221 | In [2]: a | |
222 | Out[2]: 'foo' |
|
222 | Out[2]: 'foo' | |
223 |
|
223 | |||
224 |
In [3]: print |
|
224 | In [3]: print(a) | |
225 | foo |
|
225 | foo | |
226 |
|
226 | |||
227 | In [4]: 1 / 0 |
|
|||
228 |
|
||||
229 |
|
227 | |||
230 | Support is also provided for IPython exceptions: |
|
228 | Support is also provided for IPython exceptions: | |
231 |
|
229 | |||
232 | .. code-block:: rst |
|
230 | .. code-block:: rst | |
233 |
|
231 | |||
234 | .. code-block:: ipythonconsole |
|
232 | .. code-block:: ipythonconsole | |
235 |
|
233 | |||
236 | In [1]: raise Exception |
|
234 | In [1]: raise Exception | |
237 |
|
235 | Traceback (most recent call last): | ||
238 | --------------------------------------------------------------------------- |
|
236 | ... | |
239 | Exception Traceback (most recent call last) |
|
237 | Exception | |
240 | <ipython-input-1-fca2ab0ca76b> in <module> |
|
|||
241 | ----> 1 raise Exception |
|
|||
242 |
|
||||
243 | Exception: |
|
|||
244 |
|
238 | |||
245 | """ |
|
239 | """ | |
246 | name = 'IPython console session' |
|
240 | name = 'IPython console session' | |
247 | aliases = ['ipythonconsole'] |
|
241 | aliases = ['ipythonconsole'] | |
248 | mimetypes = ['text/x-ipython-console'] |
|
242 | mimetypes = ['text/x-ipython-console'] | |
249 |
|
243 | |||
250 | # The regexps used to determine what is input and what is output. |
|
244 | # The regexps used to determine what is input and what is output. | |
251 | # The default prompts for IPython are: |
|
245 | # The default prompts for IPython are: | |
252 | # |
|
246 | # | |
253 | # in = 'In [#]: ' |
|
247 | # in = 'In [#]: ' | |
254 | # continuation = ' .D.: ' |
|
248 | # continuation = ' .D.: ' | |
255 | # template = 'Out[#]: ' |
|
249 | # template = 'Out[#]: ' | |
256 | # |
|
250 | # | |
257 | # Where '#' is the 'prompt number' or 'execution count' and 'D' |
|
251 | # Where '#' is the 'prompt number' or 'execution count' and 'D' | |
258 | # D is a number of dots matching the width of the execution count |
|
252 | # D is a number of dots matching the width of the execution count | |
259 | # |
|
253 | # | |
260 | in1_regex = r'In \[[0-9]+\]: ' |
|
254 | in1_regex = r'In \[[0-9]+\]: ' | |
261 | in2_regex = r' \.\.+\.: ' |
|
255 | in2_regex = r' \.\.+\.: ' | |
262 | out_regex = r'Out\[[0-9]+\]: ' |
|
256 | out_regex = r'Out\[[0-9]+\]: ' | |
263 |
|
257 | |||
264 | #: The regex to determine when a traceback starts. |
|
258 | #: The regex to determine when a traceback starts. | |
265 | ipytb_start = re.compile(r'^(\^C)?(-+\n)|^( File)(.*)(, line )(\d+\n)') |
|
259 | ipytb_start = re.compile(r'^(\^C)?(-+\n)|^( File)(.*)(, line )(\d+\n)') | |
266 |
|
260 | |||
267 | def __init__(self, **options): |
|
261 | def __init__(self, **options): | |
268 | """Initialize the IPython console lexer. |
|
262 | """Initialize the IPython console lexer. | |
269 |
|
263 | |||
270 | Parameters |
|
264 | Parameters | |
271 | ---------- |
|
265 | ---------- | |
272 | python3 : bool |
|
266 | python3 : bool | |
273 | If `True`, then the console inputs are parsed using a Python 3 |
|
267 | If `True`, then the console inputs are parsed using a Python 3 | |
274 | lexer. Otherwise, they are parsed using a Python 2 lexer. |
|
268 | lexer. Otherwise, they are parsed using a Python 2 lexer. | |
275 | in1_regex : RegexObject |
|
269 | in1_regex : RegexObject | |
276 | The compiled regular expression used to detect the start |
|
270 | The compiled regular expression used to detect the start | |
277 | of inputs. Although the IPython configuration setting may have a |
|
271 | of inputs. Although the IPython configuration setting may have a | |
278 | trailing whitespace, do not include it in the regex. If `None`, |
|
272 | trailing whitespace, do not include it in the regex. If `None`, | |
279 | then the default input prompt is assumed. |
|
273 | then the default input prompt is assumed. | |
280 | in2_regex : RegexObject |
|
274 | in2_regex : RegexObject | |
281 | The compiled regular expression used to detect the continuation |
|
275 | The compiled regular expression used to detect the continuation | |
282 | of inputs. Although the IPython configuration setting may have a |
|
276 | of inputs. Although the IPython configuration setting may have a | |
283 | trailing whitespace, do not include it in the regex. If `None`, |
|
277 | trailing whitespace, do not include it in the regex. If `None`, | |
284 | then the default input prompt is assumed. |
|
278 | then the default input prompt is assumed. | |
285 | out_regex : RegexObject |
|
279 | out_regex : RegexObject | |
286 | The compiled regular expression used to detect outputs. If `None`, |
|
280 | The compiled regular expression used to detect outputs. If `None`, | |
287 | then the default output prompt is assumed. |
|
281 | then the default output prompt is assumed. | |
288 |
|
282 | |||
289 | """ |
|
283 | """ | |
290 | self.python3 = get_bool_opt(options, 'python3', False) |
|
284 | self.python3 = get_bool_opt(options, 'python3', False) | |
291 | if self.python3: |
|
285 | if self.python3: | |
292 | self.aliases = ['ipython3console'] |
|
286 | self.aliases = ['ipython3console'] | |
293 | else: |
|
287 | else: | |
294 | self.aliases = ['ipython2console', 'ipythonconsole'] |
|
288 | self.aliases = ['ipython2console', 'ipythonconsole'] | |
295 |
|
289 | |||
296 | in1_regex = options.get('in1_regex', self.in1_regex) |
|
290 | in1_regex = options.get('in1_regex', self.in1_regex) | |
297 | in2_regex = options.get('in2_regex', self.in2_regex) |
|
291 | in2_regex = options.get('in2_regex', self.in2_regex) | |
298 | out_regex = options.get('out_regex', self.out_regex) |
|
292 | out_regex = options.get('out_regex', self.out_regex) | |
299 |
|
293 | |||
300 | # So that we can work with input and output prompts which have been |
|
294 | # So that we can work with input and output prompts which have been | |
301 | # rstrip'd (possibly by editors) we also need rstrip'd variants. If |
|
295 | # rstrip'd (possibly by editors) we also need rstrip'd variants. If | |
302 | # we do not do this, then such prompts will be tagged as 'output'. |
|
296 | # we do not do this, then such prompts will be tagged as 'output'. | |
303 | # The reason can't just use the rstrip'd variants instead is because |
|
297 | # The reason can't just use the rstrip'd variants instead is because | |
304 | # we want any whitespace associated with the prompt to be inserted |
|
298 | # we want any whitespace associated with the prompt to be inserted | |
305 | # with the token. This allows formatted code to be modified so as hide |
|
299 | # with the token. This allows formatted code to be modified so as hide | |
306 | # the appearance of prompts, with the whitespace included. One example |
|
300 | # the appearance of prompts, with the whitespace included. One example | |
307 | # use of this is in copybutton.js from the standard lib Python docs. |
|
301 | # use of this is in copybutton.js from the standard lib Python docs. | |
308 | in1_regex_rstrip = in1_regex.rstrip() + '\n' |
|
302 | in1_regex_rstrip = in1_regex.rstrip() + '\n' | |
309 | in2_regex_rstrip = in2_regex.rstrip() + '\n' |
|
303 | in2_regex_rstrip = in2_regex.rstrip() + '\n' | |
310 | out_regex_rstrip = out_regex.rstrip() + '\n' |
|
304 | out_regex_rstrip = out_regex.rstrip() + '\n' | |
311 |
|
305 | |||
312 | # Compile and save them all. |
|
306 | # Compile and save them all. | |
313 | attrs = ['in1_regex', 'in2_regex', 'out_regex', |
|
307 | attrs = ['in1_regex', 'in2_regex', 'out_regex', | |
314 | 'in1_regex_rstrip', 'in2_regex_rstrip', 'out_regex_rstrip'] |
|
308 | 'in1_regex_rstrip', 'in2_regex_rstrip', 'out_regex_rstrip'] | |
315 | for attr in attrs: |
|
309 | for attr in attrs: | |
316 | self.__setattr__(attr, re.compile(locals()[attr])) |
|
310 | self.__setattr__(attr, re.compile(locals()[attr])) | |
317 |
|
311 | |||
318 | Lexer.__init__(self, **options) |
|
312 | Lexer.__init__(self, **options) | |
319 |
|
313 | |||
320 | if self.python3: |
|
314 | if self.python3: | |
321 | pylexer = IPython3Lexer |
|
315 | pylexer = IPython3Lexer | |
322 | tblexer = IPythonTracebackLexer |
|
316 | tblexer = IPythonTracebackLexer | |
323 | else: |
|
317 | else: | |
324 | pylexer = IPythonLexer |
|
318 | pylexer = IPythonLexer | |
325 | tblexer = IPythonTracebackLexer |
|
319 | tblexer = IPythonTracebackLexer | |
326 |
|
320 | |||
327 | self.pylexer = pylexer(**options) |
|
321 | self.pylexer = pylexer(**options) | |
328 | self.tblexer = tblexer(**options) |
|
322 | self.tblexer = tblexer(**options) | |
329 |
|
323 | |||
330 | self.reset() |
|
324 | self.reset() | |
331 |
|
325 | |||
332 | def reset(self): |
|
326 | def reset(self): | |
333 | self.mode = 'output' |
|
327 | self.mode = 'output' | |
334 | self.index = 0 |
|
328 | self.index = 0 | |
335 | self.buffer = u'' |
|
329 | self.buffer = u'' | |
336 | self.insertions = [] |
|
330 | self.insertions = [] | |
337 |
|
331 | |||
338 | def buffered_tokens(self): |
|
332 | def buffered_tokens(self): | |
339 | """ |
|
333 | """ | |
340 | Generator of unprocessed tokens after doing insertions and before |
|
334 | Generator of unprocessed tokens after doing insertions and before | |
341 | changing to a new state. |
|
335 | changing to a new state. | |
342 |
|
336 | |||
343 | """ |
|
337 | """ | |
344 | if self.mode == 'output': |
|
338 | if self.mode == 'output': | |
345 | tokens = [(0, Generic.Output, self.buffer)] |
|
339 | tokens = [(0, Generic.Output, self.buffer)] | |
346 | elif self.mode == 'input': |
|
340 | elif self.mode == 'input': | |
347 | tokens = self.pylexer.get_tokens_unprocessed(self.buffer) |
|
341 | tokens = self.pylexer.get_tokens_unprocessed(self.buffer) | |
348 | else: # traceback |
|
342 | else: # traceback | |
349 | tokens = self.tblexer.get_tokens_unprocessed(self.buffer) |
|
343 | tokens = self.tblexer.get_tokens_unprocessed(self.buffer) | |
350 |
|
344 | |||
351 | for i, t, v in do_insertions(self.insertions, tokens): |
|
345 | for i, t, v in do_insertions(self.insertions, tokens): | |
352 | # All token indexes are relative to the buffer. |
|
346 | # All token indexes are relative to the buffer. | |
353 | yield self.index + i, t, v |
|
347 | yield self.index + i, t, v | |
354 |
|
348 | |||
355 | # Clear it all |
|
349 | # Clear it all | |
356 | self.index += len(self.buffer) |
|
350 | self.index += len(self.buffer) | |
357 | self.buffer = u'' |
|
351 | self.buffer = u'' | |
358 | self.insertions = [] |
|
352 | self.insertions = [] | |
359 |
|
353 | |||
360 | def get_mci(self, line): |
|
354 | def get_mci(self, line): | |
361 | """ |
|
355 | """ | |
362 | Parses the line and returns a 3-tuple: (mode, code, insertion). |
|
356 | Parses the line and returns a 3-tuple: (mode, code, insertion). | |
363 |
|
357 | |||
364 | `mode` is the next mode (or state) of the lexer, and is always equal |
|
358 | `mode` is the next mode (or state) of the lexer, and is always equal | |
365 | to 'input', 'output', or 'tb'. |
|
359 | to 'input', 'output', or 'tb'. | |
366 |
|
360 | |||
367 | `code` is a portion of the line that should be added to the buffer |
|
361 | `code` is a portion of the line that should be added to the buffer | |
368 | corresponding to the next mode and eventually lexed by another lexer. |
|
362 | corresponding to the next mode and eventually lexed by another lexer. | |
369 | For example, `code` could be Python code if `mode` were 'input'. |
|
363 | For example, `code` could be Python code if `mode` were 'input'. | |
370 |
|
364 | |||
371 | `insertion` is a 3-tuple (index, token, text) representing an |
|
365 | `insertion` is a 3-tuple (index, token, text) representing an | |
372 | unprocessed "token" that will be inserted into the stream of tokens |
|
366 | unprocessed "token" that will be inserted into the stream of tokens | |
373 | that are created from the buffer once we change modes. This is usually |
|
367 | that are created from the buffer once we change modes. This is usually | |
374 | the input or output prompt. |
|
368 | the input or output prompt. | |
375 |
|
369 | |||
376 | In general, the next mode depends on current mode and on the contents |
|
370 | In general, the next mode depends on current mode and on the contents | |
377 | of `line`. |
|
371 | of `line`. | |
378 |
|
372 | |||
379 | """ |
|
373 | """ | |
380 | # To reduce the number of regex match checks, we have multiple |
|
374 | # To reduce the number of regex match checks, we have multiple | |
381 | # 'if' blocks instead of 'if-elif' blocks. |
|
375 | # 'if' blocks instead of 'if-elif' blocks. | |
382 |
|
376 | |||
383 | # Check for possible end of input |
|
377 | # Check for possible end of input | |
384 | in2_match = self.in2_regex.match(line) |
|
378 | in2_match = self.in2_regex.match(line) | |
385 | in2_match_rstrip = self.in2_regex_rstrip.match(line) |
|
379 | in2_match_rstrip = self.in2_regex_rstrip.match(line) | |
386 | if (in2_match and in2_match.group().rstrip() == line.rstrip()) or \ |
|
380 | if (in2_match and in2_match.group().rstrip() == line.rstrip()) or \ | |
387 | in2_match_rstrip: |
|
381 | in2_match_rstrip: | |
388 | end_input = True |
|
382 | end_input = True | |
389 | else: |
|
383 | else: | |
390 | end_input = False |
|
384 | end_input = False | |
391 | if end_input and self.mode != 'tb': |
|
385 | if end_input and self.mode != 'tb': | |
392 | # Only look for an end of input when not in tb mode. |
|
386 | # Only look for an end of input when not in tb mode. | |
393 | # An ellipsis could appear within the traceback. |
|
387 | # An ellipsis could appear within the traceback. | |
394 | mode = 'output' |
|
388 | mode = 'output' | |
395 | code = u'' |
|
389 | code = u'' | |
396 | insertion = (0, Generic.Prompt, line) |
|
390 | insertion = (0, Generic.Prompt, line) | |
397 | return mode, code, insertion |
|
391 | return mode, code, insertion | |
398 |
|
392 | |||
399 | # Check for output prompt |
|
393 | # Check for output prompt | |
400 | out_match = self.out_regex.match(line) |
|
394 | out_match = self.out_regex.match(line) | |
401 | out_match_rstrip = self.out_regex_rstrip.match(line) |
|
395 | out_match_rstrip = self.out_regex_rstrip.match(line) | |
402 | if out_match or out_match_rstrip: |
|
396 | if out_match or out_match_rstrip: | |
403 | mode = 'output' |
|
397 | mode = 'output' | |
404 | if out_match: |
|
398 | if out_match: | |
405 | idx = out_match.end() |
|
399 | idx = out_match.end() | |
406 | else: |
|
400 | else: | |
407 | idx = out_match_rstrip.end() |
|
401 | idx = out_match_rstrip.end() | |
408 | code = line[idx:] |
|
402 | code = line[idx:] | |
409 | # Use the 'heading' token for output. We cannot use Generic.Error |
|
403 | # Use the 'heading' token for output. We cannot use Generic.Error | |
410 | # since it would conflict with exceptions. |
|
404 | # since it would conflict with exceptions. | |
411 | insertion = (0, Generic.Heading, line[:idx]) |
|
405 | insertion = (0, Generic.Heading, line[:idx]) | |
412 | return mode, code, insertion |
|
406 | return mode, code, insertion | |
413 |
|
407 | |||
414 |
|
408 | |||
415 | # Check for input or continuation prompt (non stripped version) |
|
409 | # Check for input or continuation prompt (non stripped version) | |
416 | in1_match = self.in1_regex.match(line) |
|
410 | in1_match = self.in1_regex.match(line) | |
417 | if in1_match or (in2_match and self.mode != 'tb'): |
|
411 | if in1_match or (in2_match and self.mode != 'tb'): | |
418 | # New input or when not in tb, continued input. |
|
412 | # New input or when not in tb, continued input. | |
419 | # We do not check for continued input when in tb since it is |
|
413 | # We do not check for continued input when in tb since it is | |
420 | # allowable to replace a long stack with an ellipsis. |
|
414 | # allowable to replace a long stack with an ellipsis. | |
421 | mode = 'input' |
|
415 | mode = 'input' | |
422 | if in1_match: |
|
416 | if in1_match: | |
423 | idx = in1_match.end() |
|
417 | idx = in1_match.end() | |
424 | else: # in2_match |
|
418 | else: # in2_match | |
425 | idx = in2_match.end() |
|
419 | idx = in2_match.end() | |
426 | code = line[idx:] |
|
420 | code = line[idx:] | |
427 | insertion = (0, Generic.Prompt, line[:idx]) |
|
421 | insertion = (0, Generic.Prompt, line[:idx]) | |
428 | return mode, code, insertion |
|
422 | return mode, code, insertion | |
429 |
|
423 | |||
430 | # Check for input or continuation prompt (stripped version) |
|
424 | # Check for input or continuation prompt (stripped version) | |
431 | in1_match_rstrip = self.in1_regex_rstrip.match(line) |
|
425 | in1_match_rstrip = self.in1_regex_rstrip.match(line) | |
432 | if in1_match_rstrip or (in2_match_rstrip and self.mode != 'tb'): |
|
426 | if in1_match_rstrip or (in2_match_rstrip and self.mode != 'tb'): | |
433 | # New input or when not in tb, continued input. |
|
427 | # New input or when not in tb, continued input. | |
434 | # We do not check for continued input when in tb since it is |
|
428 | # We do not check for continued input when in tb since it is | |
435 | # allowable to replace a long stack with an ellipsis. |
|
429 | # allowable to replace a long stack with an ellipsis. | |
436 | mode = 'input' |
|
430 | mode = 'input' | |
437 | if in1_match_rstrip: |
|
431 | if in1_match_rstrip: | |
438 | idx = in1_match_rstrip.end() |
|
432 | idx = in1_match_rstrip.end() | |
439 | else: # in2_match |
|
433 | else: # in2_match | |
440 | idx = in2_match_rstrip.end() |
|
434 | idx = in2_match_rstrip.end() | |
441 | code = line[idx:] |
|
435 | code = line[idx:] | |
442 | insertion = (0, Generic.Prompt, line[:idx]) |
|
436 | insertion = (0, Generic.Prompt, line[:idx]) | |
443 | return mode, code, insertion |
|
437 | return mode, code, insertion | |
444 |
|
438 | |||
445 | # Check for traceback |
|
439 | # Check for traceback | |
446 | if self.ipytb_start.match(line): |
|
440 | if self.ipytb_start.match(line): | |
447 | mode = 'tb' |
|
441 | mode = 'tb' | |
448 | code = line |
|
442 | code = line | |
449 | insertion = None |
|
443 | insertion = None | |
450 | return mode, code, insertion |
|
444 | return mode, code, insertion | |
451 |
|
445 | |||
452 | # All other stuff... |
|
446 | # All other stuff... | |
453 | if self.mode in ('input', 'output'): |
|
447 | if self.mode in ('input', 'output'): | |
454 | # We assume all other text is output. Multiline input that |
|
448 | # We assume all other text is output. Multiline input that | |
455 | # does not use the continuation marker cannot be detected. |
|
449 | # does not use the continuation marker cannot be detected. | |
456 | # For example, the 3 in the following is clearly output: |
|
450 | # For example, the 3 in the following is clearly output: | |
457 | # |
|
451 | # | |
458 | # In [1]: print 3 |
|
452 | # In [1]: print 3 | |
459 | # 3 |
|
453 | # 3 | |
460 | # |
|
454 | # | |
461 | # But the following second line is part of the input: |
|
455 | # But the following second line is part of the input: | |
462 | # |
|
456 | # | |
463 | # In [2]: while True: |
|
457 | # In [2]: while True: | |
464 | # print True |
|
458 | # print True | |
465 | # |
|
459 | # | |
466 | # In both cases, the 2nd line will be 'output'. |
|
460 | # In both cases, the 2nd line will be 'output'. | |
467 | # |
|
461 | # | |
468 | mode = 'output' |
|
462 | mode = 'output' | |
469 | else: |
|
463 | else: | |
470 | mode = 'tb' |
|
464 | mode = 'tb' | |
471 |
|
465 | |||
472 | code = line |
|
466 | code = line | |
473 | insertion = None |
|
467 | insertion = None | |
474 |
|
468 | |||
475 | return mode, code, insertion |
|
469 | return mode, code, insertion | |
476 |
|
470 | |||
477 | def get_tokens_unprocessed(self, text): |
|
471 | def get_tokens_unprocessed(self, text): | |
478 | self.reset() |
|
472 | self.reset() | |
479 | for match in line_re.finditer(text): |
|
473 | for match in line_re.finditer(text): | |
480 | line = match.group() |
|
474 | line = match.group() | |
481 | mode, code, insertion = self.get_mci(line) |
|
475 | mode, code, insertion = self.get_mci(line) | |
482 |
|
476 | |||
483 | if mode != self.mode: |
|
477 | if mode != self.mode: | |
484 | # Yield buffered tokens before transitioning to new mode. |
|
478 | # Yield buffered tokens before transitioning to new mode. | |
485 | for token in self.buffered_tokens(): |
|
479 | for token in self.buffered_tokens(): | |
486 | yield token |
|
480 | yield token | |
487 | self.mode = mode |
|
481 | self.mode = mode | |
488 |
|
482 | |||
489 | if insertion: |
|
483 | if insertion: | |
490 | self.insertions.append((len(self.buffer), [insertion])) |
|
484 | self.insertions.append((len(self.buffer), [insertion])) | |
491 | self.buffer += code |
|
485 | self.buffer += code | |
492 |
|
486 | |||
493 | for token in self.buffered_tokens(): |
|
487 | for token in self.buffered_tokens(): | |
494 | yield token |
|
488 | yield token | |
495 |
|
489 | |||
496 | class IPyLexer(Lexer): |
|
490 | class IPyLexer(Lexer): | |
497 | r""" |
|
491 | r""" | |
498 | Primary lexer for all IPython-like code. |
|
492 | Primary lexer for all IPython-like code. | |
499 |
|
493 | |||
500 | This is a simple helper lexer. If the first line of the text begins with |
|
494 | This is a simple helper lexer. If the first line of the text begins with | |
501 | "In \[[0-9]+\]:", then the entire text is parsed with an IPython console |
|
495 | "In \[[0-9]+\]:", then the entire text is parsed with an IPython console | |
502 | lexer. If not, then the entire text is parsed with an IPython lexer. |
|
496 | lexer. If not, then the entire text is parsed with an IPython lexer. | |
503 |
|
497 | |||
504 | The goal is to reduce the number of lexers that are registered |
|
498 | The goal is to reduce the number of lexers that are registered | |
505 | with Pygments. |
|
499 | with Pygments. | |
506 |
|
500 | |||
507 | """ |
|
501 | """ | |
508 | name = 'IPy session' |
|
502 | name = 'IPy session' | |
509 | aliases = ['ipy'] |
|
503 | aliases = ['ipy'] | |
510 |
|
504 | |||
511 | def __init__(self, **options): |
|
505 | def __init__(self, **options): | |
512 | self.python3 = get_bool_opt(options, 'python3', False) |
|
506 | self.python3 = get_bool_opt(options, 'python3', False) | |
513 | if self.python3: |
|
507 | if self.python3: | |
514 | self.aliases = ['ipy3'] |
|
508 | self.aliases = ['ipy3'] | |
515 | else: |
|
509 | else: | |
516 | self.aliases = ['ipy2', 'ipy'] |
|
510 | self.aliases = ['ipy2', 'ipy'] | |
517 |
|
511 | |||
518 | Lexer.__init__(self, **options) |
|
512 | Lexer.__init__(self, **options) | |
519 |
|
513 | |||
520 | self.IPythonLexer = IPythonLexer(**options) |
|
514 | self.IPythonLexer = IPythonLexer(**options) | |
521 | self.IPythonConsoleLexer = IPythonConsoleLexer(**options) |
|
515 | self.IPythonConsoleLexer = IPythonConsoleLexer(**options) | |
522 |
|
516 | |||
523 | def get_tokens_unprocessed(self, text): |
|
517 | def get_tokens_unprocessed(self, text): | |
524 | # Search for the input prompt anywhere...this allows code blocks to |
|
518 | # Search for the input prompt anywhere...this allows code blocks to | |
525 | # begin with comments as well. |
|
519 | # begin with comments as well. | |
526 | if re.match(r'.*(In \[[0-9]+\]:)', text.strip(), re.DOTALL): |
|
520 | if re.match(r'.*(In \[[0-9]+\]:)', text.strip(), re.DOTALL): | |
527 | lex = self.IPythonConsoleLexer |
|
521 | lex = self.IPythonConsoleLexer | |
528 | else: |
|
522 | else: | |
529 | lex = self.IPythonLexer |
|
523 | lex = self.IPythonLexer | |
530 | for token in lex.get_tokens_unprocessed(text): |
|
524 | for token in lex.get_tokens_unprocessed(text): | |
531 | yield token |
|
525 | yield token | |
532 |
|
526 |
General Comments 0
You need to be logged in to leave comments.
Login now