diff --git a/IPython/nbconvert/nbconvertapp.py b/IPython/nbconvert/nbconvertapp.py index 92e8791..91016c0 100755 --- a/IPython/nbconvert/nbconvertapp.py +++ b/IPython/nbconvert/nbconvertapp.py @@ -119,6 +119,10 @@ class NbConvertApp(BaseIPythonApplication): > ipython nbconvert mynotebook.ipynb --to latex --post PDF + You can get (and serve) a Reveal.js-powered slideshow + + > ipython nbconvert myslides.ipynb --to slides --post serve + Multiple notebooks can be given at the command line in a couple of different ways: @@ -157,7 +161,8 @@ class NbConvertApp(BaseIPythonApplication): post_processor_class = DottedOrNone(config=True, help="""PostProcessor class used to write the results of the conversion""") - post_processor_aliases = {'PDF': 'IPython.nbconvert.post_processors.pdf.PDFPostProcessor'} + post_processor_aliases = {'PDF': 'IPython.nbconvert.post_processors.pdf.PDFPostProcessor', + 'serve': 'IPython.nbconvert.post_processors.serve.ServePostProcessor'} post_processor_factory = Type() def _post_processor_class_changed(self, name, old, new): @@ -292,8 +297,7 @@ class NbConvertApp(BaseIPythonApplication): if conversion_success == 0: self.print_help() sys.exit(-1) - - + #----------------------------------------------------------------------------- # Main entry point #----------------------------------------------------------------------------- diff --git a/IPython/nbconvert/post_processors/__init__.py b/IPython/nbconvert/post_processors/__init__.py index 72c4819..6cae641 100644 --- a/IPython/nbconvert/post_processors/__init__.py +++ b/IPython/nbconvert/post_processors/__init__.py @@ -1,2 +1,3 @@ from .base import PostProcessorBase from .pdf import PDFPostProcessor +from .serve import ServePostProcessor diff --git a/IPython/nbconvert/post_processors/serve.py b/IPython/nbconvert/post_processors/serve.py new file mode 100644 index 0000000..86e7dad --- /dev/null +++ b/IPython/nbconvert/post_processors/serve.py @@ -0,0 +1,58 @@ +""" +Contains postprocessor for serving nbconvert output. +""" +#----------------------------------------------------------------------------- +#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 os +import webbrowser + +from BaseHTTPServer import HTTPServer +from SimpleHTTPServer import SimpleHTTPRequestHandler + +from IPython.utils.traitlets import Unicode, Bool + +from .base import PostProcessorBase + +#----------------------------------------------------------------------------- +# Classes +#----------------------------------------------------------------------------- +class ServePostProcessor(PostProcessorBase): + """Post processor designed to serve files""" + + + build_directory = Unicode(".", config=True, + help="""Directory to write output to. Leave blank + to output to the current directory""") + + open_in_browser = Bool(True, config=True, + help="""Set to False to deactivate + the opening of the browser""") + + def call(self, input): + """ + Simple implementation to serve the build directory. + """ + + try: + os.chdir(self.build_directory) + httpd = HTTPServer(('127.0.0.1', 8000), SimpleHTTPRequestHandler) + sa = httpd.socket.getsockname() + name = input[2:] + url = "http://" + sa[0] + ":" + str(sa[1]) + "/" + name + if self.open_in_browser: + webbrowser.open(url, new=2) + print("Serving " + name + " on " + url) + print("Use Control-C to stop this server.") + httpd.serve_forever() + except KeyboardInterrupt: + print("The server is shut down.") diff --git a/IPython/nbconvert/writers/files.py b/IPython/nbconvert/writers/files.py index 203f460..0d6c173 100644 --- a/IPython/nbconvert/writers/files.py +++ b/IPython/nbconvert/writers/files.py @@ -99,4 +99,4 @@ class FilesWriter(WriterBase): # Write conversion results. with io.open(dest, 'w') as f: f.write(output) - return dest \ No newline at end of file + return dest \ No newline at end of file