Show More
@@ -1,75 +1,74 b'' | |||
|
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 | # Functions |
|
15 | 15 | #----------------------------------------------------------------------------- |
|
16 | 16 | |
|
17 | 17 | def cell_preprocessor(function): |
|
18 | 18 | """ |
|
19 | 19 | Wrap a function to be executed on all cells of a notebook |
|
20 | 20 | |
|
21 | 21 | Wrapped Parameters |
|
22 | 22 | ---------- |
|
23 | 23 | cell : NotebookNode cell |
|
24 | 24 | Notebook cell being processed |
|
25 | 25 | resources : dictionary |
|
26 | 26 | Additional resources used in the conversion process. Allows |
|
27 | 27 | transformers to pass variables into the Jinja engine. |
|
28 | 28 | index : int |
|
29 | 29 | Index of the cell being processed |
|
30 | 30 | """ |
|
31 | 31 | |
|
32 | 32 | def wrappedfunc(nb, resources): |
|
33 | 33 | for worksheet in nb.worksheets : |
|
34 | 34 | for index, cell in enumerate(worksheet.cells): |
|
35 | 35 | worksheet.cells[index], resources = function(cell, resources, index) |
|
36 | 36 | return nb, resources |
|
37 | 37 | return wrappedfunc |
|
38 | 38 | |
|
39 | 39 | |
|
40 | 40 | @cell_preprocessor |
|
41 | 41 | def coalesce_streams(cell, resources, index): |
|
42 | 42 | """ |
|
43 | 43 | Merge consecutive sequences of stream output into single stream |
|
44 | 44 | to prevent extra newlines inserted at flush calls |
|
45 | 45 | |
|
46 | 46 | Parameters |
|
47 | 47 | ---------- |
|
48 | 48 | cell : NotebookNode cell |
|
49 | 49 | Notebook cell being processed |
|
50 | 50 | resources : dictionary |
|
51 | 51 | Additional resources used in the conversion process. Allows |
|
52 | 52 | transformers to pass variables into the Jinja engine. |
|
53 | 53 | index : int |
|
54 | 54 | Index of the cell being processed |
|
55 | 55 | """ |
|
56 | 56 | |
|
57 | 57 | outputs = cell.get('outputs', []) |
|
58 | 58 | if not outputs: |
|
59 | 59 | return cell, resources |
|
60 | 60 | |
|
61 | 61 | last = outputs[0] |
|
62 | 62 | new_outputs = [last] |
|
63 | 63 | |
|
64 | 64 | for output in outputs[1:]: |
|
65 | 65 | if (output.output_type == 'stream' and |
|
66 | 66 | last.output_type == 'stream' and |
|
67 | 67 | last.stream == output.stream |
|
68 | 68 | ): |
|
69 | 69 | last.text += output.text |
|
70 | 70 | else: |
|
71 | 71 | new_outputs.append(output) |
|
72 | 72 | |
|
73 | 73 | cell.outputs = new_outputs |
|
74 | 74 | return cell, resources |
|
75 |
General Comments 0
You need to be logged in to leave comments.
Login now