Show More
@@ -37,11 +37,14 b" def rst_directive(directive, text=''):" | |||||
37 | # Converters for parts of a cell. |
|
37 | # Converters for parts of a cell. | |
38 | figures_counter = 1 |
|
38 | figures_counter = 1 | |
39 |
|
39 | |||
|
40 | ||||
40 | class ConversionException(Exception): |
|
41 | class ConversionException(Exception): | |
41 | pass |
|
42 | pass | |
42 |
|
43 | |||
|
44 | ||||
43 | class Converter(object): |
|
45 | class Converter(object): | |
44 | default_encoding = 'utf-8' |
|
46 | default_encoding = 'utf-8' | |
|
47 | ||||
45 | def __init__(self, fname): |
|
48 | def __init__(self, fname): | |
46 | self.fname = fname |
|
49 | self.fname = fname | |
47 |
|
50 | |||
@@ -50,10 +53,10 b' class Converter(object):' | |||||
50 | raise ConversionException("""extension must be defined in Converter |
|
53 | raise ConversionException("""extension must be defined in Converter | |
51 | subclass""") |
|
54 | subclass""") | |
52 |
|
55 | |||
53 | def dispatch(self,cell_type): |
|
56 | def dispatch(self, cell_type): | |
54 | """return cell_type dependent render method, for example render_code |
|
57 | """return cell_type dependent render method, for example render_code | |
55 | """ |
|
58 | """ | |
56 | return getattr(self, 'render_'+cell_type, unknown_cell) |
|
59 | return getattr(self, 'render_' + cell_type, unknown_cell) | |
57 |
|
60 | |||
58 | def convert(self): |
|
61 | def convert(self): | |
59 | lines = [] |
|
62 | lines = [] | |
@@ -74,7 +77,7 b' class Converter(object):' | |||||
74 | with open(self.fname) as f: |
|
77 | with open(self.fname) as f: | |
75 | self.nb = nbformat.read(f, 'json') |
|
78 | self.nb = nbformat.read(f, 'json') | |
76 |
|
79 | |||
77 | def save(self,fname=None, encoding=None): |
|
80 | def save(self, fname=None, encoding=None): | |
78 | "read and parse notebook into self.nb" |
|
81 | "read and parse notebook into self.nb" | |
79 | if fname is None: |
|
82 | if fname is None: | |
80 | fname = os.path.splitext(self.fname)[0] + '.' + self.extension |
|
83 | fname = os.path.splitext(self.fname)[0] + '.' + self.extension | |
@@ -84,35 +87,37 b' class Converter(object):' | |||||
84 | f.write(self.output.encode(encoding)) |
|
87 | f.write(self.output.encode(encoding)) | |
85 | return fname |
|
88 | return fname | |
86 |
|
89 | |||
87 | def render_heading(self,cell): |
|
90 | def render_heading(self, cell): | |
88 |
|
|
91 | raise NotImplementedError | |
|
92 | ||||
|
93 | def render_code(self, cell): | |||
|
94 | raise NotImplementedError | |||
89 |
|
95 | |||
90 |
def render_ |
|
96 | def render_markdown(self, cell): | |
91 |
|
|
97 | raise NotImplementedError | |
92 |
|
98 | |||
93 |
def render_ |
|
99 | def render_pyout(self, cell): | |
94 |
|
|
100 | raise NotImplementedError | |
95 |
|
101 | |||
96 |
def render_ |
|
102 | def render_display_data(self, cell): | |
97 |
|
|
103 | raise NotImplementedError | |
98 |
|
104 | |||
99 |
def render_ |
|
105 | def render_stream(self, cell): | |
100 |
|
|
106 | raise NotImplementedError | |
101 |
|
107 | |||
102 | def render_stream(self,cell): |
|
|||
103 | raise NotImplementedError |
|
|||
104 |
|
108 | |||
105 | class ConverterRST(Converter): |
|
109 | class ConverterRST(Converter): | |
106 | extension = 'rst' |
|
110 | extension = 'rst' | |
107 | def render_heading(self,cell): |
|
111 | ||
|
112 | def render_heading(self, cell): | |||
108 | """convert a heading cell to rst |
|
113 | """convert a heading cell to rst | |
109 |
|
114 | |||
110 | Returns list.""" |
|
115 | Returns list.""" | |
111 | heading_level = {1:'=', 2:'-', 3:'`', 4:'\'', 5:'.',6:'~'} |
|
116 | heading_level = {1: '=', 2: '-', 3: '`', 4: '\'', 5: '.', 6: '~'} | |
112 | marker = heading_level[cell.level] |
|
117 | marker = heading_level[cell.level] | |
113 | return ['{0}\n{1}\n'.format(cell.source, marker*len(cell.source))] |
|
118 | return ['{0}\n{1}\n'.format(cell.source, marker * len(cell.source))] | |
114 |
|
119 | |||
115 | def render_code(self,cell): |
|
120 | def render_code(self, cell): | |
116 | """Convert a code cell to rst |
|
121 | """Convert a code cell to rst | |
117 |
|
122 | |||
118 | Returns list.""" |
|
123 | Returns list.""" | |
@@ -129,13 +134,13 b' class ConverterRST(Converter):' | |||||
129 |
|
134 | |||
130 | return lines |
|
135 | return lines | |
131 |
|
136 | |||
132 | def render_markdown(self,cell): |
|
137 | def render_markdown(self, cell): | |
133 | """convert a markdown cell to rst |
|
138 | """convert a markdown cell to rst | |
134 |
|
139 | |||
135 | Returns list.""" |
|
140 | Returns list.""" | |
136 | return [cell.source] |
|
141 | return [cell.source] | |
137 |
|
142 | |||
138 | def render_pyout(self,output): |
|
143 | def render_pyout(self, output): | |
139 | """convert pyout part of a code cell to rst |
|
144 | """convert pyout part of a code cell to rst | |
140 |
|
145 | |||
141 | Returns list.""" |
|
146 | Returns list.""" | |
@@ -151,7 +156,7 b' class ConverterRST(Converter):' | |||||
151 |
|
156 | |||
152 | return lines |
|
157 | return lines | |
153 |
|
158 | |||
154 | def render_display_data(self,output): |
|
159 | def render_display_data(self, output): | |
155 | """convert display data from the output of a code cell to rst. |
|
160 | """convert display data from the output of a code cell to rst. | |
156 |
|
161 | |||
157 | Returns list. |
|
162 | Returns list. | |
@@ -171,7 +176,7 b' class ConverterRST(Converter):' | |||||
171 |
|
176 | |||
172 | return lines |
|
177 | return lines | |
173 |
|
178 | |||
174 | def render_stream(self,output): |
|
179 | def render_stream(self, output): | |
175 | """convert stream part of a code cell to rst |
|
180 | """convert stream part of a code cell to rst | |
176 |
|
181 | |||
177 | Returns list.""" |
|
182 | Returns list.""" | |
@@ -183,6 +188,7 b' class ConverterRST(Converter):' | |||||
183 |
|
188 | |||
184 | return lines |
|
189 | return lines | |
185 |
|
190 | |||
|
191 | ||||
186 | def rst2simplehtml(fname): |
|
192 | def rst2simplehtml(fname): | |
187 | """Convert a rst file to simplified html suitable for blogger. |
|
193 | """Convert a rst file to simplified html suitable for blogger. | |
188 |
|
194 | |||
@@ -228,7 +234,7 b' def rst2simplehtml(fname):' | |||||
228 | break |
|
234 | break | |
229 | f.write(line) |
|
235 | f.write(line) | |
230 | f.write('\n') |
|
236 | f.write('\n') | |
231 |
|
237 | |||
232 | return newfname |
|
238 | return newfname | |
233 |
|
239 | |||
234 |
|
240 |
General Comments 0
You need to be logged in to leave comments.
Login now