Show More
@@ -1,167 +1,125 b'' | |||
|
1 | 1 | #!/usr/bin/env python |
|
2 | 2 | """Convert IPython notebooks to other formats, such as ReST, and HTML. |
|
3 | 3 | |
|
4 | 4 | Example: |
|
5 | 5 | ./nbconvert.py --format rst file.ipynb |
|
6 | 6 | |
|
7 | 7 | Produces 'file.rst', along with auto-generated figure files |
|
8 | 8 | called nb_figure_NN.png. |
|
9 | 9 | """ |
|
10 | 10 | #----------------------------------------------------------------------------- |
|
11 | 11 | # Imports |
|
12 | 12 | #----------------------------------------------------------------------------- |
|
13 | 13 | from __future__ import print_function |
|
14 | 14 | |
|
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 |
|
21 | 28 | from converters.bloggerhtml import ConverterBloggerHTML |
|
22 | 29 | 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` |
|
29 | 37 | # dictionary below. This is used to create the list of known formats, |
|
30 | 38 | # which gets printed in case an unknown format is encounteres, as well |
|
31 | 39 | # as in the help |
|
32 | 40 | |
|
33 | 41 | converters = { |
|
34 | 42 | 'rst': ConverterRST, |
|
35 | 43 | 'markdown': ConverterMarkdown, |
|
36 | 44 | 'html': ConverterHTML, |
|
37 | 45 | 'blogger-html': ConverterBloggerHTML, |
|
38 | 46 | 'latex': ConverterLaTeX, |
|
39 | 47 | 'py': ConverterPy, |
|
40 | 48 | 'reveal': ConverterReveal, |
|
41 | 49 | } |
|
42 | 50 | |
|
43 | 51 | default_format = 'rst' |
|
44 | 52 | |
|
45 | 53 | # Extract the list of known formats and mark the first format as the default. |
|
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 |
|
|
|
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', |
|
73 | 71 | 'exclude':'NbconvertApp.exclude', |
|
74 | 72 | 'highlight':'Converter.highlight_source', |
|
75 | 73 | 'preamble':'Converter.preamble', |
|
76 | 74 | } |
|
77 | 75 | |
|
78 | 76 | def __init__(self, **kwargs): |
|
79 | 77 | super(NbconvertApp, self).__init__(**kwargs) |
|
80 | 78 | # ensure those are registerd |
|
81 | 79 | self.classes.insert(0,Converter) |
|
82 | 80 | self.classes.insert(0,ConverterRST) |
|
83 | 81 | self.classes.insert(0,ConverterMarkdown) |
|
84 | 82 | self.classes.insert(0,ConverterBloggerHTML) |
|
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(): |
|
134 | 99 | """Convert a notebook to html in one step""" |
|
135 | 100 | app = NbconvertApp.instance() |
|
136 | 101 | app.description = __doc__ |
|
137 | 102 | print(""" |
|
138 | 103 | ====================================================== |
|
139 | 104 | Warning, we are deprecating this version of nbconvert, |
|
140 | 105 | please consider using the new version. |
|
141 | 106 | ====================================================== |
|
142 | 107 | """) |
|
143 | 108 | app.initialize() |
|
144 | 109 | app.start() |
|
145 | 110 | app.run() |
|
146 | 111 | #----------------------------------------------------------------------------- |
|
147 | 112 | # Script main |
|
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