##// END OF EJS Templates
fix n^2 performance issue in coalesce_streams preprocessor...
MinRK -
Show More
@@ -1,83 +1,85
1 1 """Module that allows latex output notebooks to be conditioned before
2 2 they are converted. Exposes a decorator (@cell_preprocessor) in
3 3 addition to the coalesce_streams pre-proccessor.
4 4 """
5 5 #-----------------------------------------------------------------------------
6 6 # Copyright (c) 2013, the IPython Development Team.
7 7 #
8 8 # Distributed under the terms of the Modified BSD License.
9 9 #
10 10 # The full license is in the file COPYING.txt, distributed with this software.
11 11 #-----------------------------------------------------------------------------
12 12
13 13 #-----------------------------------------------------------------------------
14 14 # Imports
15 15 #-----------------------------------------------------------------------------
16 16 import re
17 17
18 18 #-----------------------------------------------------------------------------
19 19 # Functions
20 20 #-----------------------------------------------------------------------------
21 21 def cell_preprocessor(function):
22 22 """
23 23 Wrap a function to be executed on all cells of a notebook
24 24
25 25 The wrapped function should have these parameters:
26 26
27 27 cell : NotebookNode cell
28 28 Notebook cell being processed
29 29 resources : dictionary
30 30 Additional resources used in the conversion process. Allows
31 31 preprocessors to pass variables into the Jinja engine.
32 32 index : int
33 33 Index of the cell being processed
34 34 """
35 35
36 36 def wrappedfunc(nb, resources):
37 37 for worksheet in nb.worksheets :
38 38 for index, cell in enumerate(worksheet.cells):
39 39 worksheet.cells[index], resources = function(cell, resources, index)
40 40 return nb, resources
41 41 return wrappedfunc
42 42
43 cr_pat = re.compile(r'.*\r(?=[^\n])')
43 44
44 45 @cell_preprocessor
45 46 def coalesce_streams(cell, resources, index):
46 47 """
47 48 Merge consecutive sequences of stream output into single stream
48 49 to prevent extra newlines inserted at flush calls
49 50
50 51 Parameters
51 52 ----------
52 53 cell : NotebookNode cell
53 54 Notebook cell being processed
54 55 resources : dictionary
55 56 Additional resources used in the conversion process. Allows
56 57 transformers to pass variables into the Jinja engine.
57 58 index : int
58 59 Index of the cell being processed
59 60 """
60 61
61 62 outputs = cell.get('outputs', [])
62 63 if not outputs:
63 64 return cell, resources
64 65
65 66 last = outputs[0]
66 67 new_outputs = [last]
67
68 68 for output in outputs[1:]:
69 69 if (output.output_type == 'stream' and
70 70 last.output_type == 'stream' and
71 71 last.stream == output.stream
72 72 ):
73 73 last.text += output.text
74 74
75 # Respect \r characters.
76 cr_pat = re.compile(r'.*\r(?=[^\n])')
77 last.text = cr_pat.sub('', last.text)
78 75 else:
79 76 new_outputs.append(output)
80 77 last = output
81 78
79 # process \r characters
80 for output in new_outputs:
81 if output.output_type == 'stream':
82 output.text = cr_pat.sub('', output.text)
83
82 84 cell.outputs = new_outputs
83 85 return cell, resources
General Comments 0
You need to be logged in to leave comments. Login now