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