##// END OF EJS Templates
bring back nbconvertapp
Matthias BUSSONNIER -
Show More
@@ -15,6 +15,13 b' from __future__ import print_function'
15 15 # From IPython
16 16 from IPython.external import argparse
17 17
18 # All the stuff needed for the configurable things
19 from IPython.config.application import Application, catch_config_error
20 from IPython.config.configurable import Configurable, SingletonConfigurable
21 from IPython.config.loader import Config, ConfigFileNotFound
22 from IPython.utils.traitlets import List, Unicode, Type, Bool, Dict, CaselessStrEnum
23
24
18 25 # local
19 26 from converters.html import ConverterHTML
20 27 from converters.markdown import ConverterMarkdown
@@ -23,6 +30,7 b' from converters.rst import ConverterRST'
23 30 from converters.latex import ConverterLaTeX
24 31 from converters.python import ConverterPy
25 32 from converters.reveal import ConverterReveal
33 from converters.base import Converter
26 34
27 35
28 36 # When adding a new format, make sure to add it to the `converters`
@@ -46,27 +54,17 b" default_format = 'rst'"
46 54 known_formats = ', '.join([key + " (default)" if key == default_format else key
47 55 for key in converters])
48 56
57 class NbconvertApp(Application):
49 58
50 name = Unicode('thisIsNbconvertApp',config=True)
51
52 def main(infile, format='rst', preamble=None, exclude=None):
53 """Convert a notebook to html in one step"""
54 try:
55 ConverterClass = converters[format]
56 except KeyError:
57 raise SystemExit("Unknown format '%s', " % format +
58 "known formats are: " + known_formats)
59
60 preamble = Unicode("" ,
61 config=True,
62 help="Path to a user-specified preamble file")
63 59
64 highlight = Bool(True,
65 config=True,
66 help="Enable syntax highlighting for code blocks.")
60 fmt = CaselessStrEnum(converters.keys(),
61 default_value='rst',
62 config=True,
63 help="Supported conversion format")
67 64
68 converter = ConverterClass(infile)
69 converter.render()
65 exclude = List( [],
66 config=True,
67 help = 'list of cells to exclude while converting')
70 68
71 69 aliases = {
72 70 'format':'NbconvertApp.fmt',
@@ -85,49 +83,16 b" def main(infile, format='rst', preamble=None, exclude=None):"
85 83 self.classes.insert(0,ConverterLaTeX)
86 84 self.classes.insert(0,ConverterPy)
87 85
88 converter = ConverterClass(infile, highlight_source=highlight_source, exclude=exclude)
89 converter.render()
90
91
92 aliases = {
93 'format':'NbconvertApp.fmt',
94 'highlight':'Converter.highlight_source',
95 'preamble':'Converter.preamble',
96 'infile' : 'NbconvertApp.infile'
97 }
98
99 def __init__(self, **kwargs):
100 super(NbconvertApp, self).__init__(**kwargs)
101 # ensure those are registerd
102 self.classes.insert(0,Converter)
103 self.classes.insert(0,ConverterRST)
104 self.classes.insert(0,ConverterMarkdown)
105 self.classes.insert(0,ConverterBloggerHTML)
106 self.classes.insert(0,ConverterLaTeX)
107 self.classes.insert(0,ConverterPy)
108
109 86 def initialize(self, argv=None):
110 # don't hook up crash handler before parsing command-line
111 87 self.parse_command_line(argv)
112 88 cl_config = self.config
113 89 self.update_config(cl_config)
114 #self.init_crash_handler()
115 #self.foo = Cnf(config=self.config)
116 #if self.subapp is not None:
117 # stop here if subapp is taking over
118 #return
119 #cl_config = self.config
120 #self.init_profile_dir()
121 #self.init_config_files()
122 #self.load_config_file()
123 # enforce cl-opts override configfile opts:
124 #self.update_config(cl_config)
125
126 90
127 91 def run(self):
128 92 """Convert a notebook in one step"""
129 93 ConverterClass = converters[self.fmt]
130 converter = ConverterClass(infile=self.extra_args[0], config=self.config)
94 infile = (self.extra_args or [None])[0]
95 converter = ConverterClass(infile=infile, config=self.config)
131 96 converter.render()
132 97
133 98 def main():
@@ -148,20 +113,13 b' please consider using the new version.'
148 113 #-----------------------------------------------------------------------------
149 114
150 115 if __name__ == '__main__':
151 parser = argparse.ArgumentParser(description=__doc__,
152 formatter_class=argparse.RawTextHelpFormatter)
153 116 # TODO: consider passing file like object around, rather than filenames
154 117 # would allow us to process stdin, or even http streams
155 118 #parser.add_argument('infile', nargs='?', type=argparse.FileType('r'),
156 119 # default=sys.stdin)
157 120
158 #Require a filename as a positional argument
159 #parser.add_argument('infile', nargs=1)
160 121 #parser.add_argument('-e', '--exclude', default='',
161 122 # help='Comma-separated list of cells to exclude')
162 #parser.add_argument('-H', '--no-highlighting', action='store_false',
163 # help='Disable syntax highlighting for code blocks.')
164 #args = parser.parse_args()
165 123 #exclude_cells = [s.strip() for s in args.exclude.split(',')]
166 124
167 125 main()
General Comments 0
You need to be logged in to leave comments. Login now