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