Show More
@@ -1,88 +1,91 b'' | |||
|
1 | 1 | """Code for converting notebooks to and from the v2 format.""" |
|
2 | 2 | |
|
3 | 3 | # Copyright (c) IPython Development Team. |
|
4 | 4 | # Distributed under the terms of the Modified BSD License. |
|
5 | 5 | |
|
6 | 6 | from .nbbase import ( |
|
7 | 7 | new_code_cell, new_text_cell, new_worksheet, new_notebook, new_output, |
|
8 | 8 | nbformat, nbformat_minor |
|
9 | 9 | ) |
|
10 | 10 | |
|
11 | 11 | from IPython.nbformat import v2 |
|
12 | 12 | |
|
13 | 13 | def _unbytes(obj): |
|
14 | 14 | """There should be no bytes objects in a notebook |
|
15 | 15 | |
|
16 | 16 | v2 stores png/jpeg as b64 ascii bytes |
|
17 | 17 | """ |
|
18 | 18 | if isinstance(obj, dict): |
|
19 | 19 | for k,v in obj.items(): |
|
20 | 20 | obj[k] = _unbytes(v) |
|
21 | 21 | elif isinstance(obj, list): |
|
22 | 22 | for i,v in enumerate(obj): |
|
23 | 23 | obj[i] = _unbytes(v) |
|
24 | 24 | elif isinstance(obj, bytes): |
|
25 | 25 | # only valid bytes are b64-encoded ascii |
|
26 | 26 | obj = obj.decode('ascii') |
|
27 | 27 | return obj |
|
28 | 28 | |
|
29 | 29 | def upgrade(nb, from_version=2, from_minor=0): |
|
30 | 30 | """Convert a notebook to v3. |
|
31 | 31 | |
|
32 | 32 | Parameters |
|
33 | 33 | ---------- |
|
34 | 34 | nb : NotebookNode |
|
35 | 35 | The Python representation of the notebook to convert. |
|
36 | 36 | from_version : int |
|
37 | 37 | The original version of the notebook to convert. |
|
38 | 38 | from_minor : int |
|
39 | 39 | The original minor version of the notebook to convert (only relevant for v >= 3). |
|
40 | 40 | """ |
|
41 | 41 | if from_version == 2: |
|
42 | 42 | # Mark the original nbformat so consumers know it has been converted. |
|
43 | 43 | nb.nbformat = nbformat |
|
44 | 44 | nb.nbformat_minor = nbformat_minor |
|
45 | 45 | |
|
46 | 46 | nb.orig_nbformat = 2 |
|
47 | 47 | nb = _unbytes(nb) |
|
48 | for ws in nb['worksheets']: | |
|
49 | for cell in ws['cells']: | |
|
50 | cell.setdefault('metadata', {}) | |
|
48 | 51 | return nb |
|
49 | 52 | elif from_version == 3: |
|
50 | 53 | if from_minor != nbformat_minor: |
|
51 | 54 | nb.orig_nbformat_minor = from_minor |
|
52 | 55 | nb.nbformat_minor = nbformat_minor |
|
53 | 56 | return nb |
|
54 | 57 | else: |
|
55 | 58 | raise ValueError('Cannot convert a notebook directly from v%s to v3. ' \ |
|
56 | 59 | 'Try using the IPython.nbformat.convert module.' % from_version) |
|
57 | 60 | |
|
58 | 61 | |
|
59 | 62 | def heading_to_md(cell): |
|
60 | 63 | """turn heading cell into corresponding markdown""" |
|
61 | 64 | cell.cell_type = "markdown" |
|
62 | 65 | level = cell.pop('level', 1) |
|
63 | 66 | cell.source = '#'*level + ' ' + cell.source |
|
64 | 67 | |
|
65 | 68 | |
|
66 | 69 | def raw_to_md(cell): |
|
67 | 70 | """let raw passthrough as markdown""" |
|
68 | 71 | cell.cell_type = "markdown" |
|
69 | 72 | |
|
70 | 73 | |
|
71 | 74 | def downgrade(nb): |
|
72 | 75 | """Convert a v3 notebook to v2. |
|
73 | 76 | |
|
74 | 77 | Parameters |
|
75 | 78 | ---------- |
|
76 | 79 | nb : NotebookNode |
|
77 | 80 | The Python representation of the notebook to convert. |
|
78 | 81 | """ |
|
79 | 82 | if nb.nbformat != 3: |
|
80 | 83 | return nb |
|
81 | 84 | nb.nbformat = 2 |
|
82 | 85 | for ws in nb.worksheets: |
|
83 | 86 | for cell in ws.cells: |
|
84 | 87 | if cell.cell_type == 'heading': |
|
85 | 88 | heading_to_md(cell) |
|
86 | 89 | elif cell.cell_type == 'raw': |
|
87 | 90 | raw_to_md(cell) |
|
88 | 91 | return nb No newline at end of file |
General Comments 0
You need to be logged in to leave comments.
Login now