From fa0e3fea8ac116fc89958088421aedd1602d69df 2012-11-16 14:08:35 From: Bussonnier Matthias Date: 2012-11-16 14:08:35 Subject: [PATCH] Merge pull request #57 from maxalbert/cleanup Cleanup of format selection Two small cleanups/fixes: Much cleaner way of finding the right converter. This will result in much cleaner diffs when changing the API, adding arguments, etc. It also allows us to extract the known formats automatically. Fixed an assertion which didn't actually check the file extension but an arbitrary occurrence of 'rst' --- diff --git a/nbconvert.py b/nbconvert.py index bcdd559..cdcd3ae 100755 --- a/nbconvert.py +++ b/nbconvert.py @@ -12,7 +12,6 @@ called nb_figure_NN.png. #----------------------------------------------------------------------------- from __future__ import print_function - # From IPython from IPython.external import argparse @@ -25,35 +24,40 @@ from converters.latex import ConverterLaTeX from converters.notebook import ConverterNotebook from converters.python import ConverterPy -known_formats = "rst (default), html, blogger-html, latex, markdown, py" + +# When adding a new format, make sure to add it to the `converters` +# dictionary below. This is used to create the list of known formats, +# which gets printed in case an unknown format is encounteres, as well +# as in the help + +converters = { + 'rst': ConverterRST, + 'markdown': ConverterMarkdown, + 'html': ConverterHTML, + 'blogger-html': ConverterBloggerHTML, + 'latex': ConverterLaTeX, + 'py': ConverterPy, + } + +default_format = 'rst' + +# Extract the list of known formats and mark the first format as the default. +known_formats = ', '.join([key + " (default)" if key == default_format else key + for key in converters]) + def main(infile, format='rst'): """Convert a notebook to html in one step""" - # XXX: this is just quick and dirty for now. When adding a new format, - # make sure to add it to the `known_formats` string above, which gets - # printed in in the catch-all else, as well as in the help - if format == 'rst': - converter = ConverterRST(infile) - converter.render() - elif format == 'markdown': - converter = ConverterMarkdown(infile) - converter.render() - elif format == 'html': - converter = ConverterHTML(infile) - converter.render() - elif format == 'blogger-html': - converter = ConverterBloggerHTML(infile) - converter.render() - elif format == 'latex': - converter = ConverterLaTeX(infile) - converter.render() - elif format == 'py': - converter = ConverterPy(infile) - converter.render() - else: + + try: + ConverterClass = converters[format] + except KeyError: raise SystemExit("Unknown format '%s', " % format + "known formats are: " + known_formats) + converter = ConverterClass(infile) + converter.render() + #----------------------------------------------------------------------------- # Script main #----------------------------------------------------------------------------- diff --git a/tests/test_simple.py b/tests/test_simple.py index 69f96d8..e5befc2 100644 --- a/tests/test_simple.py +++ b/tests/test_simple.py @@ -21,7 +21,7 @@ def clean_dir(): def test_simple(): c = ConverterRST(fname) f = c.render() - nt.assert_true('rst' in f, 'changed file extension to rst') + nt.assert_true(f.endswith('.rst'), 'changed file extension to rst') @nt.with_setup(clean_dir, clean_dir)