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