##// END OF EJS Templates
Removed "profiles"... Templates that are shipped with nbconvert by default should...
Jonathan Frederic -
Show More
@@ -122,11 +122,28 b' class Exporter(Configurable):'
122 122 '', config=True,
123 123 help="Name of the template file to use")
124 124
125 fileext = Unicode(
126 'txt', config=True,
127 help="Extension of the file that should be written to disk"
128 )
129
130 stdout = Bool(
131 True, config=True,
132 help="""Whether to print the converted ipynb file to stdout
133 "use full do diff files without actually writing a new file"""
134 )
135
136 write = Bool(
137 False, config=True,
138 help="""Should the converted notebook file be written to disk
139 along with potential extracted resources."""
140 )
141
125 142 #Processors that process the input data prior to the export, set in the
126 143 #constructor for this class.
127 144 preprocessors = []
128 145
129 def __init__(self, preprocessors={}, jinja_filters={}, config=None, **kw):
146 def __init__(self, preprocessors={}, jinja_filters={}, config=None, export_format, **kw):
130 147 """ Init a new converter.
131 148
132 149 config: the Configurable config object to pass around.
@@ -147,7 +164,15 b' class Exporter(Configurable):'
147 164
148 165 user defined filter will overwrite the one availlable by default.
149 166 """
150 super(ConverterTemplate, self).__init__(config=config, **kw)
167
168 #Merge default config options with user specific override options.
169 default_config = self._get_default_options()
170 if not config == None:
171 default_config._merge(config)
172 config = default_config
173
174 #Call the base class constructor
175 super(Exporter, self).__init__(config=config, **kw)
151 176
152 177 #Create a Latex environment if the user is exporting latex.
153 178 if self.tex_environement:
@@ -289,10 +314,66 b' class Exporter(Configurable):'
289 314 return nb, resources
290 315
291 316
317 def _get_default_options(self, export_format):
318 """ Load the default options for built in formats.
319
320 export_format: Format being exported to.
321 """
322
323 c = get_config()
324
325 #Set default data extraction priorities.
326 c.GlobalConfigurable.display_data_priority =['svg', 'png', 'latex', 'jpg', 'jpeg','text']
327 c.ExtractFigureTransformer.display_data_priority=['svg', 'png', 'latex', 'jpg', 'jpeg','text']
328 c.ConverterTemplate.display_data_priority= ['svg', 'png', 'latex', 'jpg', 'jpeg','text']
329
330 #For most (or all cases), the template file name matches the format name.
331 c.ConverterTemplate.template_file = export_format
332
333 if export_format == "basichtml" or "fullhtml" or "reveal":
334 c.CSSHtmlHeaderTransformer.enabled=True
335 if export_format == 'reveal'
336 c.NbconvertApp.fileext='reveal.html'
337 else:
338 c.NbconvertApp.fileext='html'
339
340 elif export_format == "latex_sphinx_howto" or export_format == "latex_sphinx_manual":
341
342 #Turn on latex environment
343 c.ConverterTemplate.tex_environement=True
344
345 #Standard latex extension
346 c.NbconvertApp.fileext='tex'
347
348 #Prioritize latex extraction for latex exports.
349 c.GlobalConfigurable.display_data_priority =['latex', 'svg', 'png', 'jpg', 'jpeg' , 'text']
350 c.ExtractFigureTransformer.display_data_priority=['latex', 'svg', 'png', 'jpg', 'jpeg']
351 c.ExtractFigureTransformer.extra_ext_map={'svg':'pdf'}
352 c.ExtractFigureTransformer.enabled=True
353
354 # Enable latex transformers (make markdown2latex work with math $.)
355 c.LatexTransformer.enabled=True
356 c.SphinxTransformer.enabled = True
357
358 elif export_format == 'markdown':
359 c.NbconvertApp.fileext='md'
360 c.ExtractFigureTransformer.enabled=True
361
362 elif export_format == 'python':
363 c.NbconvertApp.fileext='py'
364
365
366 elif export_format == 'rst':
367 c.NbconvertApp.fileext='rst'
368 c.ExtractFigureTransformer.enabled=True
369 return c
370
371
292 372 #TODO: Comment me.
293 373 def _rm_fake(strng):
294 374 return strng.replace('/files/', '')
295 375
376
296 377 #TODO: Comment me.
297 378 def _python_comment(string):
298 379 return '# '+'\n# '.join(string.split('\n'))
@@ -58,23 +58,6 b' ERROR_CONFIG_NOT_FOUND = "Config file for profile \'%s\' not found, giving up."'
58 58 class NbconvertApp(Application):
59 59 """A basic application to convert ipynb files"""
60 60
61 stdout = Bool(
62 True, config=True,
63 help="""Whether to print the converted ipynb file to stdout
64 "use full do diff files without actually writing a new file"""
65 )
66
67 write = Bool(
68 False, config=True,
69 help="""Should the converted notebook file be written to disk
70 along with potential extracted resources."""
71 )
72
73 fileext = Unicode(
74 'txt', config=True,
75 help="Extension of the file that should be written to disk"
76 )
77
78 61 aliases = {
79 62 'stdout':'NbconvertApp.stdout',
80 63 'write':'NbconvertApp.write'
@@ -99,42 +82,12 b' class NbconvertApp(Application):'
99 82 self.classes.insert(0, GlobalConfigurable)
100 83
101 84
102 def load_config_file(self, profile_name):
103 """Load a config file from the config file dir
104
105 profile_name : {string} name of the profile file to load without file
106 extension.
107 """
108
109 #Try to load the config file. If the file isn't found, catch the
110 #exception.
111 try:
112 Application.load_config_file(
113 self,
114 profile_name + '.py',
115 path=[os.path.join(NBCONVERT_DIR, 'profile')]
116 )
117 return True
118
119 except ConfigFileNotFound:
120 self.log.warn(ERROR_CONFIG_NOT_FOUND, profile_name)
121 return False
122
123
124 85 def start(self, argv=None):
125 86 """Convert a notebook in one step"""
126 87
127 88 #Parse the commandline options.
128 89 self.parse_command_line(argv)
129 90
130 #Load an addition config file if specified by the user via the
131 #commandline.
132 cl_config = self.config
133 profile_file = argv[1]
134 if not self.load_config_file(profile_file):
135 exit(1)
136 self.update_config(cl_config)
137
138 91 #Call base
139 92 super(NbconvertApp, self).start()
140 93
@@ -147,11 +100,11 b' class NbconvertApp(Application):'
147 100
148 101 #Create the Jinja template exporter. TODO: Add ability to
149 102 #import in IPYNB aswell
150 exporter = Exporter(config=self.config, preprocessors=userpreprocessors)
103 exporter = Exporter(config=self.config, preprocessors=userpreprocessors,export_format=export_format)
151 104
152 105 #Export
153 106 output, resources = exporter.from_filename(ipynb_file)
154 if self.stdout :
107 if exporter.stdout :
155 108 print(output.encode('utf-8'))
156 109
157 110 #Get the file name without the '.ipynb' (6 chars) extension and then
@@ -161,15 +114,15 b' class NbconvertApp(Application):'
161 114 out_root = ipynb_file[:-6].replace('.', '_').replace(' ', '_')
162 115
163 116 #Write file output from conversion.
164 if self.write :
165 with io.open(os.path.join(out_root+'.'+self.fileext), 'w') as f:
117 if exporter.write :
118 with io.open(os.path.join(out_root+'.'+exporter.fileext), 'w') as f:
166 119 f.write(output)
167 120
168 121 #Output any associate figures into the same "root" directory.
169 122 binkeys = resources.get('figures', {}).get('binary',{}).keys()
170 123 textkeys = resources.get('figures', {}).get('text',{}).keys()
171 124 if binkeys or textkeys :
172 if self.write:
125 if exporter.write:
173 126 files_dir = out_root+'_files'
174 127 if not os.path.exists(out_root+'_files'):
175 128 os.mkdir(files_dir)
@@ -182,7 +135,7 b' class NbconvertApp(Application):'
182 135
183 136 #Figures that weren't exported which will need to be created by the
184 137 #user. Tell the user what figures these are.
185 elif self.stdout:
138 elif exporter.stdout:
186 139 print(KEYS_PROMPT_HEAD)
187 140 print(resources['figures'].keys())
188 141 print(KEYS_PROMPT_BODY)
1 NO CONTENT: file was removed
1 NO CONTENT: file was removed
1 NO CONTENT: file was removed
1 NO CONTENT: file was removed
1 NO CONTENT: file was removed
1 NO CONTENT: file was removed
1 NO CONTENT: file was removed
1 NO CONTENT: file was removed
1 NO CONTENT: file was removed
1 NO CONTENT: file was removed
1 NO CONTENT: file was removed
1 NO CONTENT: file was removed
1 NO CONTENT: file was removed
1 NO CONTENT: file was removed
1 NO CONTENT: file was removed
1 NO CONTENT: file was removed
General Comments 0
You need to be logged in to leave comments. Login now