Show More
@@ -46,6 +46,22 def restore_bytes(nb): | |||||
46 | # output keys that are likely to have multiline values |
|
46 | # output keys that are likely to have multiline values | |
47 | _multiline_outputs = ['text', 'html', 'svg', 'latex', 'javascript', 'json'] |
|
47 | _multiline_outputs = ['text', 'html', 'svg', 'latex', 'javascript', 'json'] | |
48 |
|
48 | |||
|
49 | ||||
|
50 | # FIXME: workaround for old splitlines() | |||
|
51 | def _join_lines(lines): | |||
|
52 | """join lines that have been written by splitlines() | |||
|
53 | ||||
|
54 | Has logic to protect against `splitlines()`, which | |||
|
55 | should have been `splitlines(True)` | |||
|
56 | """ | |||
|
57 | if lines and lines[0].endswith(('\n', '\r')): | |||
|
58 | # created by splitlines(True) | |||
|
59 | return u''.join(lines) | |||
|
60 | else: | |||
|
61 | # created by splitlines() | |||
|
62 | return u'\n'.join(lines) | |||
|
63 | ||||
|
64 | ||||
49 | def rejoin_lines(nb): |
|
65 | def rejoin_lines(nb): | |
50 | """rejoin multiline text into strings |
|
66 | """rejoin multiline text into strings | |
51 |
|
67 | |||
@@ -60,17 +76,17 def rejoin_lines(nb): | |||||
60 | for cell in ws.cells: |
|
76 | for cell in ws.cells: | |
61 | if cell.cell_type == 'code': |
|
77 | if cell.cell_type == 'code': | |
62 | if 'input' in cell and isinstance(cell.input, list): |
|
78 | if 'input' in cell and isinstance(cell.input, list): | |
63 |
cell.input = |
|
79 | cell.input = _join_lines(cell.input) | |
64 | for output in cell.outputs: |
|
80 | for output in cell.outputs: | |
65 | for key in _multiline_outputs: |
|
81 | for key in _multiline_outputs: | |
66 | item = output.get(key, None) |
|
82 | item = output.get(key, None) | |
67 | if isinstance(item, list): |
|
83 | if isinstance(item, list): | |
68 |
output[key] = |
|
84 | output[key] = _join_lines(item) | |
69 | else: # text, heading cell |
|
85 | else: # text, heading cell | |
70 | for key in ['source', 'rendered']: |
|
86 | for key in ['source', 'rendered']: | |
71 | item = cell.get(key, None) |
|
87 | item = cell.get(key, None) | |
72 | if isinstance(item, list): |
|
88 | if isinstance(item, list): | |
73 |
cell[key] = |
|
89 | cell[key] = _join_lines(item) | |
74 | return nb |
|
90 | return nb | |
75 |
|
91 | |||
76 |
|
92 | |||
@@ -86,17 +102,17 def split_lines(nb): | |||||
86 | for cell in ws.cells: |
|
102 | for cell in ws.cells: | |
87 | if cell.cell_type == 'code': |
|
103 | if cell.cell_type == 'code': | |
88 | if 'input' in cell and isinstance(cell.input, basestring): |
|
104 | if 'input' in cell and isinstance(cell.input, basestring): | |
89 |
cell.input = |
|
105 | cell.input = cell.input.splitlines(True) | |
90 | for output in cell.outputs: |
|
106 | for output in cell.outputs: | |
91 | for key in _multiline_outputs: |
|
107 | for key in _multiline_outputs: | |
92 | item = output.get(key, None) |
|
108 | item = output.get(key, None) | |
93 | if isinstance(item, basestring): |
|
109 | if isinstance(item, basestring): | |
94 |
output[key] = |
|
110 | output[key] = item.splitlines(True) | |
95 | else: # text, heading cell |
|
111 | else: # text, heading cell | |
96 | for key in ['source', 'rendered']: |
|
112 | for key in ['source', 'rendered']: | |
97 | item = cell.get(key, None) |
|
113 | item = cell.get(key, None) | |
98 | if isinstance(item, basestring): |
|
114 | if isinstance(item, basestring): | |
99 |
cell[key] = |
|
115 | cell[key] = item.splitlines(True) | |
100 | return nb |
|
116 | return nb | |
101 |
|
117 | |||
102 | # b64 encode/decode are never actually used, because all bytes objects in |
|
118 | # b64 encode/decode are never actually used, because all bytes objects in |
@@ -88,6 +88,12 ws.cells.append(new_code_cell( | |||||
88 | etype=u'NameError', |
|
88 | etype=u'NameError', | |
89 | evalue=u'NameError was here', |
|
89 | evalue=u'NameError was here', | |
90 | traceback=[u'frame 0', u'frame 1', u'frame 2'] |
|
90 | traceback=[u'frame 0', u'frame 1', u'frame 2'] | |
|
91 | ),new_output( | |||
|
92 | output_type=u'stream', | |||
|
93 | output_text='foo\rbar\r\n' | |||
|
94 | ),new_output( | |||
|
95 | output_type=u'stream', | |||
|
96 | output_text='\rfoo\rbar\n' | |||
91 | )] |
|
97 | )] | |
92 | )) |
|
98 | )) | |
93 |
|
99 |
General Comments 0
You need to be logged in to leave comments.
Login now