From a925e26088919ddf815072881bd71b30c4ac3407 2014-06-03 22:32:53 From: Julia Evans Date: 2014-06-03 22:32:53 Subject: [PATCH] Add IPython Notebook exporter --- diff --git a/IPython/nbconvert/exporters/export.py b/IPython/nbconvert/exporters/export.py index 62f909e..c3bbd84 100644 --- a/IPython/nbconvert/exporters/export.py +++ b/IPython/nbconvert/exporters/export.py @@ -18,6 +18,7 @@ from .pdf import PDFExporter from .markdown import MarkdownExporter from .python import PythonExporter from .rst import RSTExporter +from .notebook import NotebookExporter #----------------------------------------------------------------------------- # Classes @@ -130,6 +131,7 @@ exporter_map = dict( markdown=MarkdownExporter, python=PythonExporter, rst=RSTExporter, + notebook=NotebookExporter, ) def _make_exporter(name, E): diff --git a/IPython/nbconvert/exporters/notebook.py b/IPython/nbconvert/exporters/notebook.py new file mode 100644 index 0000000..b20d7c4 --- /dev/null +++ b/IPython/nbconvert/exporters/notebook.py @@ -0,0 +1,36 @@ +"""IPython Notebook Exporter 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 .exporter import Exporter +from IPython.nbformat import current as nbformat + +#----------------------------------------------------------------------------- +# Classes +#----------------------------------------------------------------------------- + +class NotebookExporter(Exporter): + """ + Exports an IPython notebook. + """ + def _file_extension_default(self): + return 'ipynb' + + output_mimetype = 'application/json' + + def from_notebook_node(self, nb, resources=None, **kw): + nb_copy, resources = super(NotebookExporter, self).from_notebook_node(nb, resources, **kw) + output = nbformat.writes_json(nb_copy) + return output, resources + + diff --git a/IPython/nbconvert/exporters/tests/test_notebook.py b/IPython/nbconvert/exporters/tests/test_notebook.py new file mode 100644 index 0000000..56d76c2 --- /dev/null +++ b/IPython/nbconvert/exporters/tests/test_notebook.py @@ -0,0 +1,45 @@ +""" +Module with tests for notebook.py +""" + +#----------------------------------------------------------------------------- +# 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 ExportersTestsBase +from ..notebook import NotebookExporter + +#----------------------------------------------------------------------------- +# Class +#----------------------------------------------------------------------------- + +class TestNotebookExporter(ExportersTestsBase): + """Contains test functions for notebook.py""" + + exporter_class = NotebookExporter + + def test_constructor(self): + """ + Can a NotebookExporter be constructed? + """ + NotebookExporter() + + + def test_export(self): + """ + Does the NotebookExporter return the file unchanged? + """ + with open(self._get_notebook()) as f: + file_contents = f.read() + (output, resources) = NotebookExporter().from_filename(self._get_notebook()) + assert len(output) > 0 + assert output == file_contents +