Show More
@@ -16,6 +16,7 b' Authors:' | |||||
16 | # Imports |
|
16 | # Imports | |
17 | #----------------------------------------------------------------------------- |
|
17 | #----------------------------------------------------------------------------- | |
18 |
|
18 | |||
|
19 | import re | |||
19 | from .rwbase import NotebookReader, NotebookWriter |
|
20 | from .rwbase import NotebookReader, NotebookWriter | |
20 | from .nbbase import new_code_cell, new_text_cell, new_worksheet, new_notebook |
|
21 | from .nbbase import new_code_cell, new_text_cell, new_worksheet, new_notebook | |
21 |
|
22 | |||
@@ -23,6 +24,8 b' from .nbbase import new_code_cell, new_text_cell, new_worksheet, new_notebook' | |||||
23 | # Code |
|
24 | # Code | |
24 | #----------------------------------------------------------------------------- |
|
25 | #----------------------------------------------------------------------------- | |
25 |
|
26 | |||
|
27 | _encoding_declaration_re = re.compile(r"^#.*coding[:=]\s*([-\w.]+)") | |||
|
28 | ||||
26 | class PyReaderError(Exception): |
|
29 | class PyReaderError(Exception): | |
27 | pass |
|
30 | pass | |
28 |
|
31 | |||
@@ -38,7 +41,7 b' class PyReader(NotebookReader):' | |||||
38 | cell_lines = [] |
|
41 | cell_lines = [] | |
39 | state = u'codecell' |
|
42 | state = u'codecell' | |
40 | for line in lines: |
|
43 | for line in lines: | |
41 | if line.startswith(u'# <nbformat>'): |
|
44 | if line.startswith(u'# <nbformat>') or _encoding_declaration_re.match(line): | |
42 | pass |
|
45 | pass | |
43 | elif line.startswith(u'# <codecell>'): |
|
46 | elif line.startswith(u'# <codecell>'): | |
44 | cell = self.new_cell(state, cell_lines) |
|
47 | cell = self.new_cell(state, cell_lines) | |
@@ -110,7 +113,7 b' class PyReader(NotebookReader):' | |||||
110 | class PyWriter(NotebookWriter): |
|
113 | class PyWriter(NotebookWriter): | |
111 |
|
114 | |||
112 | def writes(self, nb, **kwargs): |
|
115 | def writes(self, nb, **kwargs): | |
113 | lines = [] |
|
116 | lines = [u'# -*- coding: utf-8 -*-'] | |
114 | lines.extend([u'# <nbformat>2</nbformat>','']) |
|
117 | lines.extend([u'# <nbformat>2</nbformat>','']) | |
115 | for ws in nb.worksheets: |
|
118 | for ws in nb.worksheets: | |
116 | for cell in ws.cells: |
|
119 | for cell in ws.cells: |
General Comments 0
You need to be logged in to leave comments.
Login now