##// END OF EJS Templates
remove commented code
Matthias BUSSONNIER -
Show More
@@ -1,130 +1,117 b''
1 #!/usr/bin/env python
1 #!/usr/bin/env python
2 """Convert IPython notebooks to other formats, such as ReST, and HTML.
2 """Convert IPython notebooks to other formats, such as ReST, and HTML.
3
3
4 Example:
4 Example:
5 ./nbconvert.py --format rst file.ipynb
5 ./nbconvert.py --format rst file.ipynb
6
6
7 Produces 'file.rst', along with auto-generated figure files
7 Produces 'file.rst', along with auto-generated figure files
8 called nb_figure_NN.png.
8 called nb_figure_NN.png.
9 """
9 """
10 #-----------------------------------------------------------------------------
10 #-----------------------------------------------------------------------------
11 # Imports
11 # Imports
12 #-----------------------------------------------------------------------------
12 #-----------------------------------------------------------------------------
13 from __future__ import print_function
13 from __future__ import print_function
14
14
15 # From IPython
15 # From IPython
16 from IPython.external import argparse
16 from IPython.external import argparse
17
17
18 # All the stuff needed for the configurable things
18 # All the stuff needed for the configurable things
19 from IPython.config.application import Application, catch_config_error
19 from IPython.config.application import Application, catch_config_error
20 from IPython.config.configurable import Configurable, SingletonConfigurable
20 from IPython.config.configurable import Configurable, SingletonConfigurable
21 from IPython.config.loader import Config, ConfigFileNotFound
21 from IPython.config.loader import Config, ConfigFileNotFound
22 from IPython.utils.traitlets import List, Unicode, Type, Bool, Dict, CaselessStrEnum
22 from IPython.utils.traitlets import List, Unicode, Type, Bool, Dict, CaselessStrEnum
23
23
24
24
25 # local
25 # local
26 from converters.html import ConverterHTML
26 from converters.html import ConverterHTML
27 from converters.markdown import ConverterMarkdown
27 from converters.markdown import ConverterMarkdown
28 from converters.bloggerhtml import ConverterBloggerHTML
28 from converters.bloggerhtml import ConverterBloggerHTML
29 from converters.rst import ConverterRST
29 from converters.rst import ConverterRST
30 from converters.latex import ConverterLaTeX
30 from converters.latex import ConverterLaTeX
31 from converters.python import ConverterPy
31 from converters.python import ConverterPy
32 from converters.base import Converter
32 from converters.base import Converter
33
33
34
34
35 # When adding a new format, make sure to add it to the `converters`
35 # When adding a new format, make sure to add it to the `converters`
36 # dictionary below. This is used to create the list of known formats,
36 # dictionary below. This is used to create the list of known formats,
37 # which gets printed in case an unknown format is encounteres, as well
37 # which gets printed in case an unknown format is encounteres, as well
38 # as in the help
38 # as in the help
39
39
40 converters = {
40 converters = {
41 'rst': ConverterRST,
41 'rst': ConverterRST,
42 'markdown': ConverterMarkdown,
42 'markdown': ConverterMarkdown,
43 'html': ConverterHTML,
43 'html': ConverterHTML,
44 'blogger-html': ConverterBloggerHTML,
44 'blogger-html': ConverterBloggerHTML,
45 'latex': ConverterLaTeX,
45 'latex': ConverterLaTeX,
46 'py': ConverterPy,
46 'py': ConverterPy,
47 }
47 }
48
48
49 default_format = 'rst'
49 default_format = 'rst'
50
50
51 # Extract the list of known formats and mark the first format as the default.
51 # Extract the list of known formats and mark the first format as the default.
52 known_formats = ', '.join([key + " (default)" if key == default_format else key
52 known_formats = ', '.join([key + " (default)" if key == default_format else key
53 for key in converters])
53 for key in converters])
54
54
55 class NbconvertApp(Application):
55 class NbconvertApp(Application):
56
56
57
57
58 fmt = CaselessStrEnum(converters.keys(),
58 fmt = CaselessStrEnum(converters.keys(),
59 default_value='rst',
59 default_value='rst',
60 config=True,
60 config=True,
61 help="Supported conversion format")
61 help="Supported conversion format")
62
62
63 exclude = List( [],
63 exclude = List( [],
64 config=True,
64 config=True,
65 help = 'list of cells to exclude while converting')
65 help = 'list of cells to exclude while converting')
66
66
67 aliases = {
67 aliases = {
68 'format':'NbconvertApp.fmt',
68 'format':'NbconvertApp.fmt',
69 'exclude':'NbconvertApp.exclude',
69 'exclude':'NbconvertApp.exclude',
70 'highlight':'Converter.highlight_source',
70 'highlight':'Converter.highlight_source',
71 'preamble':'Converter.preamble',
71 'preamble':'Converter.preamble',
72 }
72 }
73
73
74 def __init__(self, **kwargs):
74 def __init__(self, **kwargs):
75 super(NbconvertApp, self).__init__(**kwargs)
75 super(NbconvertApp, self).__init__(**kwargs)
76 # ensure those are registerd
76 # ensure those are registerd
77 self.classes.insert(0,Converter)
77 self.classes.insert(0,Converter)
78 self.classes.insert(0,ConverterRST)
78 self.classes.insert(0,ConverterRST)
79 self.classes.insert(0,ConverterMarkdown)
79 self.classes.insert(0,ConverterMarkdown)
80 self.classes.insert(0,ConverterBloggerHTML)
80 self.classes.insert(0,ConverterBloggerHTML)
81 self.classes.insert(0,ConverterLaTeX)
81 self.classes.insert(0,ConverterLaTeX)
82 self.classes.insert(0,ConverterPy)
82 self.classes.insert(0,ConverterPy)
83
83
84 def initialize(self, argv=None):
84 def initialize(self, argv=None):
85 # don't hook up crash handler before parsing command-line
86 self.parse_command_line(argv)
85 self.parse_command_line(argv)
87 cl_config = self.config
86 cl_config = self.config
88 self.update_config(cl_config)
87 self.update_config(cl_config)
89 #self.init_crash_handler()
90 #self.foo = Cnf(config=self.config)
91 #if self.subapp is not None:
92 # stop here if subapp is taking over
93 #return
94 #cl_config = self.config
95 #self.init_profile_dir()
96 #self.init_config_files()
97 #self.load_config_file()
98 # enforce cl-opts override configfile opts:
99 #self.update_config(cl_config)
100
101
88
102 def run(self):
89 def run(self):
103 """Convert a notebook to html in one step"""
90 """Convert a notebook to html in one step"""
104 ConverterClass = converters[self.fmt]
91 ConverterClass = converters[self.fmt]
105 infile = (self.extra_args or [None])[0]
92 infile = (self.extra_args or [None])[0]
106 converter = ConverterClass(infile=infile, config=self.config)
93 converter = ConverterClass(infile=infile, config=self.config)
107 converter.render()
94 converter.render()
108
95
109 def main():
96 def main():
110 """Convert a notebook to html in one step"""
97 """Convert a notebook to html in one step"""
111 app = NbconvertApp.instance()
98 app = NbconvertApp.instance()
112 app.description = __doc__
99 app.description = __doc__
113 app.initialize()
100 app.initialize()
114 app.start()
101 app.start()
115 app.run()
102 app.run()
116 #-----------------------------------------------------------------------------
103 #-----------------------------------------------------------------------------
117 # Script main
104 # Script main
118 #-----------------------------------------------------------------------------
105 #-----------------------------------------------------------------------------
119
106
120 if __name__ == '__main__':
107 if __name__ == '__main__':
121 # TODO: consider passing file like object around, rather than filenames
108 # TODO: consider passing file like object around, rather than filenames
122 # would allow us to process stdin, or even http streams
109 # would allow us to process stdin, or even http streams
123 #parser.add_argument('infile', nargs='?', type=argparse.FileType('r'),
110 #parser.add_argument('infile', nargs='?', type=argparse.FileType('r'),
124 # default=sys.stdin)
111 # default=sys.stdin)
125
112
126 #parser.add_argument('-e', '--exclude', default='',
113 #parser.add_argument('-e', '--exclude', default='',
127 # help='Comma-separated list of cells to exclude')
114 # help='Comma-separated list of cells to exclude')
128 #exclude_cells = [s.strip() for s in args.exclude.split(',')]
115 #exclude_cells = [s.strip() for s in args.exclude.split(',')]
129
116
130 main()
117 main()
General Comments 0
You need to be logged in to leave comments. Login now