##// END OF EJS Templates
only process cr if it's found...
MinRK -
Show More
@@ -1,77 +1,77 b''
1 1 """Preprocessor for merging consecutive stream outputs for easier handling."""
2 2
3 3 # Copyright (c) IPython Development Team.
4 4 # Distributed under the terms of the Modified BSD License.
5 5
6 6 import re
7 7
8 8 def cell_preprocessor(function):
9 9 """
10 10 Wrap a function to be executed on all cells of a notebook
11 11
12 12 The wrapped function should have these parameters:
13 13
14 14 cell : NotebookNode cell
15 15 Notebook cell being processed
16 16 resources : dictionary
17 17 Additional resources used in the conversion process. Allows
18 18 preprocessors to pass variables into the Jinja engine.
19 19 index : int
20 20 Index of the cell being processed
21 21 """
22 22
23 23 def wrappedfunc(nb, resources):
24 24 from IPython.config import Application
25 25 if Application.initialized():
26 26 Application.instance().log.debug(
27 27 "Applying preprocessor: %s", function.__name__
28 28 )
29 29 for worksheet in nb.worksheets:
30 30 for index, cell in enumerate(worksheet.cells):
31 31 worksheet.cells[index], resources = function(cell, resources, index)
32 32 return nb, resources
33 33 return wrappedfunc
34 34
35 35 cr_pat = re.compile(r'.*\r(?=[^\n])')
36 36
37 37 @cell_preprocessor
38 38 def coalesce_streams(cell, resources, index):
39 39 """
40 40 Merge consecutive sequences of stream output into single stream
41 41 to prevent extra newlines inserted at flush calls
42 42
43 43 Parameters
44 44 ----------
45 45 cell : NotebookNode cell
46 46 Notebook cell being processed
47 47 resources : dictionary
48 48 Additional resources used in the conversion process. Allows
49 49 transformers to pass variables into the Jinja engine.
50 50 index : int
51 51 Index of the cell being processed
52 52 """
53 53
54 54 outputs = cell.get('outputs', [])
55 55 if not outputs:
56 56 return cell, resources
57 57
58 58 last = outputs[0]
59 59 new_outputs = [last]
60 60 for output in outputs[1:]:
61 61 if (output.output_type == 'stream' and
62 62 last.output_type == 'stream' and
63 63 last.stream == output.stream
64 64 ):
65 65 last.text += output.text
66 66
67 67 else:
68 68 new_outputs.append(output)
69 69 last = output
70 70
71 71 # process \r characters
72 72 for output in new_outputs:
73 if output.output_type == 'stream':
73 if output.output_type == 'stream' and '\r' in output.text:
74 74 output.text = cr_pat.sub('', output.text)
75 75
76 76 cell.outputs = new_outputs
77 77 return cell, resources
General Comments 0
You need to be logged in to leave comments. Login now