Show More
@@ -0,0 +1,21 b'' | |||||
|
1 | """Contains all of the exceptions used in NBConvert explicitly""" | |||
|
2 | #----------------------------------------------------------------------------- | |||
|
3 | # Copyright (c) 2013, the IPython Development Team. | |||
|
4 | # | |||
|
5 | # Distributed under the terms of the Modified BSD License. | |||
|
6 | # | |||
|
7 | # The full license is in the file COPYING.txt, distributed with this software. | |||
|
8 | #----------------------------------------------------------------------------- | |||
|
9 | ||||
|
10 | #----------------------------------------------------------------------------- | |||
|
11 | # Imports | |||
|
12 | #----------------------------------------------------------------------------- | |||
|
13 | from __future__ import print_function | |||
|
14 | ||||
|
15 | #----------------------------------------------------------------------------- | |||
|
16 | # Classes and functions | |||
|
17 | #----------------------------------------------------------------------------- | |||
|
18 | class ConversionException(Exception): | |||
|
19 | """An exception raised by the conversion process.""" | |||
|
20 | ||||
|
21 | pass No newline at end of file |
@@ -33,104 +33,91 b' from IPython.utils.traitlets import MetaHasTraits, Unicode, List, Bool' | |||||
33 | from jinja2 import Environment, FileSystemLoader |
|
33 | from jinja2 import Environment, FileSystemLoader | |
34 |
|
34 | |||
35 | # local import (pre-transformers) |
|
35 | # local import (pre-transformers) | |
|
36 | from exceptions import ConversionException | |||
36 | from . import transformers as trans #TODO |
|
37 | from . import transformers as trans #TODO | |
|
38 | from .latex_transformer import (LatexTransformer) #TODO | |||
|
39 | from .utils import markdown2rst #TODO | |||
|
40 | ||||
|
41 | import textwrap #TODO | |||
37 |
|
42 | |||
|
43 | #Jinja2 filters | |||
|
44 | from .jinja_filters import (python_comment, indent, | |||
|
45 | rm_fake, remove_ansi, markdown, highlight, highlight2latex, | |||
|
46 | ansi2html, markdown2latex, get_lines, escape_tex, FilterDataType, | |||
|
47 | rm_dollars, rm_math_space | |||
|
48 | ) | |||
|
49 | ||||
|
50 | #Try to import the Sphinx exporter. If the user doesn't have Sphinx isntalled | |||
|
51 | #on his/her machine, fail silently. | |||
38 | try: |
|
52 | try: | |
39 | from .sphinx_transformer import (SphinxTransformer) #TODO |
|
53 | from .sphinx_transformer import (SphinxTransformer) #TODO | |
40 | except ImportError: |
|
54 | except ImportError: | |
41 | SphinxTransformer = None |
|
55 | SphinxTransformer = None | |
42 |
|
56 | |||
43 | from .latex_transformer import (LatexTransformer) #TODO |
|
57 | #----------------------------------------------------------------------------- | |
|
58 | # Globals and constants | |||
|
59 | #----------------------------------------------------------------------------- | |||
44 |
|
60 | |||
45 | # some jinja filters |
|
61 | #Standard Jinja2 environment constants | |
46 | from .jinja_filters import (python_comment, indent, |
|
62 | TEMPLATE_PATH = "/../templates/" | |
47 | rm_fake, remove_ansi, markdown, highlight, highlight2latex, |
|
63 | TEMPLATE_SKELETON_PATH = "/../templates/skeleton/" | |
48 | ansi2html, markdown2latex, get_lines, escape_tex, FilterDataType, |
|
64 | TEMPLATE_EXTENSION = ".tpl" | |
49 | rm_dollars, rm_math_space |
|
|||
50 | ) |
|
|||
51 |
|
65 | |||
52 | from .utils import markdown2rst #TODO |
|
66 | #Latex Jinja2 constants | |
|
67 | LATEX_TEMPLATE_PATH = "/../templates/tex/" | |||
|
68 | LATEX_TEMPLATE_SKELETON_PATH = "/../templates/tex/skeleton/" | |||
|
69 | LATEX_TEMPLATE_EXTENSION = ".tplx" | |||
53 |
|
70 | |||
54 | import textwrap #TODO |
|
71 | #Special Jinja2 syntax that will not conflict when exporting latex. | |
|
72 | LATEX_JINJA_COMMENT_BLOCK = ["((=", "=))"] | |||
|
73 | LATEX_JINJA_VARIABLE_BLOCK = ["(((", ")))"] | |||
|
74 | LATEX_JINJA_LOGIC_BLOCK = ["((*", "*))"] | |||
55 |
|
75 | |||
|
76 | #Jinja2 extensions to load. | |||
|
77 | JINJA_EXTENSIONS = ['jinja2.ext.loopcontrols'] | |||
|
78 | ||||
|
79 | #----------------------------------------------------------------------------- | |||
|
80 | # Local utilities | |||
|
81 | #----------------------------------------------------------------------------- | |||
|
82 | #TODO: Move to utils.strings | |||
56 | def wrap(text, width=100): |
|
83 | def wrap(text, width=100): | |
57 |
""" |
|
84 | """ Try to detect and wrap paragraph""" | |
58 | splitt = text.split('\n') |
|
85 | splitt = text.split('\n') | |
59 | wrp = map(lambda x:textwrap.wrap(x,width),splitt) |
|
86 | wrp = map(lambda x:textwrap.wrap(x,width),splitt) | |
60 | wrpd = map('\n'.join, wrp) |
|
87 | wrpd = map('\n'.join, wrp) | |
61 | return '\n'.join(wrpd) |
|
88 | return '\n'.join(wrpd) | |
62 |
|
89 | |||
63 |
|
||||
64 |
|
||||
65 | # define differents environemnt with different |
|
|||
66 | # delimiters not to conflict with languages inside |
|
|||
67 |
|
||||
68 | env = Environment( |
|
|||
69 | loader=FileSystemLoader([ |
|
|||
70 | os.path.dirname(os.path.realpath(__file__))+'/../templates/', |
|
|||
71 | os.path.dirname(os.path.realpath(__file__))+'/../templates/skeleton/', |
|
|||
72 | ]), |
|
|||
73 | extensions=['jinja2.ext.loopcontrols'] |
|
|||
74 | ) |
|
|||
75 |
|
||||
76 |
|
||||
77 | texenv = Environment( |
|
|||
78 | loader=FileSystemLoader([ |
|
|||
79 | os.path.dirname(os.path.realpath(__file__))+'/../templates/tex/', |
|
|||
80 | os.path.dirname(os.path.realpath(__file__))+'/../templates/skeleton/tex/', |
|
|||
81 | ]), |
|
|||
82 | extensions=['jinja2.ext.loopcontrols'] |
|
|||
83 | ) |
|
|||
84 |
|
||||
85 |
|
||||
86 | texenv.block_start_string = '((*' |
|
|||
87 | texenv.block_end_string = '*))' |
|
|||
88 |
|
||||
89 | texenv.variable_start_string = '(((' |
|
|||
90 | texenv.variable_end_string = ')))' |
|
|||
91 |
|
||||
92 | texenv.comment_start_string = '((=' |
|
|||
93 | texenv.comment_end_string = '=))' |
|
|||
94 |
|
||||
95 | texenv.filters['escape_tex'] = escape_tex |
|
|||
96 |
|
||||
97 | #----------------------------------------------------------------------------- |
|
90 | #----------------------------------------------------------------------------- | |
98 |
# Class |
|
91 | # Classes and functions | |
99 | #----------------------------------------------------------------------------- |
|
92 | #----------------------------------------------------------------------------- | |
100 | class ConversionException(Exception): |
|
|||
101 | pass |
|
|||
102 |
|
||||
103 | class Exporter(Configurable): |
|
93 | class Exporter(Configurable): | |
104 | """ A Jinja2 base converter templates |
|
94 | """ A Jinja2 base converter templates | |
105 |
|
95 | |||
106 | Preprocess the ipynb files, feed it throug jinja templates, |
|
96 | Preprocess the ipynb files, feed it throug jinja templates, | |
107 | and spit an converted files and a data object with other data |
|
97 | and spit an converted files and a data object with other data | |
108 |
|
||||
109 | should be mostly configurable |
|
98 | should be mostly configurable | |
110 | """ |
|
99 | """ | |
111 |
|
100 | |||
112 | pre_transformer_order = List(['haspyout_transformer'], |
|
101 | pre_transformer_order = List(['haspyout_transformer'], | |
113 |
|
|
102 | config=True, | |
114 |
|
|
103 | help= """ | |
115 |
|
|
104 | An ordered list of pre transformer to apply to the ipynb | |
116 |
|
|
105 | file before running through templates | |
117 |
|
|
106 | """ | |
118 |
|
|
107 | ) | |
119 |
|
||||
120 | tex_environement = Bool(False, |
|
|||
121 | config=True, |
|
|||
122 | help=""" is this a tex environment or not """) |
|
|||
123 |
|
108 | |||
124 | template_file = Unicode('', |
|
109 | tex_environement = Bool( | |
125 | config=True, |
|
110 | False, | |
126 | help=""" Name of the template file to use """ ) |
|
111 | config=True, | |
127 | #------------------------------------------------------------------------- |
|
112 | help=" Whether or not the user is exporting to latex.") | |
128 | # Instance-level attributes that are set in the constructor for this |
|
|||
129 | # class. |
|
|||
130 | #------------------------------------------------------------------------- |
|
|||
131 |
|
113 | |||
|
114 | template_file = Unicode( | |||
|
115 | '', config=True, | |||
|
116 | help="Name of the template file to use") | |||
132 |
|
117 | |||
133 | preprocessors = [] |
|
118 | #Processors that process the input data prior to the export, set in the | |
|
119 | #constructor for this class. | |||
|
120 | preprocessors = [] | |||
134 |
|
121 | |||
135 | def __init__(self, preprocessors={}, jinja_filters={}, config=None, **kw): |
|
122 | def __init__(self, preprocessors={}, jinja_filters={}, config=None, **kw): | |
136 | """ Init a new converter. |
|
123 | """ Init a new converter. | |
@@ -156,9 +143,34 b' class Exporter(Configurable):' | |||||
156 | """ |
|
143 | """ | |
157 | super(ConverterTemplate, self).__init__(config=config, **kw) |
|
144 | super(ConverterTemplate, self).__init__(config=config, **kw) | |
158 |
|
145 | |||
159 | # variable parameters depending on the pype of jinja environement |
|
146 | #Create a Latex environment if the user is exporting latex. | |
160 |
|
|
147 | if self.tex_environement: | |
161 | self.ext = '.tplx' if self.tex_environement else '.tpl' |
|
148 | self.ext = LATEX_TEMPLATE_EXTENSION | |
|
149 | self.env = Environment( | |||
|
150 | loader=FileSystemLoader([ | |||
|
151 | os.path.dirname(os.path.realpath(__file__)) + LATEX_TEMPLATE_PATH, | |||
|
152 | os.path.dirname(os.path.realpath(__file__)) + LATEX_TEMPLATE_SKELETON_PATH, | |||
|
153 | ]), | |||
|
154 | extensions=JINJA_EXTENSIONS | |||
|
155 | ) | |||
|
156 | ||||
|
157 | #Set special Jinja2 syntax that will not conflict with latex. | |||
|
158 | self.env.block_start_string = LATEX_JINJA_LOGIC_BLOCK[0] | |||
|
159 | self.env.block_end_string = LATEX_JINJA_LOGIC_BLOCK[1] | |||
|
160 | self.env.variable_start_string = LATEX_JINJA_VARIABLE_BLOCK[0] | |||
|
161 | self.env.variable_end_string = LATEX_JINJA_VARIABLE_BLOCK[1] | |||
|
162 | self.env.comment_start_string = LATEX_JINJA_COMMENT_BLOCK[0] | |||
|
163 | self.env.comment_end_string = LATEX_JINJA_COMMENT_BLOCK[1] | |||
|
164 | ||||
|
165 | else: #Standard environment | |||
|
166 | self.ext = TEMPLATE_EXTENSION | |||
|
167 | self.env = Environment( | |||
|
168 | loader=FileSystemLoader([ | |||
|
169 | os.path.dirname(os.path.realpath(__file__)) + TEMPLATE_PATH, | |||
|
170 | os.path.dirname(os.path.realpath(__file__)) + TEMPLATE_SKELETON_PATH, | |||
|
171 | ]), | |||
|
172 | extensions=JINJA_EXTENSIONS | |||
|
173 | ) | |||
162 |
|
174 | |||
163 | for name in self.pre_transformer_order: |
|
175 | for name in self.pre_transformer_order: | |
164 | # get the user-defined transformer first |
|
176 | # get the user-defined transformer first | |
@@ -167,25 +179,25 b' class Exporter(Configurable):' | |||||
167 | transformer = transformer(config=config) |
|
179 | transformer = transformer(config=config) | |
168 | self.preprocessors.append(transformer) |
|
180 | self.preprocessors.append(transformer) | |
169 |
|
181 | |||
170 |
# |
|
182 | #For compatibility, TODO: remove later. | |
171 | self.preprocessors.append(trans.coalesce_streams) |
|
183 | self.preprocessors.append(trans.coalesce_streams) | |
172 | self.preprocessors.append(trans.ExtractFigureTransformer(config=config)) |
|
184 | self.preprocessors.append(trans.ExtractFigureTransformer(config=config)) | |
173 | self.preprocessors.append(trans.RevealHelpTransformer(config=config)) |
|
185 | self.preprocessors.append(trans.RevealHelpTransformer(config=config)) | |
174 | self.preprocessors.append(trans.CSSHtmlHeaderTransformer(config=config)) |
|
186 | self.preprocessors.append(trans.CSSHtmlHeaderTransformer(config=config)) | |
|
187 | self.preprocessors.append(LatexTransformer(config=config)) | |||
|
188 | ||||
|
189 | #Only load the sphinx transformer if the file reference worked | |||
|
190 | #(Sphinx dependencies exist on the user's machine.) | |||
175 | if SphinxTransformer: |
|
191 | if SphinxTransformer: | |
176 | self.preprocessors.append(SphinxTransformer(config=config)) |
|
192 | self.preprocessors.append(SphinxTransformer(config=config)) | |
177 | self.preprocessors.append(LatexTransformer(config=config)) |
|
|||
178 |
|
193 | |||
179 | ## |
|
194 | #Add filters to the Jinja2 environment | |
180 | self.env.filters['filter_data_type'] = FilterDataType(config=config) |
|
195 | self.env.filters['filter_data_type'] = FilterDataType(config=config) | |
181 | self.env.filters['pycomment'] = python_comment |
|
196 | self.env.filters['pycomment'] = python_comment | |
182 | self.env.filters['indent'] = indent |
|
197 | self.env.filters['indent'] = indent | |
183 | self.env.filters['rm_fake'] = rm_fake |
|
198 | self.env.filters['rm_fake'] = rm_fake | |
184 | self.env.filters['rm_ansi'] = remove_ansi |
|
199 | self.env.filters['rm_ansi'] = remove_ansi | |
185 | self.env.filters['markdown'] = markdown |
|
200 | self.env.filters['markdown'] = markdown | |
186 | self.env.filters['highlight'] = highlight2latex if self.tex_environement else highlight |
|
|||
187 | self.env.filters['highlight2html'] = highlight |
|
|||
188 | self.env.filters['highlight2latex'] = highlight2latex |
|
|||
189 | self.env.filters['ansi2html'] = ansi2html |
|
201 | self.env.filters['ansi2html'] = ansi2html | |
190 | self.env.filters['markdown2latex'] = markdown2latex |
|
202 | self.env.filters['markdown2latex'] = markdown2latex | |
191 | self.env.filters['markdown2rst'] = markdown2rst |
|
203 | self.env.filters['markdown2rst'] = markdown2rst | |
@@ -193,57 +205,78 b' class Exporter(Configurable):' | |||||
193 | self.env.filters['wrap'] = wrap |
|
205 | self.env.filters['wrap'] = wrap | |
194 | self.env.filters['rm_dollars'] = rm_dollars |
|
206 | self.env.filters['rm_dollars'] = rm_dollars | |
195 | self.env.filters['rm_math_space'] = rm_math_space |
|
207 | self.env.filters['rm_math_space'] = rm_math_space | |
|
208 | self.env.filters['highlight2html'] = highlight | |||
|
209 | self.env.filters['highlight2latex'] = highlight2latex | |||
196 |
|
210 | |||
197 | ## user filter will overwrite |
|
211 | #Latex specific filters | |
198 | for key, filtr in jinja_filters.iteritems(): |
|
212 | if self.tex_environement: | |
199 | if isinstance(filtr, MetaHasTraits): |
|
213 | self.env.filters['escape_tex'] = escape_tex | |
200 |
|
|
214 | self.env.filters['highlight'] = highlight2latex | |
201 |
|
|
215 | else: | |
202 |
|
|
216 | self.env.filters['highlight'] = highlight | |
203 |
|
217 | |||
|
218 | #Load user filters. Overwrite existing filters if need be. | |||
|
219 | for key, user_filter in jinja_filters.iteritems(): | |||
|
220 | if isinstance(user_filter, MetaHasTraits): | |||
|
221 | self.env.filters[key] = user_filter(config=config) | |||
|
222 | else: | |||
|
223 | self.env.filters[key] = user_filter | |||
|
224 | ||||
|
225 | #Load the template file. | |||
204 | self.template = self.env.get_template(self.template_file+self.ext) |
|
226 | self.template = self.env.get_template(self.template_file+self.ext) | |
205 |
|
227 | |||
206 |
|
228 | |||
207 | def process(self, nb): |
|
229 | def _preprocess(self, nb): | |
208 | """ |
|
230 | """ Preprocess the notebook using the transformers specific | |
209 | preprocess the notebook json for easier use with the templates. |
|
231 | for the current export format. | |
210 | will call all the `preprocessor`s in order before returning it. |
|
232 | ||
|
233 | nb: Notebook to preprocess | |||
211 | """ |
|
234 | """ | |
212 |
|
235 | |||
213 |
# |
|
236 | #Dict of 'resources' that can be filled by the preprocessors. | |
214 | # like key/value data to extract files from ipynb like in latex conversion |
|
|||
215 | resources = {} |
|
237 | resources = {} | |
216 |
|
238 | |||
217 | for preprocessor in self.preprocessors: |
|
239 | #Run each transformer on the notebook. Carry the output along | |
218 | nb, resources = preprocessor(nb, resources) |
|
240 | #to each transformer | |
219 |
|
241 | for transformer in self.preprocessors: | ||
|
242 | nb, resources = transformer(nb, resources) | |||
220 | return nb, resources |
|
243 | return nb, resources | |
221 |
|
244 | |||
222 | def convert(self, nb): |
|
|||
223 | """ convert the ipynb file |
|
|||
224 |
|
245 | |||
225 | return both the converted ipynb file and a dict containing potential |
|
246 | def export(self, nb): | |
226 | other resources |
|
247 | """Export notebook object | |
|
248 | ||||
|
249 | nb: Notebook object to export. | |||
|
250 | ||||
|
251 | Returns both the converted ipynb file and a dict containing the | |||
|
252 | resources created along the way via the transformers and Jinja2 | |||
|
253 | processing. | |||
227 | """ |
|
254 | """ | |
228 | nb, resources = self.process(nb) |
|
255 | ||
|
256 | nb, resources = self._preprocess(nb) | |||
229 | return self.template.render(nb=nb, resources=resources), resources |
|
257 | return self.template.render(nb=nb, resources=resources), resources | |
230 |
|
258 | |||
231 |
|
259 | |||
232 | def from_filename(self, filename): |
|
260 | def from_filename(self, filename): | |
233 |
""" |
|
261 | """Read and export a notebook from a filename | |
234 | with io.open(filename) as f: |
|
|||
235 | return self.convert(nbformat.read(f, 'json')) |
|
|||
236 |
|
|
262 | ||
237 | def from_file(self, filelike): |
|
263 | filename: Filename of the notebook file to export. | |
238 | """read and convert a notebook from a filelike object |
|
|||
239 |
|
264 | |||
240 | filelike object will just be "read" and should be json format.. |
|
265 | Returns both the converted ipynb file and a dict containing the | |
|
266 | resources created along the way via the transformers and Jinja2 | |||
|
267 | processing. | |||
241 | """ |
|
268 | """ | |
242 | return self.convert(nbformat.read(filelike, 'json')) |
|
269 | with io.open(filename) as f: | |
|
270 | return self.export(nbformat.read(f, 'json')) | |||
|
271 | ||||
|
272 | def from_file(self, file_stream): | |||
|
273 | """Read and export a notebook from a filename | |||
243 |
|
|
274 | ||
244 | def from_json(self, json): |
|
275 | file_stream: File handle of file that contains notebook data. | |
245 | """ not implemented |
|
|||
246 |
|
276 | |||
247 | Should convert from a json object |
|
277 | Returns both the converted ipynb file and a dict containing the | |
|
278 | resources created along the way via the transformers and Jinja2 | |||
|
279 | processing. | |||
248 | """ |
|
280 | """ | |
249 | raise NotImplementedError('not implemented (yet?)') |
|
281 | ||
|
282 | return self.export(nbformat.read(file_stream, 'json')) No newline at end of file |
General Comments 0
You need to be logged in to leave comments.
Login now