Captures & transforms cell magics. After a cell magic is started, this stores up any lines it gets until it is reset (sent None).
(end_on_blank_line=False)
| 357 | |
| 358 | @CoroutineInputTransformer.wrap |
| 359 | def cellmagic(end_on_blank_line=False): |
| 360 | """Captures & transforms cell magics. |
| 361 | |
| 362 | After a cell magic is started, this stores up any lines it gets until it is |
| 363 | reset (sent None). |
| 364 | """ |
| 365 | tpl = 'get_ipython().run_cell_magic(%r, %r, %r)' |
| 366 | cellmagic_help_re = re.compile(r'%%\w+\?') |
| 367 | line = '' |
| 368 | while True: |
| 369 | line = (yield line) |
| 370 | # consume leading empty lines |
| 371 | while not line: |
| 372 | line = (yield line) |
| 373 | |
| 374 | if not line.startswith(ESC_MAGIC2): |
| 375 | # This isn't a cell magic, idle waiting for reset then start over |
| 376 | while line is not None: |
| 377 | line = (yield line) |
| 378 | continue |
| 379 | |
| 380 | if cellmagic_help_re.match(line): |
| 381 | # This case will be handled by help_end |
| 382 | continue |
| 383 | |
| 384 | first = line |
| 385 | body = [] |
| 386 | line = (yield None) |
| 387 | while (line is not None) and \ |
| 388 | ((line.strip() != '') or not end_on_blank_line): |
| 389 | body.append(line) |
| 390 | line = (yield None) |
| 391 | |
| 392 | # Output |
| 393 | magic_name, _, first = first.partition(' ') |
| 394 | magic_name = magic_name.lstrip(ESC_MAGIC2) |
| 395 | line = tpl % (magic_name, first, u'\n'.join(body)) |
| 396 | |
| 397 | |
| 398 | def _strip_prompts(prompt_re, initial_re=None, turnoff_re=None): |