From 757b104917ec0a8ee2a019837aaf770a59087497 2013-07-16 12:56:27 From: Jonathan Frederic Date: 2013-07-16 12:56:27 Subject: [PATCH] Rename SVG2PDFTransformer class --- diff --git a/IPython/nbconvert/exporters/latex.py b/IPython/nbconvert/exporters/latex.py index f12e401..621c20e 100755 --- a/IPython/nbconvert/exporters/latex.py +++ b/IPython/nbconvert/exporters/latex.py @@ -71,7 +71,7 @@ class LatexExporter(Exporter): default_transformers = List([transformers.ExtractFigureTransformer, transformers.CSSHTMLHeaderTransformer, transformers.LatexTransformer, - transformers.Svg2PdfTransformer], + transformers.SVG2PDFTransformer], config=True, help="""List of transformers available by default, by name, namespace, instance, or type.""") @@ -99,7 +99,7 @@ class LatexExporter(Exporter): 'ExtractFigureTransformer': { 'enabled':True }, - 'Svg2PdfTransformer': { + 'SVG2PDFTransformer': { 'enabled':True }, 'LatexTransformer': { diff --git a/IPython/nbconvert/nbconvertapp.py.orig b/IPython/nbconvert/nbconvertapp.py.orig new file mode 100755 index 0000000..d110d38 --- /dev/null +++ b/IPython/nbconvert/nbconvertapp.py.orig @@ -0,0 +1,203 @@ +#!/usr/bin/env python +"""NBConvert is a utility for conversion of IPYNB files. + +Commandline interface for the NBConvert conversion utility. Read the +readme.rst for usage information +""" +#----------------------------------------------------------------------------- +#Copyright (c) 2013, the IPython Development Team. +# +#Distributed under the terms of the Modified BSD License. +# +#The full license is in the file COPYING.txt, distributed with this software. +#----------------------------------------------------------------------------- + +#----------------------------------------------------------------------------- +#Imports +#----------------------------------------------------------------------------- + +#Stdlib imports +from __future__ import print_function +import sys +import os +import glob + +#From IPython +from IPython.core.application import BaseIPythonApplication +from IPython.config.application import catch_config_error +from IPython.utils.traitlets import Unicode, List, Instance, DottedObjectName, Type +from IPython.utils.importstring import import_item + +from .exporters.export import export_by_name, get_export_names, ExporterNameError +from .exporters.exporter import Exporter +from .writers.base import WriterBase +from .utils.base import NbConvertBase + +#----------------------------------------------------------------------------- +#Classes and functions +#----------------------------------------------------------------------------- + +class NbConvertApp(BaseIPythonApplication): + """Application used to convert to and from notebook file type (*.ipynb)""" + +<<<<<<< HEAD + name = 'ipython-nbconvert' +======= + name = Unicode(u'ipython-nbconvert') + + config_file_name = Unicode(u'ipython_nbconvert_config.py') +>>>>>>> Moved config_file_name up + + description = Unicode( + u"""This application is used to convert notebook files (*.ipynb). + An ipython config file can be used to batch convert notebooks in the + current directory.""") + + examples = Unicode(u""" + Running `ipython nbconvert` will read the directory config file and then + apply it to one or more notebooks. + + Multiple notebooks can be given at the command line in a couple of + different ways: + + > ipython nbconvert notebook*.ipynb + > ipython nbconvert notebook1.ipynb notebook2.ipynb + > ipython nbconvert # this will use the config file to fill in the notebooks + """) + + #Writer specific variables + writer = Instance('IPython.nbconvert.writers.base.WriterBase', + help="""Instance of the writer class used to write the + results of the conversion.""") + writer_class = DottedObjectName('FilesWriter', config=True, + help="""Writer class used to write the + results of the conversion""") + writer_aliases = {'FilesWriter': 'IPython.nbconvert.writers.files.FilesWriter', + 'DebugWriter': 'IPython.nbconvert.writers.debug.DebugWriter', + 'StdoutWriter': 'IPython.nbconvert.writers.stdout.StdoutWriter'} + writer_factory = Type() + + def _writer_class_changed(self, name, old, new): + if new in self.writer_aliases: + new = self.writer_aliases[new] + self.writer_factory = import_item(new) + + + #Other configurable variables + export_format = Unicode( + "", config=True, + help="""If specified, nbconvert will convert the document(s) specified + using this format.""") + + notebooks = List([], config=True, help="""List of notebooks to convert. + Search patterns are supported.""") + + nbconvert_aliases = {'format':'NbConvertApp.export_format', + 'notebooks':'NbConvertApp.notebooks', + 'writer':'NbConvertApp.writer_class'} + + + @catch_config_error + def initialize(self, argv=None): + self.aliases.update(self.nbconvert_aliases) + + super(NbConvertApp, self).initialize(argv) + + #Register class here to have help with help all + self.classes.insert(0, Exporter) + self.classes.insert(0, WriterBase) + self.classes.insert(0, NbConvertBase) + + #Init + self.init_config(self.extra_args) + self.init_writer() + + + def init_config(self, extra_args): + """ + Add notebooks to the config if needed. Glob each notebook to replace + notebook patterns with filenames. + """ + + #Get any additional notebook patterns from the commandline + if len(extra_args) > 0: + for pattern in extra_args: + self.notebooks.append(pattern) + + #Use glob to replace all the notebook patterns with filenames. + filenames = [] + for pattern in self.notebooks: + for filename in glob.glob(pattern): + if not filename in filenames: + filenames.append(filename) + self.notebooks = filenames + + + def init_writer(self): + """ + Initialize the writer (which is stateless) + """ + self._writer_class_changed(None, self.writer_class, self.writer_class) + self.writer = self.writer_factory(parent=self) + + + def start(self, argv=None): + """ + Ran after initiialization completed + """ + super(NbConvertApp, self).start() + self.convert_notebooks() + + + def convert_notebooks(self): + """ + Convert the notebooks in the self.notebook traitlet + """ + #Export each notebook + conversion_success = 0 + for notebook_filename in self.notebooks: + + #Get a unique key for the notebook and set it in the resources object. + basename = os.path.basename(notebook_filename) + notebook_name = basename[:basename.rfind('.')] + resources = {} + resources['unique_key'] = notebook_name + + #Try to export + try: + output, resources = export_by_name(self.export_format, + notebook_filename, + resources=resources, + config=self.config) + except ExporterNameError as e: + print("Error: '%s' exporter not found." % self.export_format, + file=sys.stderr) + print("Known exporters are:", + "\n\t" + "\n\t".join(get_export_names()), + file=sys.stderr) + sys.exit(-1) + #except Exception as e: + #print("Error: could not export '%s'" % notebook_filename, file=sys.stderr) + #print(e, file=sys.stderr) + else: + self.writer.write(output, resources, notebook_name=notebook_name) + conversion_success += 1 + + #If nothing was converted successfully, help the user. + if conversion_success == 0: + + #No notebooks were specified, show help. + if len(self.notebooks) == 0: + self.print_help() + + #Notebooks were specified, but not converted successfully. Show how + #to access help. + else: + print('For help, use "ipython nbconvert --help"') + + +#----------------------------------------------------------------------------- +# Main entry point +#----------------------------------------------------------------------------- + +launch_new_instance = NbConvertApp.launch_instance diff --git a/IPython/nbconvert/transformers/SVG2PDF.py b/IPython/nbconvert/transformers/SVG2PDF.py index 1aa9c69..a692601 100644 --- a/IPython/nbconvert/transformers/SVG2PDF.py +++ b/IPython/nbconvert/transformers/SVG2PDF.py @@ -35,7 +35,7 @@ INKSCAPE_OSX_COMMAND = '/Applications/Inkscape.app/Contents/Resources/bin/inksca # Classes #----------------------------------------------------------------------------- -class Svg2PdfTransformer(ConvertFiguresTransformer): +class SVG2PDFTransformer(ConvertFiguresTransformer): """ Converts all of the outputs in a notebook from one format to another. """ diff --git a/IPython/nbconvert/transformers/__init__.py b/IPython/nbconvert/transformers/__init__.py index 9351bfc..5d98b87 100755 --- a/IPython/nbconvert/transformers/__init__.py +++ b/IPython/nbconvert/transformers/__init__.py @@ -1,7 +1,7 @@ # Class base Transformers from .base import ConfigurableTransformer from .convertfigures import ConvertFiguresTransformer -from .SVG2PDF import Svg2PdfTransformer +from .SVG2PDF import SVG2PDFTransformer from .extractfigure import ExtractFigureTransformer from .revealhelp import RevealHelpTransformer from .latex import LatexTransformer