##// END OF EJS Templates
adjust strip_prompts logic a bit further...
MinRK -
Show More
@@ -359,17 +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, continuation_re=None):
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
364
365 prompt_re is stripped only once, on either the first or second line.
365 Parameters
366 If prompt_re is found on one of the first two lines,
366 ----------
367 continuation_re is stripped from lines thereafter.
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 continuation_re is None:
380 if initial_re is None:
372 continuation_re = prompt_re
381 initial_re = prompt_re
373 line = ''
382 line = ''
374 while True:
383 while True:
375 line = (yield line)
384 line = (yield line)
@@ -377,26 +386,22 b' def _strip_prompts(prompt_re, continuation_re=None):'
377 # First line of cell
386 # First line of cell
378 if line is None:
387 if line is None:
379 continue
388 continue
380 out, n1 = prompt_re.subn('', line, count=1)
389 out, n1 = initial_re.subn('', line, count=1)
381 line = (yield out)
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 if line is None:
392 if line is None:
386 continue
393 continue
387 # check for first prompt if not found on first line, continuation otherwise
394 # check for any prompt on the second line of the cell,
388 if n1:
395 # because people often copy from just after the first prompt,
389 pat = continuation_re
396 # so we might not see it in the first line.
390 else:
397 out, n2 = prompt_re.subn('', line, count=1)
391 pat = prompt_re
392 out, n2 = pat.subn('', line, count=1)
393 line = (yield out)
398 line = (yield out)
394
399
395 if n1 or n2:
400 if n1 or n2:
396 # Found the input prompt in the first two lines - check for it in
401 # Found a prompt in the first two lines - check for it in
397 # the rest of the cell as well.
402 # the rest of the cell as well.
398 while line is not None:
403 while line is not None:
399 line = (yield continuation_re.sub('', line, count=1))
404 line = (yield prompt_re.sub('', line, count=1))
400
405
401 else:
406 else:
402 # Prompts not in input - wait for reset
407 # Prompts not in input - wait for reset
@@ -407,18 +412,17 b' def _strip_prompts(prompt_re, continuation_re=None):'
407 def classic_prompt():
412 def classic_prompt():
408 """Strip the >>>/... prompts of the Python interactive shell."""
413 """Strip the >>>/... prompts of the Python interactive shell."""
409 # FIXME: non-capturing version (?:...) usable?
414 # FIXME: non-capturing version (?:...) usable?
410 prompt_re = re.compile(r'^(>>> ?)')
415 prompt_re = re.compile(r'^(>>> ?|\.\.\. ?)')
411 continuation_re = re.compile(r'^(>>> ?|\.\.\. ?)')
416 initial_re = re.compile(r'^(>>> ?)')
412 return _strip_prompts(prompt_re, continuation_re)
417 return _strip_prompts(prompt_re, initial_re)
413
418
414 @CoroutineInputTransformer.wrap
419 @CoroutineInputTransformer.wrap
415 def ipy_prompt():
420 def ipy_prompt():
416 """Strip IPython's In [1]:/...: prompts."""
421 """Strip IPython's In [1]:/...: prompts."""
417 # FIXME: non-capturing version (?:...) usable?
422 # FIXME: non-capturing version (?:...) usable?
418 # FIXME: r'^(In \[\d+\]: | {3}\.{3,}: )' clearer?
423 # FIXME: r'^(In \[\d+\]: | {3}\.{3,}: )' clearer?
419 prompt_re = re.compile(r'^(In \[\d+\]: )')
424 prompt_re = re.compile(r'^(In \[\d+\]: |\ \ \ \.\.\.+: )')
420 continuation_re = re.compile(r'^(In \[\d+\]: |\ \ \ \.\.\.+: )')
425 return _strip_prompts(prompt_re)
421 return _strip_prompts(prompt_re, continuation_re)
422
426
423
427
424 @CoroutineInputTransformer.wrap
428 @CoroutineInputTransformer.wrap
@@ -179,8 +179,12 b' syntax_ml = \\'
179 ('... 123"""','123"""'),
179 ('... 123"""','123"""'),
180 ],
180 ],
181 [('a="""','a="""'),
181 [('a="""','a="""'),
182 ('... 123','... 123'),
182 ('... 123','123'),
183 ('... 456"""','... 456"""'),
183 ('... 456"""','456"""'),
184 ],
185 [('a="""','a="""'),
186 ('>>> 123','123'),
187 ('... 456"""','456"""'),
184 ],
188 ],
185 [('a="""','a="""'),
189 [('a="""','a="""'),
186 ('123','123'),
190 ('123','123'),
@@ -189,7 +193,7 b' syntax_ml = \\'
189 [('....__class__','....__class__'),
193 [('....__class__','....__class__'),
190 ],
194 ],
191 [('a=5', 'a=5'),
195 [('a=5', 'a=5'),
192 ('...', '...'),
196 ('...', ''),
193 ],
197 ],
194 [('>>> def f(x):', 'def f(x):'),
198 [('>>> def f(x):', 'def f(x):'),
195 ('...', ''),
199 ('...', ''),
@@ -206,8 +210,12 b' syntax_ml = \\'
206 (' ...: 123"""','123"""'),
210 (' ...: 123"""','123"""'),
207 ],
211 ],
208 [('a="""','a="""'),
212 [('a="""','a="""'),
209 (' ...: 123',' ...: 123'),
213 (' ...: 123','123'),
210 (' ...: 456"""',' ...: 456"""'),
214 (' ...: 456"""','456"""'),
215 ],
216 [('a="""','a="""'),
217 ('In [1]: 123','123'),
218 (' ...: 456"""','456"""'),
211 ],
219 ],
212 [('a="""','a="""'),
220 [('a="""','a="""'),
213 ('123','123'),
221 ('123','123'),
General Comments 0
You need to be logged in to leave comments. Login now