diff --git a/IPython/nbconvert/writers/__init__.py b/IPython/nbconvert/writers/__init__.py new file mode 100644 index 0000000..f0bc939 --- /dev/null +++ b/IPython/nbconvert/writers/__init__.py @@ -0,0 +1,2 @@ +from .files import FilesWriter +from .stdout import StdoutWriter diff --git a/IPython/nbconvert/writers/base.py b/IPython/nbconvert/writers/base.py new file mode 100644 index 0000000..ba0b0cb --- /dev/null +++ b/IPython/nbconvert/writers/base.py @@ -0,0 +1,57 @@ +#!/usr/bin/env python +""" +Contains writer base class. +""" +#----------------------------------------------------------------------------- +#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 +#----------------------------------------------------------------------------- + +from IPython.utils.traitlets import List + +from ..utils.config import GlobalConfigurable + +#----------------------------------------------------------------------------- +# Classes +#----------------------------------------------------------------------------- + +class WriterBase(GlobalConfigurable): + """Consumes output from nbconvert export...() methods and writes to a + useful location. """ + + + files = List([], config=True, help=""" + List of the files that the notebook references. Files will be + included with written output.""") + + + def __init__(self, config=None, **kw): + """ + Constructor + """ + super(WriterBase, self).__init__(config=config, **kw) + + + def write(self, output, resources, **kw): + """ + Consume and write Jinja output. + + Parameters + ---------- + output : string + Conversion results. This string contains the file contents of the + converted file. + resources : dict + Resources created and filled by the nbconvert conversion process. + Includes output from transformers, such as the extract figure + transformer. + """ + + raise NotImplementedError() diff --git a/IPython/nbconvert/writers/debug.py b/IPython/nbconvert/writers/debug.py new file mode 100644 index 0000000..7089855 --- /dev/null +++ b/IPython/nbconvert/writers/debug.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python +""" +Contains debug writer. +""" +#----------------------------------------------------------------------------- +#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 +#----------------------------------------------------------------------------- + +from .base import WriterBase +from pprint import pprint + +#----------------------------------------------------------------------------- +# Classes +#----------------------------------------------------------------------------- + +class DebugWriter(WriterBase): + """Consumes output from nbconvert export...() methods and writes usefull + debugging information to the stdout. The information includes a list of + resources that were extracted from the notebook(s) during export.""" + + + def write(self, output, resources, **kw): + """ + Consume and write Jinja output. + + See base for more... + """ + + if 'figures' in resources: + print("Figures extracted from %s" % notebook_name) + print('-' * 80) + pprint.pprint(resources['figures'], indent=2, width=70) + else: + print("No figures extracted from %s" % notebook_name) + print('=' * 80) diff --git a/IPython/nbconvert/writers/files.py b/IPython/nbconvert/writers/files.py new file mode 100644 index 0000000..77a2910 --- /dev/null +++ b/IPython/nbconvert/writers/files.py @@ -0,0 +1,98 @@ +#!/usr/bin/env python +""" +Contains writer for writing nbconvert output to filesystem. +""" +#----------------------------------------------------------------------------- +#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 +#----------------------------------------------------------------------------- + +import io +import os +import shutil +import glob + +from IPython.utils.traitlets import Unicode + +from .base import WriterBase + +#----------------------------------------------------------------------------- +# Classes +#----------------------------------------------------------------------------- + +class FilesWriter(WriterBase): + """Consumes nbconvert output and produces files.""" + + + build_directory = Unicode("nbconvert_build", config=True, + help="""Directory to write output to. Leave blank + to output to the current directory""") + + + #Make sure that the output directory exists. + def _build_directory_changed(self, name, old, new): + if new and not os.path.isdir(new): + os.makedirs(new) + + + def __init__(self, **kw): + super(FilesWriter, self).__init__(**kw) + self._build_directory_changed('build_directory', self.build_directory, + self.build_directory) + + + def write(self, output, resources, notebook_name=None, **kw): + """ + Consume and write Jinja output to the file system. Output directory + is set via the 'build_directory' variable of this instance (a + configurable). + + See base for more... + """ + + #Pull the extension from the resources dict. + output_extension = resources['output_extension'] + + #Write all of the extracted resources to the destination directory. + #NOTE: WE WRITE EVERYTHING AS-IF IT'S BINARY. THE EXTRACT FIG + #TRANSFORMER SHOULD HANDLE UNIX/WINDOWS LINE ENDINGS... + for filename, data in resources.get('figures', {}).items(): + + #Determine where to write the file to + dest = os.path.join(self.build_directory, filename) + + #Write file + with io.open(dest, 'wb') as f: + f.write(data) + + #Copy referenced files to output directory + if self.build_directory: + for filename in self.files: + + #Copy files that match search pattern + for matching_filename in glob.glob(filename): + + #Make sure folder exists. + dest = os.path.join(self.build_directory, filename) + path = os.path.dirname(dest) + if not os.path.isdir(path): + os.makedirs(path) + + #Copy + shutil.copyfile(matching_filename, dest) + + #Determine where to write conversion results. + dest = notebook_name + '.' + output_extension + if self.build_directory: + dest = os.path.join(self.build_directory, dest) + + #Write conversion results. + with io.open(dest, 'w') as f: + f.write(output) diff --git a/IPython/nbconvert/writers/stdout.py b/IPython/nbconvert/writers/stdout.py new file mode 100644 index 0000000..6c9f042 --- /dev/null +++ b/IPython/nbconvert/writers/stdout.py @@ -0,0 +1,37 @@ +#!/usr/bin/env python +""" +Contains Stdout writer +""" +#----------------------------------------------------------------------------- +#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 +#----------------------------------------------------------------------------- + +from .base import WriterBase + +#----------------------------------------------------------------------------- +# Classes +#----------------------------------------------------------------------------- + +class StdoutWriter(WriterBase): + """Consumes output from nbconvert export...() methods and writes to the + stdout stream. Allows for quick debuging of nbconvert output. Using the + debug flag makes the writer pretty-print the figures contained within the + notebook.""" + + + def write(self, output, resources, **kw): + """ + Consume and write Jinja output. + + See base for more... + """ + + print(output)