Show More
@@ -1,167 +1,125 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 | |||
|
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 | # local |
|
25 | # local | |
19 | from converters.html import ConverterHTML |
|
26 | from converters.html import ConverterHTML | |
20 | from converters.markdown import ConverterMarkdown |
|
27 | from converters.markdown import ConverterMarkdown | |
21 | from converters.bloggerhtml import ConverterBloggerHTML |
|
28 | from converters.bloggerhtml import ConverterBloggerHTML | |
22 | from converters.rst import ConverterRST |
|
29 | from converters.rst import ConverterRST | |
23 | from converters.latex import ConverterLaTeX |
|
30 | from converters.latex import ConverterLaTeX | |
24 | from converters.python import ConverterPy |
|
31 | from converters.python import ConverterPy | |
25 | from converters.reveal import ConverterReveal |
|
32 | from converters.reveal import ConverterReveal | |
|
33 | from converters.base import Converter | |||
26 |
|
34 | |||
27 |
|
35 | |||
28 | # When adding a new format, make sure to add it to the `converters` |
|
36 | # When adding a new format, make sure to add it to the `converters` | |
29 | # dictionary below. This is used to create the list of known formats, |
|
37 | # dictionary below. This is used to create the list of known formats, | |
30 | # which gets printed in case an unknown format is encounteres, as well |
|
38 | # which gets printed in case an unknown format is encounteres, as well | |
31 | # as in the help |
|
39 | # as in the help | |
32 |
|
40 | |||
33 | converters = { |
|
41 | converters = { | |
34 | 'rst': ConverterRST, |
|
42 | 'rst': ConverterRST, | |
35 | 'markdown': ConverterMarkdown, |
|
43 | 'markdown': ConverterMarkdown, | |
36 | 'html': ConverterHTML, |
|
44 | 'html': ConverterHTML, | |
37 | 'blogger-html': ConverterBloggerHTML, |
|
45 | 'blogger-html': ConverterBloggerHTML, | |
38 | 'latex': ConverterLaTeX, |
|
46 | 'latex': ConverterLaTeX, | |
39 | 'py': ConverterPy, |
|
47 | 'py': ConverterPy, | |
40 | 'reveal': ConverterReveal, |
|
48 | 'reveal': ConverterReveal, | |
41 | } |
|
49 | } | |
42 |
|
50 | |||
43 | default_format = 'rst' |
|
51 | default_format = 'rst' | |
44 |
|
52 | |||
45 | # Extract the list of known formats and mark the first format as the default. |
|
53 | # Extract the list of known formats and mark the first format as the default. | |
46 | known_formats = ', '.join([key + " (default)" if key == default_format else key |
|
54 | known_formats = ', '.join([key + " (default)" if key == default_format else key | |
47 | for key in converters]) |
|
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, |
|
60 | fmt = CaselessStrEnum(converters.keys(), | |
65 |
|
|
61 | default_value='rst', | |
66 | help="Enable syntax highlighting for code blocks.") |
|
62 | config=True, | |
|
63 | help="Supported conversion format") | |||
67 |
|
64 | |||
68 | converter = ConverterClass(infile) |
|
65 | exclude = List( [], | |
69 | converter.render() |
|
66 | config=True, | |
|
67 | help = 'list of cells to exclude while converting') | |||
70 |
|
68 | |||
71 | aliases = { |
|
69 | aliases = { | |
72 | 'format':'NbconvertApp.fmt', |
|
70 | 'format':'NbconvertApp.fmt', | |
73 | 'exclude':'NbconvertApp.exclude', |
|
71 | 'exclude':'NbconvertApp.exclude', | |
74 | 'highlight':'Converter.highlight_source', |
|
72 | 'highlight':'Converter.highlight_source', | |
75 | 'preamble':'Converter.preamble', |
|
73 | 'preamble':'Converter.preamble', | |
76 | } |
|
74 | } | |
77 |
|
75 | |||
78 | def __init__(self, **kwargs): |
|
76 | def __init__(self, **kwargs): | |
79 | super(NbconvertApp, self).__init__(**kwargs) |
|
77 | super(NbconvertApp, self).__init__(**kwargs) | |
80 | # ensure those are registerd |
|
78 | # ensure those are registerd | |
81 | self.classes.insert(0,Converter) |
|
79 | self.classes.insert(0,Converter) | |
82 | self.classes.insert(0,ConverterRST) |
|
80 | self.classes.insert(0,ConverterRST) | |
83 | self.classes.insert(0,ConverterMarkdown) |
|
81 | self.classes.insert(0,ConverterMarkdown) | |
84 | self.classes.insert(0,ConverterBloggerHTML) |
|
82 | self.classes.insert(0,ConverterBloggerHTML) | |
85 | self.classes.insert(0,ConverterLaTeX) |
|
83 | self.classes.insert(0,ConverterLaTeX) | |
86 | self.classes.insert(0,ConverterPy) |
|
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 | def initialize(self, argv=None): |
|
86 | def initialize(self, argv=None): | |
110 | # don't hook up crash handler before parsing command-line |
|
|||
111 | self.parse_command_line(argv) |
|
87 | self.parse_command_line(argv) | |
112 | cl_config = self.config |
|
88 | cl_config = self.config | |
113 | self.update_config(cl_config) |
|
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 | def run(self): |
|
91 | def run(self): | |
128 | """Convert a notebook in one step""" |
|
92 | """Convert a notebook in one step""" | |
129 | ConverterClass = converters[self.fmt] |
|
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 | converter.render() |
|
96 | converter.render() | |
132 |
|
97 | |||
133 | def main(): |
|
98 | def main(): | |
134 | """Convert a notebook to html in one step""" |
|
99 | """Convert a notebook to html in one step""" | |
135 | app = NbconvertApp.instance() |
|
100 | app = NbconvertApp.instance() | |
136 | app.description = __doc__ |
|
101 | app.description = __doc__ | |
137 | print(""" |
|
102 | print(""" | |
138 | ====================================================== |
|
103 | ====================================================== | |
139 | Warning, we are deprecating this version of nbconvert, |
|
104 | Warning, we are deprecating this version of nbconvert, | |
140 | please consider using the new version. |
|
105 | please consider using the new version. | |
141 | ====================================================== |
|
106 | ====================================================== | |
142 | """) |
|
107 | """) | |
143 | app.initialize() |
|
108 | app.initialize() | |
144 | app.start() |
|
109 | app.start() | |
145 | app.run() |
|
110 | app.run() | |
146 | #----------------------------------------------------------------------------- |
|
111 | #----------------------------------------------------------------------------- | |
147 | # Script main |
|
112 | # Script main | |
148 | #----------------------------------------------------------------------------- |
|
113 | #----------------------------------------------------------------------------- | |
149 |
|
114 | |||
150 | if __name__ == '__main__': |
|
115 | if __name__ == '__main__': | |
151 | parser = argparse.ArgumentParser(description=__doc__, |
|
|||
152 | formatter_class=argparse.RawTextHelpFormatter) |
|
|||
153 | # TODO: consider passing file like object around, rather than filenames |
|
116 | # TODO: consider passing file like object around, rather than filenames | |
154 | # would allow us to process stdin, or even http streams |
|
117 | # would allow us to process stdin, or even http streams | |
155 | #parser.add_argument('infile', nargs='?', type=argparse.FileType('r'), |
|
118 | #parser.add_argument('infile', nargs='?', type=argparse.FileType('r'), | |
156 | # default=sys.stdin) |
|
119 | # default=sys.stdin) | |
157 |
|
120 | |||
158 | #Require a filename as a positional argument |
|
|||
159 | #parser.add_argument('infile', nargs=1) |
|
|||
160 | #parser.add_argument('-e', '--exclude', default='', |
|
121 | #parser.add_argument('-e', '--exclude', default='', | |
161 | # help='Comma-separated list of cells to exclude') |
|
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 | #exclude_cells = [s.strip() for s in args.exclude.split(',')] |
|
123 | #exclude_cells = [s.strip() for s in args.exclude.split(',')] | |
166 |
|
124 | |||
167 | main() |
|
125 | main() |
General Comments 0
You need to be logged in to leave comments.
Login now