diff --git a/nbconvert.py b/nbconvert.py index 68c1ca2..66288ae 100755 --- a/nbconvert.py +++ b/nbconvert.py @@ -15,6 +15,13 @@ from __future__ import print_function # From IPython from IPython.external import argparse +# All the stuff needed for the configurable things +from IPython.config.application import Application, catch_config_error +from IPython.config.configurable import Configurable, SingletonConfigurable +from IPython.config.loader import Config, ConfigFileNotFound +from IPython.utils.traitlets import List, Unicode, Type, Bool, Dict, CaselessStrEnum + + # local from converters.html import ConverterHTML from converters.markdown import ConverterMarkdown @@ -23,6 +30,7 @@ from converters.rst import ConverterRST from converters.latex import ConverterLaTeX from converters.python import ConverterPy from converters.reveal import ConverterReveal +from converters.base import Converter # When adding a new format, make sure to add it to the `converters` @@ -46,27 +54,17 @@ default_format = 'rst' known_formats = ', '.join([key + " (default)" if key == default_format else key for key in converters]) +class NbconvertApp(Application): - name = Unicode('thisIsNbconvertApp',config=True) - -def main(infile, format='rst', preamble=None, exclude=None): - """Convert a notebook to html in one step""" - try: - ConverterClass = converters[format] - except KeyError: - raise SystemExit("Unknown format '%s', " % format + - "known formats are: " + known_formats) - - preamble = Unicode("" , - config=True, - help="Path to a user-specified preamble file") - highlight = Bool(True, - config=True, - help="Enable syntax highlighting for code blocks.") + fmt = CaselessStrEnum(converters.keys(), + default_value='rst', + config=True, + help="Supported conversion format") - converter = ConverterClass(infile) - converter.render() + exclude = List( [], + config=True, + help = 'list of cells to exclude while converting') aliases = { 'format':'NbconvertApp.fmt', @@ -85,49 +83,16 @@ def main(infile, format='rst', preamble=None, exclude=None): self.classes.insert(0,ConverterLaTeX) self.classes.insert(0,ConverterPy) - converter = ConverterClass(infile, highlight_source=highlight_source, exclude=exclude) - converter.render() - - - aliases = { - 'format':'NbconvertApp.fmt', - 'highlight':'Converter.highlight_source', - 'preamble':'Converter.preamble', - 'infile' : 'NbconvertApp.infile' - } - - def __init__(self, **kwargs): - super(NbconvertApp, self).__init__(**kwargs) - # ensure those are registerd - self.classes.insert(0,Converter) - self.classes.insert(0,ConverterRST) - self.classes.insert(0,ConverterMarkdown) - self.classes.insert(0,ConverterBloggerHTML) - self.classes.insert(0,ConverterLaTeX) - self.classes.insert(0,ConverterPy) - def initialize(self, argv=None): - # don't hook up crash handler before parsing command-line self.parse_command_line(argv) cl_config = self.config self.update_config(cl_config) - #self.init_crash_handler() - #self.foo = Cnf(config=self.config) - #if self.subapp is not None: - # stop here if subapp is taking over - #return - #cl_config = self.config - #self.init_profile_dir() - #self.init_config_files() - #self.load_config_file() - # enforce cl-opts override configfile opts: - #self.update_config(cl_config) - def run(self): """Convert a notebook in one step""" ConverterClass = converters[self.fmt] - converter = ConverterClass(infile=self.extra_args[0], config=self.config) + infile = (self.extra_args or [None])[0] + converter = ConverterClass(infile=infile, config=self.config) converter.render() def main(): @@ -148,20 +113,13 @@ please consider using the new version. #----------------------------------------------------------------------------- if __name__ == '__main__': - parser = argparse.ArgumentParser(description=__doc__, - formatter_class=argparse.RawTextHelpFormatter) # TODO: consider passing file like object around, rather than filenames # would allow us to process stdin, or even http streams #parser.add_argument('infile', nargs='?', type=argparse.FileType('r'), # default=sys.stdin) - #Require a filename as a positional argument - #parser.add_argument('infile', nargs=1) #parser.add_argument('-e', '--exclude', default='', # help='Comma-separated list of cells to exclude') - #parser.add_argument('-H', '--no-highlighting', action='store_false', - # help='Disable syntax highlighting for code blocks.') - #args = parser.parse_args() #exclude_cells = [s.strip() for s in args.exclude.split(',')] main()