Show More
@@ -359,17 +359,26 b' def cellmagic(end_on_blank_line=False):' | |||
|
359 | 359 | line = tpl % (magic_name, first, u'\n'.join(body)) |
|
360 | 360 | |
|
361 | 361 | |
|
362 |
def _strip_prompts(prompt_re, |
|
|
362 | def _strip_prompts(prompt_re, initial_re=None): | |
|
363 | 363 | """Remove matching input prompts from a block of input. |
|
364 | 364 | |
|
365 | prompt_re is stripped only once, on either the first or second line. | |
|
366 | If prompt_re is found on one of the first two lines, | |
|
367 | continuation_re is stripped from lines thereafter. | |
|
365 | Parameters | |
|
366 | ---------- | |
|
367 | prompt_re : regular expression | |
|
368 | A regular expression matching any input prompt (including continuation) | |
|
369 | initial_re : regular expression, optional | |
|
370 | A regular expression matching only the initial prompt, but not continuation. | |
|
371 | If no initial expression is given, prompt_re will be used everywhere. | |
|
372 | Used mainly for plain Python prompts, where the continuation prompt | |
|
373 | ``...`` is a valid Python expression in Python 3, so shouldn't be stripped. | |
|
368 | 374 | |
|
369 | If continuation_re is unspecified, prompt_re will be used for both. | |
|
375 | If initial_re and prompt_re differ, | |
|
376 | only initial_re will be tested against the first line. | |
|
377 | If any prompt is found on the first two lines, | |
|
378 | prompts will be stripped from the rest of the block. | |
|
370 | 379 | """ |
|
371 |
if |
|
|
372 |
|
|
|
380 | if initial_re is None: | |
|
381 | initial_re = prompt_re | |
|
373 | 382 | line = '' |
|
374 | 383 | while True: |
|
375 | 384 | line = (yield line) |
@@ -377,26 +386,22 b' def _strip_prompts(prompt_re, continuation_re=None):' | |||
|
377 | 386 | # First line of cell |
|
378 | 387 | if line is None: |
|
379 | 388 | continue |
|
380 |
out, n1 = |
|
|
389 | out, n1 = initial_re.subn('', line, count=1) | |
|
381 | 390 | line = (yield out) |
|
382 | 391 | |
|
383 | # Second line of cell, because people often copy from just after the | |
|
384 | # first prompt, so we might not see it in the first line. | |
|
385 | 392 | if line is None: |
|
386 | 393 | continue |
|
387 | # check for first prompt if not found on first line, continuation otherwise | |
|
388 | if n1: | |
|
389 | pat = continuation_re | |
|
390 | else: | |
|
391 | pat = prompt_re | |
|
392 | out, n2 = pat.subn('', line, count=1) | |
|
394 | # check for any prompt on the second line of the cell, | |
|
395 | # because people often copy from just after the first prompt, | |
|
396 | # so we might not see it in the first line. | |
|
397 | out, n2 = prompt_re.subn('', line, count=1) | |
|
393 | 398 | line = (yield out) |
|
394 | 399 | |
|
395 | 400 | if n1 or n2: |
|
396 |
# Found |
|
|
401 | # Found a prompt in the first two lines - check for it in | |
|
397 | 402 | # the rest of the cell as well. |
|
398 | 403 | while line is not None: |
|
399 |
line = (yield |
|
|
404 | line = (yield prompt_re.sub('', line, count=1)) | |
|
400 | 405 | |
|
401 | 406 | else: |
|
402 | 407 | # Prompts not in input - wait for reset |
@@ -407,18 +412,17 b' def _strip_prompts(prompt_re, continuation_re=None):' | |||
|
407 | 412 | def classic_prompt(): |
|
408 | 413 | """Strip the >>>/... prompts of the Python interactive shell.""" |
|
409 | 414 | # FIXME: non-capturing version (?:...) usable? |
|
410 | prompt_re = re.compile(r'^(>>> ?)') | |
|
411 |
|
|
|
412 |
return _strip_prompts(prompt_re, |
|
|
415 | prompt_re = re.compile(r'^(>>> ?|\.\.\. ?)') | |
|
416 | initial_re = re.compile(r'^(>>> ?)') | |
|
417 | return _strip_prompts(prompt_re, initial_re) | |
|
413 | 418 | |
|
414 | 419 | @CoroutineInputTransformer.wrap |
|
415 | 420 | def ipy_prompt(): |
|
416 | 421 | """Strip IPython's In [1]:/...: prompts.""" |
|
417 | 422 | # FIXME: non-capturing version (?:...) usable? |
|
418 | 423 | # FIXME: r'^(In \[\d+\]: | {3}\.{3,}: )' clearer? |
|
419 | prompt_re = re.compile(r'^(In \[\d+\]: )') | |
|
420 | continuation_re = re.compile(r'^(In \[\d+\]: |\ \ \ \.\.\.+: )') | |
|
421 | return _strip_prompts(prompt_re, continuation_re) | |
|
424 | prompt_re = re.compile(r'^(In \[\d+\]: |\ \ \ \.\.\.+: )') | |
|
425 | return _strip_prompts(prompt_re) | |
|
422 | 426 | |
|
423 | 427 | |
|
424 | 428 | @CoroutineInputTransformer.wrap |
@@ -179,8 +179,12 b' syntax_ml = \\' | |||
|
179 | 179 | ('... 123"""','123"""'), |
|
180 | 180 | ], |
|
181 | 181 | [('a="""','a="""'), |
|
182 |
('... 123',' |
|
|
183 |
('... 456"""',' |
|
|
182 | ('... 123','123'), | |
|
183 | ('... 456"""','456"""'), | |
|
184 | ], | |
|
185 | [('a="""','a="""'), | |
|
186 | ('>>> 123','123'), | |
|
187 | ('... 456"""','456"""'), | |
|
184 | 188 | ], |
|
185 | 189 | [('a="""','a="""'), |
|
186 | 190 | ('123','123'), |
@@ -189,7 +193,7 b' syntax_ml = \\' | |||
|
189 | 193 | [('....__class__','....__class__'), |
|
190 | 194 | ], |
|
191 | 195 | [('a=5', 'a=5'), |
|
192 |
('...', ' |
|
|
196 | ('...', ''), | |
|
193 | 197 | ], |
|
194 | 198 | [('>>> def f(x):', 'def f(x):'), |
|
195 | 199 | ('...', ''), |
@@ -206,8 +210,12 b' syntax_ml = \\' | |||
|
206 | 210 | (' ...: 123"""','123"""'), |
|
207 | 211 | ], |
|
208 | 212 | [('a="""','a="""'), |
|
209 |
(' ...: 123',' |
|
|
210 |
(' ...: 456"""',' |
|
|
213 | (' ...: 123','123'), | |
|
214 | (' ...: 456"""','456"""'), | |
|
215 | ], | |
|
216 | [('a="""','a="""'), | |
|
217 | ('In [1]: 123','123'), | |
|
218 | (' ...: 456"""','456"""'), | |
|
211 | 219 | ], |
|
212 | 220 | [('a="""','a="""'), |
|
213 | 221 | ('123','123'), |
General Comments 0
You need to be logged in to leave comments.
Login now