Show More
@@ -359,8 +359,26 b' def cellmagic(end_on_blank_line=False):' | |||||
359 | line = tpl % (magic_name, first, u'\n'.join(body)) |
|
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 |
"""Remove matching input prompts from a block of input. |
|
363 | """Remove matching input prompts from a block of input. | |
|
364 | ||||
|
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. | |||
|
374 | ||||
|
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. | |||
|
379 | """ | |||
|
380 | if initial_re is None: | |||
|
381 | initial_re = prompt_re | |||
364 | line = '' |
|
382 | line = '' | |
365 | while True: |
|
383 | while True: | |
366 | line = (yield line) |
|
384 | line = (yield line) | |
@@ -368,18 +386,19 b' def _strip_prompts(prompt_re):' | |||||
368 | # First line of cell |
|
386 | # First line of cell | |
369 | if line is None: |
|
387 | if line is None: | |
370 | continue |
|
388 | continue | |
371 |
out, n1 = |
|
389 | out, n1 = initial_re.subn('', line, count=1) | |
372 | line = (yield out) |
|
390 | line = (yield out) | |
373 |
|
391 | |||
374 | # Second line of cell, because people often copy from just after the |
|
|||
375 | # first prompt, so we might not see it in the first line. |
|
|||
376 | if line is None: |
|
392 | if line is None: | |
377 | continue |
|
393 | continue | |
|
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. | |||
378 | out, n2 = prompt_re.subn('', line, count=1) |
|
397 | out, n2 = prompt_re.subn('', line, count=1) | |
379 | line = (yield out) |
|
398 | line = (yield out) | |
380 |
|
399 | |||
381 | if n1 or n2: |
|
400 | if n1 or n2: | |
382 |
# Found |
|
401 | # Found a prompt in the first two lines - check for it in | |
383 | # the rest of the cell as well. |
|
402 | # the rest of the cell as well. | |
384 | while line is not None: |
|
403 | while line is not None: | |
385 | line = (yield prompt_re.sub('', line, count=1)) |
|
404 | line = (yield prompt_re.sub('', line, count=1)) | |
@@ -394,7 +413,8 b' def classic_prompt():' | |||||
394 | """Strip the >>>/... prompts of the Python interactive shell.""" |
|
413 | """Strip the >>>/... prompts of the Python interactive shell.""" | |
395 | # FIXME: non-capturing version (?:...) usable? |
|
414 | # FIXME: non-capturing version (?:...) usable? | |
396 | prompt_re = re.compile(r'^(>>> ?|\.\.\. ?)') |
|
415 | prompt_re = re.compile(r'^(>>> ?|\.\.\. ?)') | |
397 | return _strip_prompts(prompt_re) |
|
416 | initial_re = re.compile(r'^(>>> ?)') | |
|
417 | return _strip_prompts(prompt_re, initial_re) | |||
398 |
|
418 | |||
399 | @CoroutineInputTransformer.wrap |
|
419 | @CoroutineInputTransformer.wrap | |
400 | def ipy_prompt(): |
|
420 | def ipy_prompt(): |
@@ -183,9 +183,18 b' syntax_ml = \\' | |||||
183 | ('... 456"""','456"""'), |
|
183 | ('... 456"""','456"""'), | |
184 | ], |
|
184 | ], | |
185 | [('a="""','a="""'), |
|
185 | [('a="""','a="""'), | |
|
186 | ('>>> 123','123'), | |||
|
187 | ('... 456"""','456"""'), | |||
|
188 | ], | |||
|
189 | [('a="""','a="""'), | |||
186 | ('123','123'), |
|
190 | ('123','123'), | |
187 | ('... 456"""','... 456"""'), |
|
191 | ('... 456"""','... 456"""'), | |
188 | ], |
|
192 | ], | |
|
193 | [('....__class__','....__class__'), | |||
|
194 | ], | |||
|
195 | [('a=5', 'a=5'), | |||
|
196 | ('...', ''), | |||
|
197 | ], | |||
189 | [('>>> def f(x):', 'def f(x):'), |
|
198 | [('>>> def f(x):', 'def f(x):'), | |
190 | ('...', ''), |
|
199 | ('...', ''), | |
191 | ('... return x', ' return x'), |
|
200 | ('... return x', ' return x'), | |
@@ -205,6 +214,10 b' syntax_ml = \\' | |||||
205 | (' ...: 456"""','456"""'), |
|
214 | (' ...: 456"""','456"""'), | |
206 | ], |
|
215 | ], | |
207 | [('a="""','a="""'), |
|
216 | [('a="""','a="""'), | |
|
217 | ('In [1]: 123','123'), | |||
|
218 | (' ...: 456"""','456"""'), | |||
|
219 | ], | |||
|
220 | [('a="""','a="""'), | |||
208 | ('123','123'), |
|
221 | ('123','123'), | |
209 | (' ...: 456"""',' ...: 456"""'), |
|
222 | (' ...: 456"""',' ...: 456"""'), | |
210 | ], |
|
223 | ], |
General Comments 0
You need to be logged in to leave comments.
Login now