##// END OF EJS Templates
use cast_unicode in nbformat
MinRK -
Show More
@@ -1,215 +1,216 b''
1 """The basic dict based notebook format.
1 """The basic dict based notebook format.
2
2
3 The Python representation of a notebook is a nested structure of
3 The Python representation of a notebook is a nested structure of
4 dictionary subclasses that support attribute access
4 dictionary subclasses that support attribute access
5 (IPython.utils.ipstruct.Struct). The functions in this module are merely
5 (IPython.utils.ipstruct.Struct). The functions in this module are merely
6 helpers to build the structs in the right form.
6 helpers to build the structs in the right form.
7
7
8 Authors:
8 Authors:
9
9
10 * Brian Granger
10 * Brian Granger
11 """
11 """
12
12
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14 # Copyright (C) 2008-2011 The IPython Development Team
14 # Copyright (C) 2008-2011 The IPython Development Team
15 #
15 #
16 # Distributed under the terms of the BSD License. The full license is in
16 # Distributed under the terms of the BSD License. The full license is in
17 # the file COPYING, distributed as part of this software.
17 # the file COPYING, distributed as part of this software.
18 #-----------------------------------------------------------------------------
18 #-----------------------------------------------------------------------------
19
19
20 #-----------------------------------------------------------------------------
20 #-----------------------------------------------------------------------------
21 # Imports
21 # Imports
22 #-----------------------------------------------------------------------------
22 #-----------------------------------------------------------------------------
23
23
24 import pprint
24 import pprint
25 import uuid
25 import uuid
26
26
27 from IPython.utils.ipstruct import Struct
27 from IPython.utils.ipstruct import Struct
28 from IPython.utils.py3compat import cast_unicode
28
29
29 #-----------------------------------------------------------------------------
30 #-----------------------------------------------------------------------------
30 # Code
31 # Code
31 #-----------------------------------------------------------------------------
32 #-----------------------------------------------------------------------------
32
33
33 # Change this when incrementing the nbformat version
34 # Change this when incrementing the nbformat version
34 nbformat = 3
35 nbformat = 3
35 nbformat_minor = 0
36 nbformat_minor = 0
36
37
37 class NotebookNode(Struct):
38 class NotebookNode(Struct):
38 pass
39 pass
39
40
40
41
41 def from_dict(d):
42 def from_dict(d):
42 if isinstance(d, dict):
43 if isinstance(d, dict):
43 newd = NotebookNode()
44 newd = NotebookNode()
44 for k,v in d.items():
45 for k,v in d.items():
45 newd[k] = from_dict(v)
46 newd[k] = from_dict(v)
46 return newd
47 return newd
47 elif isinstance(d, (tuple, list)):
48 elif isinstance(d, (tuple, list)):
48 return [from_dict(i) for i in d]
49 return [from_dict(i) for i in d]
49 else:
50 else:
50 return d
51 return d
51
52
52
53
53 def new_output(output_type=None, output_text=None, output_png=None,
54 def new_output(output_type=None, output_text=None, output_png=None,
54 output_html=None, output_svg=None, output_latex=None, output_json=None,
55 output_html=None, output_svg=None, output_latex=None, output_json=None,
55 output_javascript=None, output_jpeg=None, prompt_number=None,
56 output_javascript=None, output_jpeg=None, prompt_number=None,
56 ename=None, evalue=None, traceback=None, stream=None, metadata=None):
57 ename=None, evalue=None, traceback=None, stream=None, metadata=None):
57 """Create a new code cell with input and output"""
58 """Create a new code cell with input and output"""
58 output = NotebookNode()
59 output = NotebookNode()
59 if output_type is not None:
60 if output_type is not None:
60 output.output_type = unicode(output_type)
61 output.output_type = unicode(output_type)
61
62
62 if metadata is None:
63 if metadata is None:
63 metadata = {}
64 metadata = {}
64 if not isinstance(metadata, dict):
65 if not isinstance(metadata, dict):
65 raise TypeError("metadata must be dict")
66 raise TypeError("metadata must be dict")
66 output.metadata = metadata
67 output.metadata = metadata
67
68
68 if output_type != 'pyerr':
69 if output_type != 'pyerr':
69 if output_text is not None:
70 if output_text is not None:
70 output.text = unicode(output_text)
71 output.text = cast_unicode(output_text)
71 if output_png is not None:
72 if output_png is not None:
72 output.png = unicode(output_png)
73 output.png = cast_unicode(output_png)
73 if output_jpeg is not None:
74 if output_jpeg is not None:
74 output.jpeg = unicode(output_jpeg)
75 output.jpeg = cast_unicode(output_jpeg)
75 if output_html is not None:
76 if output_html is not None:
76 output.html = unicode(output_html)
77 output.html = cast_unicode(output_html)
77 if output_svg is not None:
78 if output_svg is not None:
78 output.svg = unicode(output_svg)
79 output.svg = cast_unicode(output_svg)
79 if output_latex is not None:
80 if output_latex is not None:
80 output.latex = unicode(output_latex)
81 output.latex = cast_unicode(output_latex)
81 if output_json is not None:
82 if output_json is not None:
82 output.json = unicode(output_json)
83 output.json = cast_unicode(output_json)
83 if output_javascript is not None:
84 if output_javascript is not None:
84 output.javascript = unicode(output_javascript)
85 output.javascript = cast_unicode(output_javascript)
85
86
86 if output_type == u'pyout':
87 if output_type == u'pyout':
87 if prompt_number is not None:
88 if prompt_number is not None:
88 output.prompt_number = int(prompt_number)
89 output.prompt_number = int(prompt_number)
89
90
90 if output_type == u'pyerr':
91 if output_type == u'pyerr':
91 if ename is not None:
92 if ename is not None:
92 output.ename = unicode(ename)
93 output.ename = cast_unicode(ename)
93 if evalue is not None:
94 if evalue is not None:
94 output.evalue = unicode(evalue)
95 output.evalue = cast_unicode(evalue)
95 if traceback is not None:
96 if traceback is not None:
96 output.traceback = [unicode(frame) for frame in list(traceback)]
97 output.traceback = [cast_unicode(frame) for frame in list(traceback)]
97
98
98 if output_type == u'stream':
99 if output_type == u'stream':
99 output.stream = 'stdout' if stream is None else unicode(stream)
100 output.stream = 'stdout' if stream is None else cast_unicode(stream)
100
101
101 return output
102 return output
102
103
103
104
104 def new_code_cell(input=None, prompt_number=None, outputs=None,
105 def new_code_cell(input=None, prompt_number=None, outputs=None,
105 language=u'python', collapsed=False, metadata=None):
106 language=u'python', collapsed=False, metadata=None):
106 """Create a new code cell with input and output"""
107 """Create a new code cell with input and output"""
107 cell = NotebookNode()
108 cell = NotebookNode()
108 cell.cell_type = u'code'
109 cell.cell_type = u'code'
109 if language is not None:
110 if language is not None:
110 cell.language = unicode(language)
111 cell.language = cast_unicode(language)
111 if input is not None:
112 if input is not None:
112 cell.input = unicode(input)
113 cell.input = cast_unicode(input)
113 if prompt_number is not None:
114 if prompt_number is not None:
114 cell.prompt_number = int(prompt_number)
115 cell.prompt_number = int(prompt_number)
115 if outputs is None:
116 if outputs is None:
116 cell.outputs = []
117 cell.outputs = []
117 else:
118 else:
118 cell.outputs = outputs
119 cell.outputs = outputs
119 if collapsed is not None:
120 if collapsed is not None:
120 cell.collapsed = bool(collapsed)
121 cell.collapsed = bool(collapsed)
121 cell.metadata = NotebookNode(metadata or {})
122 cell.metadata = NotebookNode(metadata or {})
122
123
123 return cell
124 return cell
124
125
125 def new_text_cell(cell_type, source=None, rendered=None, metadata=None):
126 def new_text_cell(cell_type, source=None, rendered=None, metadata=None):
126 """Create a new text cell."""
127 """Create a new text cell."""
127 cell = NotebookNode()
128 cell = NotebookNode()
128 # VERSIONHACK: plaintext -> raw
129 # VERSIONHACK: plaintext -> raw
129 # handle never-released plaintext name for raw cells
130 # handle never-released plaintext name for raw cells
130 if cell_type == 'plaintext':
131 if cell_type == 'plaintext':
131 cell_type = 'raw'
132 cell_type = 'raw'
132 if source is not None:
133 if source is not None:
133 cell.source = unicode(source)
134 cell.source = cast_unicode(source)
134 if rendered is not None:
135 if rendered is not None:
135 cell.rendered = unicode(rendered)
136 cell.rendered = cast_unicode(rendered)
136 cell.metadata = NotebookNode(metadata or {})
137 cell.metadata = NotebookNode(metadata or {})
137 cell.cell_type = cell_type
138 cell.cell_type = cell_type
138 return cell
139 return cell
139
140
140
141
141 def new_heading_cell(source=None, rendered=None, level=1, metadata=None):
142 def new_heading_cell(source=None, rendered=None, level=1, metadata=None):
142 """Create a new section cell with a given integer level."""
143 """Create a new section cell with a given integer level."""
143 cell = NotebookNode()
144 cell = NotebookNode()
144 cell.cell_type = u'heading'
145 cell.cell_type = u'heading'
145 if source is not None:
146 if source is not None:
146 cell.source = unicode(source)
147 cell.source = cast_unicode(source)
147 if rendered is not None:
148 if rendered is not None:
148 cell.rendered = unicode(rendered)
149 cell.rendered = cast_unicode(rendered)
149 cell.level = int(level)
150 cell.level = int(level)
150 cell.metadata = NotebookNode(metadata or {})
151 cell.metadata = NotebookNode(metadata or {})
151 return cell
152 return cell
152
153
153
154
154 def new_worksheet(name=None, cells=None, metadata=None):
155 def new_worksheet(name=None, cells=None, metadata=None):
155 """Create a worksheet by name with with a list of cells."""
156 """Create a worksheet by name with with a list of cells."""
156 ws = NotebookNode()
157 ws = NotebookNode()
157 if name is not None:
158 if name is not None:
158 ws.name = unicode(name)
159 ws.name = cast_unicode(name)
159 if cells is None:
160 if cells is None:
160 ws.cells = []
161 ws.cells = []
161 else:
162 else:
162 ws.cells = list(cells)
163 ws.cells = list(cells)
163 ws.metadata = NotebookNode(metadata or {})
164 ws.metadata = NotebookNode(metadata or {})
164 return ws
165 return ws
165
166
166
167
167 def new_notebook(name=None, metadata=None, worksheets=None):
168 def new_notebook(name=None, metadata=None, worksheets=None):
168 """Create a notebook by name, id and a list of worksheets."""
169 """Create a notebook by name, id and a list of worksheets."""
169 nb = NotebookNode()
170 nb = NotebookNode()
170 nb.nbformat = nbformat
171 nb.nbformat = nbformat
171 nb.nbformat_minor = nbformat_minor
172 nb.nbformat_minor = nbformat_minor
172 if worksheets is None:
173 if worksheets is None:
173 nb.worksheets = []
174 nb.worksheets = []
174 else:
175 else:
175 nb.worksheets = list(worksheets)
176 nb.worksheets = list(worksheets)
176 if metadata is None:
177 if metadata is None:
177 nb.metadata = new_metadata()
178 nb.metadata = new_metadata()
178 else:
179 else:
179 nb.metadata = NotebookNode(metadata)
180 nb.metadata = NotebookNode(metadata)
180 if name is not None:
181 if name is not None:
181 nb.metadata.name = unicode(name)
182 nb.metadata.name = cast_unicode(name)
182 return nb
183 return nb
183
184
184
185
185 def new_metadata(name=None, authors=None, license=None, created=None,
186 def new_metadata(name=None, authors=None, license=None, created=None,
186 modified=None, gistid=None):
187 modified=None, gistid=None):
187 """Create a new metadata node."""
188 """Create a new metadata node."""
188 metadata = NotebookNode()
189 metadata = NotebookNode()
189 if name is not None:
190 if name is not None:
190 metadata.name = unicode(name)
191 metadata.name = cast_unicode(name)
191 if authors is not None:
192 if authors is not None:
192 metadata.authors = list(authors)
193 metadata.authors = list(authors)
193 if created is not None:
194 if created is not None:
194 metadata.created = unicode(created)
195 metadata.created = cast_unicode(created)
195 if modified is not None:
196 if modified is not None:
196 metadata.modified = unicode(modified)
197 metadata.modified = cast_unicode(modified)
197 if license is not None:
198 if license is not None:
198 metadata.license = unicode(license)
199 metadata.license = cast_unicode(license)
199 if gistid is not None:
200 if gistid is not None:
200 metadata.gistid = unicode(gistid)
201 metadata.gistid = cast_unicode(gistid)
201 return metadata
202 return metadata
202
203
203 def new_author(name=None, email=None, affiliation=None, url=None):
204 def new_author(name=None, email=None, affiliation=None, url=None):
204 """Create a new author."""
205 """Create a new author."""
205 author = NotebookNode()
206 author = NotebookNode()
206 if name is not None:
207 if name is not None:
207 author.name = unicode(name)
208 author.name = cast_unicode(name)
208 if email is not None:
209 if email is not None:
209 author.email = unicode(email)
210 author.email = cast_unicode(email)
210 if affiliation is not None:
211 if affiliation is not None:
211 author.affiliation = unicode(affiliation)
212 author.affiliation = cast_unicode(affiliation)
212 if url is not None:
213 if url is not None:
213 author.url = unicode(url)
214 author.url = cast_unicode(url)
214 return author
215 return author
215
216
@@ -1,204 +1,204 b''
1 """Read and write notebooks as regular .py files.
1 """Read and write notebooks as regular .py files.
2
2
3 Authors:
3 Authors:
4
4
5 * Brian Granger
5 * Brian Granger
6 """
6 """
7
7
8 #-----------------------------------------------------------------------------
8 #-----------------------------------------------------------------------------
9 # Copyright (C) 2008-2011 The IPython Development Team
9 # Copyright (C) 2008-2011 The IPython Development Team
10 #
10 #
11 # Distributed under the terms of the BSD License. The full license is in
11 # Distributed under the terms of the BSD License. The full license is in
12 # the file COPYING, distributed as part of this software.
12 # the file COPYING, distributed as part of this software.
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14
14
15 #-----------------------------------------------------------------------------
15 #-----------------------------------------------------------------------------
16 # Imports
16 # Imports
17 #-----------------------------------------------------------------------------
17 #-----------------------------------------------------------------------------
18
18
19 import re
19 import re
20 from .rwbase import NotebookReader, NotebookWriter
20 from .rwbase import NotebookReader, NotebookWriter
21 from .nbbase import (
21 from .nbbase import (
22 new_code_cell, new_text_cell, new_worksheet,
22 new_code_cell, new_text_cell, new_worksheet,
23 new_notebook, new_heading_cell, nbformat, nbformat_minor,
23 new_notebook, new_heading_cell, nbformat, nbformat_minor,
24 )
24 )
25
25
26 #-----------------------------------------------------------------------------
26 #-----------------------------------------------------------------------------
27 # Code
27 # Code
28 #-----------------------------------------------------------------------------
28 #-----------------------------------------------------------------------------
29
29
30 _encoding_declaration_re = re.compile(r"^#.*coding[:=]\s*([-\w.]+)")
30 _encoding_declaration_re = re.compile(r"^#.*coding[:=]\s*([-\w.]+)")
31
31
32 class PyReaderError(Exception):
32 class PyReaderError(Exception):
33 pass
33 pass
34
34
35
35
36 class PyReader(NotebookReader):
36 class PyReader(NotebookReader):
37
37
38 def reads(self, s, **kwargs):
38 def reads(self, s, **kwargs):
39 return self.to_notebook(s,**kwargs)
39 return self.to_notebook(s,**kwargs)
40
40
41 def to_notebook(self, s, **kwargs):
41 def to_notebook(self, s, **kwargs):
42 lines = s.splitlines()
42 lines = s.splitlines()
43 cells = []
43 cells = []
44 cell_lines = []
44 cell_lines = []
45 kwargs = {}
45 kwargs = {}
46 state = u'codecell'
46 state = u'codecell'
47 for line in lines:
47 for line in lines:
48 if line.startswith(u'# <nbformat>') or _encoding_declaration_re.match(line):
48 if line.startswith(u'# <nbformat>') or _encoding_declaration_re.match(line):
49 pass
49 pass
50 elif line.startswith(u'# <codecell>'):
50 elif line.startswith(u'# <codecell>'):
51 cell = self.new_cell(state, cell_lines, **kwargs)
51 cell = self.new_cell(state, cell_lines, **kwargs)
52 if cell is not None:
52 if cell is not None:
53 cells.append(cell)
53 cells.append(cell)
54 state = u'codecell'
54 state = u'codecell'
55 cell_lines = []
55 cell_lines = []
56 kwargs = {}
56 kwargs = {}
57 elif line.startswith(u'# <htmlcell>'):
57 elif line.startswith(u'# <htmlcell>'):
58 cell = self.new_cell(state, cell_lines, **kwargs)
58 cell = self.new_cell(state, cell_lines, **kwargs)
59 if cell is not None:
59 if cell is not None:
60 cells.append(cell)
60 cells.append(cell)
61 state = u'htmlcell'
61 state = u'htmlcell'
62 cell_lines = []
62 cell_lines = []
63 kwargs = {}
63 kwargs = {}
64 elif line.startswith(u'# <markdowncell>'):
64 elif line.startswith(u'# <markdowncell>'):
65 cell = self.new_cell(state, cell_lines, **kwargs)
65 cell = self.new_cell(state, cell_lines, **kwargs)
66 if cell is not None:
66 if cell is not None:
67 cells.append(cell)
67 cells.append(cell)
68 state = u'markdowncell'
68 state = u'markdowncell'
69 cell_lines = []
69 cell_lines = []
70 kwargs = {}
70 kwargs = {}
71 # VERSIONHACK: plaintext -> raw
71 # VERSIONHACK: plaintext -> raw
72 elif line.startswith(u'# <rawcell>') or line.startswith(u'# <plaintextcell>'):
72 elif line.startswith(u'# <rawcell>') or line.startswith(u'# <plaintextcell>'):
73 cell = self.new_cell(state, cell_lines, **kwargs)
73 cell = self.new_cell(state, cell_lines, **kwargs)
74 if cell is not None:
74 if cell is not None:
75 cells.append(cell)
75 cells.append(cell)
76 state = u'rawcell'
76 state = u'rawcell'
77 cell_lines = []
77 cell_lines = []
78 kwargs = {}
78 kwargs = {}
79 elif line.startswith(u'# <headingcell'):
79 elif line.startswith(u'# <headingcell'):
80 cell = self.new_cell(state, cell_lines, **kwargs)
80 cell = self.new_cell(state, cell_lines, **kwargs)
81 if cell is not None:
81 if cell is not None:
82 cells.append(cell)
82 cells.append(cell)
83 cell_lines = []
83 cell_lines = []
84 m = re.match(r'# <headingcell level=(?P<level>\d)>',line)
84 m = re.match(r'# <headingcell level=(?P<level>\d)>',line)
85 if m is not None:
85 if m is not None:
86 state = u'headingcell'
86 state = u'headingcell'
87 kwargs = {}
87 kwargs = {}
88 kwargs['level'] = int(m.group('level'))
88 kwargs['level'] = int(m.group('level'))
89 else:
89 else:
90 state = u'codecell'
90 state = u'codecell'
91 kwargs = {}
91 kwargs = {}
92 cell_lines = []
92 cell_lines = []
93 else:
93 else:
94 cell_lines.append(line)
94 cell_lines.append(line)
95 if cell_lines and state == u'codecell':
95 if cell_lines and state == u'codecell':
96 cell = self.new_cell(state, cell_lines)
96 cell = self.new_cell(state, cell_lines)
97 if cell is not None:
97 if cell is not None:
98 cells.append(cell)
98 cells.append(cell)
99 ws = new_worksheet(cells=cells)
99 ws = new_worksheet(cells=cells)
100 nb = new_notebook(worksheets=[ws])
100 nb = new_notebook(worksheets=[ws])
101 return nb
101 return nb
102
102
103 def new_cell(self, state, lines, **kwargs):
103 def new_cell(self, state, lines, **kwargs):
104 if state == u'codecell':
104 if state == u'codecell':
105 input = u'\n'.join(lines)
105 input = u'\n'.join(lines)
106 input = input.strip(u'\n')
106 input = input.strip(u'\n')
107 if input:
107 if input:
108 return new_code_cell(input=input)
108 return new_code_cell(input=input)
109 elif state == u'htmlcell':
109 elif state == u'htmlcell':
110 text = self._remove_comments(lines)
110 text = self._remove_comments(lines)
111 if text:
111 if text:
112 return new_text_cell(u'html',source=text)
112 return new_text_cell(u'html',source=text)
113 elif state == u'markdowncell':
113 elif state == u'markdowncell':
114 text = self._remove_comments(lines)
114 text = self._remove_comments(lines)
115 if text:
115 if text:
116 return new_text_cell(u'markdown',source=text)
116 return new_text_cell(u'markdown',source=text)
117 elif state == u'rawcell':
117 elif state == u'rawcell':
118 text = self._remove_comments(lines)
118 text = self._remove_comments(lines)
119 if text:
119 if text:
120 return new_text_cell(u'raw',source=text)
120 return new_text_cell(u'raw',source=text)
121 elif state == u'headingcell':
121 elif state == u'headingcell':
122 text = self._remove_comments(lines)
122 text = self._remove_comments(lines)
123 level = kwargs.get('level',1)
123 level = kwargs.get('level',1)
124 if text:
124 if text:
125 return new_heading_cell(source=text,level=level)
125 return new_heading_cell(source=text,level=level)
126
126
127 def _remove_comments(self, lines):
127 def _remove_comments(self, lines):
128 new_lines = []
128 new_lines = []
129 for line in lines:
129 for line in lines:
130 if line.startswith(u'#'):
130 if line.startswith(u'#'):
131 new_lines.append(line[2:])
131 new_lines.append(line[2:])
132 else:
132 else:
133 new_lines.append(line)
133 new_lines.append(line)
134 text = u'\n'.join(new_lines)
134 text = u'\n'.join(new_lines)
135 text = text.strip(u'\n')
135 text = text.strip(u'\n')
136 return text
136 return text
137
137
138 def split_lines_into_blocks(self, lines):
138 def split_lines_into_blocks(self, lines):
139 if len(lines) == 1:
139 if len(lines) == 1:
140 yield lines[0]
140 yield lines[0]
141 raise StopIteration()
141 raise StopIteration()
142 import ast
142 import ast
143 source = '\n'.join(lines)
143 source = '\n'.join(lines)
144 code = ast.parse(source)
144 code = ast.parse(source)
145 starts = [x.lineno-1 for x in code.body]
145 starts = [x.lineno-1 for x in code.body]
146 for i in range(len(starts)-1):
146 for i in range(len(starts)-1):
147 yield '\n'.join(lines[starts[i]:starts[i+1]]).strip('\n')
147 yield '\n'.join(lines[starts[i]:starts[i+1]]).strip('\n')
148 yield '\n'.join(lines[starts[-1]:]).strip('\n')
148 yield '\n'.join(lines[starts[-1]:]).strip('\n')
149
149
150
150
151 class PyWriter(NotebookWriter):
151 class PyWriter(NotebookWriter):
152
152
153 def writes(self, nb, **kwargs):
153 def writes(self, nb, **kwargs):
154 lines = [u'# -*- coding: utf-8 -*-']
154 lines = [u'# -*- coding: utf-8 -*-']
155 lines.extend([
155 lines.extend([
156 u'# <nbformat>%i.%i</nbformat>' % (nbformat, nbformat_minor),
156 u'# <nbformat>%i.%i</nbformat>' % (nbformat, nbformat_minor),
157 u'',
157 u'',
158 ])
158 ])
159 for ws in nb.worksheets:
159 for ws in nb.worksheets:
160 for cell in ws.cells:
160 for cell in ws.cells:
161 if cell.cell_type == u'code':
161 if cell.cell_type == u'code':
162 input = cell.get(u'input')
162 input = cell.get(u'input')
163 if input is not None:
163 if input is not None:
164 lines.extend([u'# <codecell>',u''])
164 lines.extend([u'# <codecell>',u''])
165 lines.extend(input.splitlines())
165 lines.extend(input.splitlines())
166 lines.append(u'')
166 lines.append(u'')
167 elif cell.cell_type == u'html':
167 elif cell.cell_type == u'html':
168 input = cell.get(u'source')
168 input = cell.get(u'source')
169 if input is not None:
169 if input is not None:
170 lines.extend([u'# <htmlcell>',u''])
170 lines.extend([u'# <htmlcell>',u''])
171 lines.extend([u'# ' + line for line in input.splitlines()])
171 lines.extend([u'# ' + line for line in input.splitlines()])
172 lines.append(u'')
172 lines.append(u'')
173 elif cell.cell_type == u'markdown':
173 elif cell.cell_type == u'markdown':
174 input = cell.get(u'source')
174 input = cell.get(u'source')
175 if input is not None:
175 if input is not None:
176 lines.extend([u'# <markdowncell>',u''])
176 lines.extend([u'# <markdowncell>',u''])
177 lines.extend([u'# ' + line for line in input.splitlines()])
177 lines.extend([u'# ' + line for line in input.splitlines()])
178 lines.append(u'')
178 lines.append(u'')
179 elif cell.cell_type == u'raw':
179 elif cell.cell_type == u'raw':
180 input = cell.get(u'source')
180 input = cell.get(u'source')
181 if input is not None:
181 if input is not None:
182 lines.extend([u'# <rawcell>',u''])
182 lines.extend([u'# <rawcell>',u''])
183 lines.extend([u'# ' + line for line in input.splitlines()])
183 lines.extend([u'# ' + line for line in input.splitlines()])
184 lines.append(u'')
184 lines.append(u'')
185 elif cell.cell_type == u'heading':
185 elif cell.cell_type == u'heading':
186 input = cell.get(u'source')
186 input = cell.get(u'source')
187 level = cell.get(u'level',1)
187 level = cell.get(u'level',1)
188 if input is not None:
188 if input is not None:
189 lines.extend([u'# <headingcell level=%s>' % level,u''])
189 lines.extend([u'# <headingcell level=%s>' % level,u''])
190 lines.extend([u'# ' + line for line in input.splitlines()])
190 lines.extend([u'# ' + line for line in input.splitlines()])
191 lines.append(u'')
191 lines.append(u'')
192 lines.append('')
192 lines.append('')
193 return unicode('\n'.join(lines))
193 return u'\n'.join(lines)
194
194
195
195
196 _reader = PyReader()
196 _reader = PyReader()
197 _writer = PyWriter()
197 _writer = PyWriter()
198
198
199 reads = _reader.reads
199 reads = _reader.reads
200 read = _reader.read
200 read = _reader.read
201 to_notebook = _reader.to_notebook
201 to_notebook = _reader.to_notebook
202 write = _writer.write
202 write = _writer.write
203 writes = _writer.writes
203 writes = _writer.writes
204
204
General Comments 0
You need to be logged in to leave comments. Login now