##// END OF EJS Templates
Deactivate prompt transformers if the first line matches certain patterns...
Thomas Kluyver -
Show More
@@ -400,7 +400,7 def cellmagic(end_on_blank_line=False):
400 line = tpl % (magic_name, first, u'\n'.join(body))
400 line = tpl % (magic_name, first, u'\n'.join(body))
401
401
402
402
403 def _strip_prompts(prompt_re, initial_re=None):
403 def _strip_prompts(prompt_re, initial_re=None, turnoff_re=None):
404 """Remove matching input prompts from a block of input.
404 """Remove matching input prompts from a block of input.
405
405
406 Parameters
406 Parameters
@@ -418,6 +418,12 def _strip_prompts(prompt_re, initial_re=None):
418 If any prompt is found on the first two lines,
418 If any prompt is found on the first two lines,
419 prompts will be stripped from the rest of the block.
419 prompts will be stripped from the rest of the block.
420 """
420 """
421 def pass_thru(line1):
422 "Pass lines through unaltered until the end of the cell"
423 line = line1
424 while line is not None:
425 line = (yield line)
426
421 if initial_re is None:
427 if initial_re is None:
422 initial_re = prompt_re
428 initial_re = prompt_re
423 line = ''
429 line = ''
@@ -428,6 +434,14 def _strip_prompts(prompt_re, initial_re=None):
428 if line is None:
434 if line is None:
429 continue
435 continue
430 out, n1 = initial_re.subn('', line, count=1)
436 out, n1 = initial_re.subn('', line, count=1)
437 if turnoff_re and not n1:
438 if turnoff_re.match(line):
439 # We're in e.g. a cell magic; disable this transformer for
440 # the rest of the cell.
441 yield from pass_thru(line)
442 line = None
443 continue
444
431 line = (yield out)
445 line = (yield out)
432
446
433 if line is None:
447 if line is None:
@@ -446,8 +460,8 def _strip_prompts(prompt_re, initial_re=None):
446
460
447 else:
461 else:
448 # Prompts not in input - wait for reset
462 # Prompts not in input - wait for reset
449 while line is not None:
463 yield from pass_thru(line)
450 line = (yield line)
464 line = None
451
465
452 @CoroutineInputTransformer.wrap
466 @CoroutineInputTransformer.wrap
453 def classic_prompt():
467 def classic_prompt():
@@ -455,14 +469,18 def classic_prompt():
455 # FIXME: non-capturing version (?:...) usable?
469 # FIXME: non-capturing version (?:...) usable?
456 prompt_re = re.compile(r'^(>>>|\.\.\.)( |$)')
470 prompt_re = re.compile(r'^(>>>|\.\.\.)( |$)')
457 initial_re = re.compile(r'^>>>( |$)')
471 initial_re = re.compile(r'^>>>( |$)')
458 return _strip_prompts(prompt_re, initial_re)
472 # Any %magic/!system is IPython syntax, so we needn't look for >>> prompts
473 turnoff_re = re.compile(r'^[%!]')
474 return _strip_prompts(prompt_re, initial_re, turnoff_re)
459
475
460 @CoroutineInputTransformer.wrap
476 @CoroutineInputTransformer.wrap
461 def ipy_prompt():
477 def ipy_prompt():
462 """Strip IPython's In [1]:/...: prompts."""
478 """Strip IPython's In [1]:/...: prompts."""
463 # FIXME: non-capturing version (?:...) usable?
479 # FIXME: non-capturing version (?:...) usable?
464 prompt_re = re.compile(r'^(In \[\d+\]: |\s*\.{3,}: ?)')
480 prompt_re = re.compile(r'^(In \[\d+\]: |\s*\.{3,}: ?)')
465 return _strip_prompts(prompt_re)
481 # Disable prompt stripping inside cell magics
482 turnoff_re = re.compile(r'^%%')
483 return _strip_prompts(prompt_re, turnoff_re=turnoff_re)
466
484
467
485
468 @CoroutineInputTransformer.wrap
486 @CoroutineInputTransformer.wrap
@@ -452,7 +452,7 class IPythonInputTestCase(InputSplitterTestCase):
452 ("%%cellm a\nIn[1]:", u'cellm', u'a', u'In[1]:'),
452 ("%%cellm a\nIn[1]:", u'cellm', u'a', u'In[1]:'),
453 ("%%cellm \nline\n>>> hi", u'cellm', u'', u'line\n>>> hi'),
453 ("%%cellm \nline\n>>> hi", u'cellm', u'', u'line\n>>> hi'),
454 (">>> %%cellm \nline\n>>> hi", u'cellm', u'', u'line\nhi'),
454 (">>> %%cellm \nline\n>>> hi", u'cellm', u'', u'line\nhi'),
455 ("%%cellm \n>>> hi", u'cellm', u'', u'hi'),
455 ("%%cellm \n>>> hi", u'cellm', u'', u'>>> hi'),
456 ("%%cellm \nline1\nline2", u'cellm', u'', u'line1\nline2'),
456 ("%%cellm \nline1\nline2", u'cellm', u'', u'line1\nline2'),
457 ("%%cellm \nline1\\\\\nline2", u'cellm', u'', u'line1\\\\\nline2'),
457 ("%%cellm \nline1\\\\\nline2", u'cellm', u'', u'line1\\\\\nline2'),
458 ]:
458 ]:
@@ -360,12 +360,25 def test_classic_prompt():
360 for example in syntax_ml['multiline_datastructure_prompt']:
360 for example in syntax_ml['multiline_datastructure_prompt']:
361 transform_checker(example, ipt.classic_prompt)
361 transform_checker(example, ipt.classic_prompt)
362
362
363 # Check that we don't transform the second line if the first is obviously
364 # IPython syntax
365 transform_checker([
366 (u'%foo', '%foo'),
367 (u'>>> bar', '>>> bar'),
368 ], ipt.classic_prompt)
369
363
370
364 def test_ipy_prompt():
371 def test_ipy_prompt():
365 tt.check_pairs(transform_and_reset(ipt.ipy_prompt), syntax['ipy_prompt'])
372 tt.check_pairs(transform_and_reset(ipt.ipy_prompt), syntax['ipy_prompt'])
366 for example in syntax_ml['ipy_prompt']:
373 for example in syntax_ml['ipy_prompt']:
367 transform_checker(example, ipt.ipy_prompt)
374 transform_checker(example, ipt.ipy_prompt)
368
375
376 # Check that we don't transform the second line if we're inside a cell magic
377 transform_checker([
378 (u'%%foo', '%%foo'),
379 (u'In [1]: bar', 'In [1]: bar'),
380 ], ipt.ipy_prompt)
381
369 def test_coding_cookie():
382 def test_coding_cookie():
370 tt.check_pairs(transform_and_reset(ipt.strip_encoding_cookie), syntax['strip_encoding_cookie'])
383 tt.check_pairs(transform_and_reset(ipt.strip_encoding_cookie), syntax['strip_encoding_cookie'])
371 for example in syntax_ml['strip_encoding_cookie']:
384 for example in syntax_ml['strip_encoding_cookie']:
General Comments 0
You need to be logged in to leave comments. Login now