diff --git a/converters/base.py b/converters/base.py index a1b0ecb..99038a0 100755 --- a/converters/base.py +++ b/converters/base.py @@ -28,6 +28,8 @@ from types import FunctionType # IPython imports from IPython.nbformat import current as nbformat +from IPython.config.configurable import Configurable, SingletonConfigurable +from IPython.utils.traitlets import List, Unicode, Type, Bool, Dict, CaselessStrEnum # Our own imports from .utils import remove_fake_files_url @@ -94,8 +96,9 @@ class DocStringInheritor(type): return type.__new__(meta, classname, bases, newClassDict) -class Converter(object): - __metaclass__ = DocStringInheritor + +class Converter(Configurable): + #__metaclass__ = DocStringInheritor #------------------------------------------------------------------------- # Class-level attributes determining the behaviour of the class but # probably not varying from instance to instance. @@ -110,33 +113,44 @@ class Converter(object): # Instance-level attributes that are set in the constructor for this # class. #------------------------------------------------------------------------- - infile = str() - highlight_source = True - infile_dir = str() - infile_root = str() - clean_name = str() - files_dir = str() - outbase = str() + infile = Unicode() + + highlight_source = Bool(True, + config=True, + help="Enable syntax highlighting for code blocks.") + + preamble = Unicode("" , + config=True, + help="Path to a user-specified preamble file") + + infile_dir = Unicode() + infile_root = Unicode() + clean_name = Unicode() + files_dir = Unicode() + outbase = Unicode() #------------------------------------------------------------------------- # Instance-level attributes that are set by other methods in the base # class. #------------------------------------------------------------------------- figures_counter = 0 - output = unicode() + output = Unicode() #------------------------------------------------------------------------- # Instance-level attributes that are not actually mentioned further # in this class. TODO: Could they be usefully moved to a subclass? #------------------------------------------------------------------------- - with_preamble = True + with_preamble = Bool(True,config=True) user_preamble = None raw_as_verbatim = False - def __init__(self, infile, highlight_source=True, exclude=[], **kw): + + def __init__(self, infile=None, config=None, exclude=[] **kw): + super(Converter,self).__init__(config=config) + + #DocStringInheritor.__init__(self=config) # N.B. Initialized in the same order as defined above. Please try to # keep in this way for readability's sake. self.exclude_cells = exclude self.infile = infile - self.highlight_source = highlight_source self.infile_dir, infile_root = os.path.split(infile) self.infile_root = os.path.splitext(infile_root)[0] self.clean_name = clean_filename(self.infile_root) diff --git a/nbconvert.py b/nbconvert.py index 094a514..975334c 100755 --- a/nbconvert.py +++ b/nbconvert.py @@ -17,7 +17,7 @@ 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 +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 @@ -29,6 +29,7 @@ from converters.bloggerhtml import ConverterBloggerHTML from converters.rst import ConverterRST from converters.latex import ConverterLaTeX from converters.python import ConverterPy +from converters.base import Converter # When adding a new format, make sure to add it to the `converters` @@ -53,21 +54,12 @@ known_formats = ', '.join([key + " (default)" if key == default_format else key class NbconvertApp(Application): - name = Unicode('thisIsNbconvertApp',config=True) fmt = CaselessStrEnum(converters.keys(), - default_value='rst', + default_value='rst', config=True, help="Supported conversion format") - preamble = Unicode("" , - config=True, - help="Path to a user-specified preamble file") - - highlight = Bool(True, - config=True, - help="Enable syntax highlighting for code blocks.") - exclude = List( [], config=True, help = 'list of cells to exclude while converting') @@ -80,19 +72,25 @@ class NbconvertApp(Application): aliases = { 'format':'NbconvertApp.fmt', - 'highlight':'NbconvertApp.highlight', - 'preamble':'NbconvertApp.preamble', + '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 - print(self.config) self.update_config(cl_config) #self.init_crash_handler() #self.foo = Cnf(config=self.config) @@ -110,14 +108,13 @@ class NbconvertApp(Application): def run(self): """Convert a notebook to html in one step""" ConverterClass = converters[self.fmt] - - converter = ConverterClass(self.infile, highlight_source=self.highlight, preamble=self.preamble, exclude=self.exclude) + converter = ConverterClass(infile=self.extra_args[0], config=self.config) converter.render() def main(): """Convert a notebook to html in one step""" app = NbconvertApp.instance() - print(app.classes) + app.description = __doc__ app.initialize() app.start() app.run() @@ -126,20 +123,13 @@ def main(): #----------------------------------------------------------------------------- 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()