Show More
@@ -23,28 +23,37 b' from __future__ import print_function, absolute_import' | |||||
23 | # Stdlib imports |
|
23 | # Stdlib imports | |
24 | import io |
|
24 | import io | |
25 | import os |
|
25 | import os | |
|
26 | import re | |||
26 |
|
27 | |||
27 | # IPython imports |
|
28 | # IPython imports | |
28 | from IPython.config.configurable import Configurable |
|
29 | from IPython.config.configurable import Configurable | |
29 | from IPython.nbformat import current as nbformat |
|
30 | from IPython.nbformat import current as nbformat | |
30 | from IPython.utils.traitlets import MetaHasTraits, Unicode, List, Bool |
|
31 | from IPython.utils.traitlets import MetaHasTraits, Unicode, List, Bool | |
|
32 | from IPython.utils.text import indent | |||
31 |
|
33 | |||
32 | # other libs/dependencies |
|
34 | # other libs/dependencies | |
33 | from jinja2 import Environment, FileSystemLoader |
|
35 | from jinja2 import Environment, FileSystemLoader | |
|
36 | from markdown import markdown | |||
34 |
|
37 | |||
35 | # local import (pre-transformers) |
|
38 | # local import (pre-transformers) | |
36 | from exceptions import ConversionException |
|
39 | from exceptions import ConversionException | |
37 | from . import transformers as trans #TODO |
|
40 | from . import transformers as trans #TODO | |
38 | from .latex_transformer import (LatexTransformer) #TODO |
|
41 | from .latex_transformer import (LatexTransformer) #TODO | |
39 | from .utils import markdown2rst #TODO |
|
42 | from .utils import markdown2rst #TODO | |
|
43 | from .utils import markdown2latex #TODO | |||
|
44 | from .utils import highlight2latex #TODO | |||
|
45 | from .utils import get_lines #TODO | |||
|
46 | from .utils import remove_ansi #TODO | |||
|
47 | from .utils import highlight, ansi2html #TODO | |||
|
48 | from .latex_transformer import rm_math_space #TODO | |||
40 |
|
49 | |||
41 | import textwrap #TODO |
|
50 | import textwrap #TODO | |
42 |
|
51 | |||
43 | #Jinja2 filters |
|
52 | #Jinja2 filters | |
44 |
from .jinja_filters import (python_comment, |
|
53 | from .jinja_filters import (python_comment, | |
45 | rm_fake, remove_ansi, markdown, highlight, highlight2latex, |
|
54 | rm_fake, | |
46 |
|
|
55 | escape_tex, FilterDataType, | |
47 |
rm_dollars |
|
56 | rm_dollars | |
48 | ) |
|
57 | ) | |
49 |
|
58 | |||
50 | #Try to import the Sphinx exporter. If the user doesn't have Sphinx isntalled |
|
59 | #Try to import the Sphinx exporter. If the user doesn't have Sphinx isntalled | |
@@ -76,6 +85,16 b' LATEX_JINJA_LOGIC_BLOCK = ["((*", "*))"]' | |||||
76 | #Jinja2 extensions to load. |
|
85 | #Jinja2 extensions to load. | |
77 | JINJA_EXTENSIONS = ['jinja2.ext.loopcontrols'] |
|
86 | JINJA_EXTENSIONS = ['jinja2.ext.loopcontrols'] | |
78 |
|
87 | |||
|
88 | #Latex substitutions for escaping latex. | |||
|
89 | LATEX_SUBS = ( | |||
|
90 | (re.compile(r'\\'), r'\\textbackslash'), | |||
|
91 | (re.compile(r'([{}_#%&$])'), r'\\\1'), | |||
|
92 | (re.compile(r'~'), r'\~{}'), | |||
|
93 | (re.compile(r'\^'), r'\^{}'), | |||
|
94 | (re.compile(r'"'), r"''"), | |||
|
95 | (re.compile(r'\.\.\.+'), r'\\ldots'), | |||
|
96 | ) | |||
|
97 | ||||
79 | #----------------------------------------------------------------------------- |
|
98 | #----------------------------------------------------------------------------- | |
80 | # Local utilities |
|
99 | # Local utilities | |
81 | #----------------------------------------------------------------------------- |
|
100 | #----------------------------------------------------------------------------- | |
@@ -134,7 +153,6 b' class Exporter(Configurable):' | |||||
134 | transformers registerd by this key will take precedence on |
|
153 | transformers registerd by this key will take precedence on | |
135 | default one. |
|
154 | default one. | |
136 |
|
155 | |||
137 |
|
||||
138 | jinja_filters: dict of supplementary jinja filter that should be made |
|
156 | jinja_filters: dict of supplementary jinja filter that should be made | |
139 | availlable in template. If those are of Configurable Class type, |
|
157 | availlable in template. If those are of Configurable Class type, | |
140 | they will be instanciated with the config object as argument. |
|
158 | they will be instanciated with the config object as argument. | |
@@ -193,9 +211,9 b' class Exporter(Configurable):' | |||||
193 |
|
211 | |||
194 | #Add filters to the Jinja2 environment |
|
212 | #Add filters to the Jinja2 environment | |
195 | self.env.filters['filter_data_type'] = FilterDataType(config=config) |
|
213 | self.env.filters['filter_data_type'] = FilterDataType(config=config) | |
196 | self.env.filters['pycomment'] = python_comment |
|
214 | self.env.filters['pycomment'] = _python_comment | |
197 | self.env.filters['indent'] = indent |
|
215 | self.env.filters['indent'] = indent | |
198 | self.env.filters['rm_fake'] = rm_fake |
|
216 | self.env.filters['rm_fake'] = _rm_fake | |
199 | self.env.filters['rm_ansi'] = remove_ansi |
|
217 | self.env.filters['rm_ansi'] = remove_ansi | |
200 | self.env.filters['markdown'] = markdown |
|
218 | self.env.filters['markdown'] = markdown | |
201 | self.env.filters['ansi2html'] = ansi2html |
|
219 | self.env.filters['ansi2html'] = ansi2html | |
@@ -203,14 +221,14 b' class Exporter(Configurable):' | |||||
203 | self.env.filters['markdown2rst'] = markdown2rst |
|
221 | self.env.filters['markdown2rst'] = markdown2rst | |
204 | self.env.filters['get_lines'] = get_lines |
|
222 | self.env.filters['get_lines'] = get_lines | |
205 | self.env.filters['wrap'] = wrap |
|
223 | self.env.filters['wrap'] = wrap | |
206 | self.env.filters['rm_dollars'] = rm_dollars |
|
224 | self.env.filters['rm_dollars'] = _rm_dollars | |
207 | self.env.filters['rm_math_space'] = rm_math_space |
|
225 | self.env.filters['rm_math_space'] = rm_math_space | |
208 | self.env.filters['highlight2html'] = highlight |
|
226 | self.env.filters['highlight2html'] = highlight | |
209 | self.env.filters['highlight2latex'] = highlight2latex |
|
227 | self.env.filters['highlight2latex'] = highlight2latex | |
210 |
|
228 | |||
211 | #Latex specific filters |
|
229 | #Latex specific filters | |
212 | if self.tex_environement: |
|
230 | if self.tex_environement: | |
213 | self.env.filters['escape_tex'] = escape_tex |
|
231 | self.env.filters['escape_tex'] = _escape_tex | |
214 | self.env.filters['highlight'] = highlight2latex |
|
232 | self.env.filters['highlight'] = highlight2latex | |
215 | else: |
|
233 | else: | |
216 | self.env.filters['highlight'] = highlight |
|
234 | self.env.filters['highlight'] = highlight | |
@@ -226,23 +244,6 b' class Exporter(Configurable):' | |||||
226 | self.template = self.env.get_template(self.template_file+self.ext) |
|
244 | self.template = self.env.get_template(self.template_file+self.ext) | |
227 |
|
245 | |||
228 |
|
246 | |||
229 | def _preprocess(self, nb): |
|
|||
230 | """ Preprocess the notebook using the transformers specific |
|
|||
231 | for the current export format. |
|
|||
232 |
|
||||
233 | nb: Notebook to preprocess |
|
|||
234 | """ |
|
|||
235 |
|
||||
236 | #Dict of 'resources' that can be filled by the preprocessors. |
|
|||
237 | resources = {} |
|
|||
238 |
|
||||
239 | #Run each transformer on the notebook. Carry the output along |
|
|||
240 | #to each transformer |
|
|||
241 | for transformer in self.preprocessors: |
|
|||
242 | nb, resources = transformer(nb, resources) |
|
|||
243 | return nb, resources |
|
|||
244 |
|
||||
245 |
|
||||
246 | def export(self, nb): |
|
247 | def export(self, nb): | |
247 | """Export notebook object |
|
248 | """Export notebook object | |
248 |
|
249 | |||
@@ -269,6 +270,7 b' class Exporter(Configurable):' | |||||
269 | with io.open(filename) as f: |
|
270 | with io.open(filename) as f: | |
270 | return self.export(nbformat.read(f, 'json')) |
|
271 | return self.export(nbformat.read(f, 'json')) | |
271 |
|
272 | |||
|
273 | ||||
272 | def from_file(self, file_stream): |
|
274 | def from_file(self, file_stream): | |
273 | """Read and export a notebook from a filename |
|
275 | """Read and export a notebook from a filename | |
274 |
|
276 | |||
@@ -279,4 +281,40 b' class Exporter(Configurable):' | |||||
279 | processing. |
|
281 | processing. | |
280 | """ |
|
282 | """ | |
281 |
|
283 | |||
282 | return self.export(nbformat.read(file_stream, 'json')) No newline at end of file |
|
284 | return self.export(nbformat.read(file_stream, 'json')) | |
|
285 | ||||
|
286 | ||||
|
287 | def _preprocess(self, nb): | |||
|
288 | """ Preprocess the notebook using the transformers specific | |||
|
289 | for the current export format. | |||
|
290 | ||||
|
291 | nb: Notebook to preprocess | |||
|
292 | """ | |||
|
293 | ||||
|
294 | #Dict of 'resources' that can be filled by the preprocessors. | |||
|
295 | resources = {} | |||
|
296 | ||||
|
297 | #Run each transformer on the notebook. Carry the output along | |||
|
298 | #to each transformer | |||
|
299 | for transformer in self.preprocessors: | |||
|
300 | nb, resources = transformer(nb, resources) | |||
|
301 | return nb, resources | |||
|
302 | ||||
|
303 | ||||
|
304 | def _rm_fake(strng): | |||
|
305 | return strng.replace('/files/', '') | |||
|
306 | ||||
|
307 | ||||
|
308 | def _rm_dollars(strng): | |||
|
309 | return strng.strip('$') | |||
|
310 | ||||
|
311 | ||||
|
312 | def _python_comment(string): | |||
|
313 | return '# '+'\n# '.join(string.split('\n')) | |||
|
314 | ||||
|
315 | ||||
|
316 | def _escape_tex(value): | |||
|
317 | newval = value | |||
|
318 | for pattern, replacement in LATEX_SUBS: | |||
|
319 | newval = pattern.sub(replacement, newval) | |||
|
320 | return newval No newline at end of file |
1 | NO CONTENT: file was removed |
|
NO CONTENT: file was removed |
General Comments 0
You need to be logged in to leave comments.
Login now