Show More
@@ -1,94 +1,74 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 | # Stdlib |
|
|||
16 | import codecs |
|
|||
17 | import io |
|
|||
18 | import logging |
|
|||
19 | import os |
|
|||
20 | import pprint |
|
|||
21 | import re |
|
|||
22 | import subprocess |
|
|||
23 | import sys |
|
|||
24 | import json |
|
|||
25 | import copy |
|
|||
26 | from types import FunctionType |
|
|||
27 | from shutil import rmtree |
|
|||
28 | from markdown import markdown |
|
|||
29 |
|
15 | |||
30 | # From IPython |
|
16 | # From IPython | |
31 | from IPython.external import argparse |
|
17 | from IPython.external import argparse | |
32 | from IPython.nbformat import current as nbformat |
|
|||
33 | from IPython.utils.text import indent |
|
|||
34 | from IPython.nbformat.v3.nbjson import BytesEncoder |
|
|||
35 | from IPython.utils import path, py3compat |
|
|||
36 |
|
18 | |||
37 | # local |
|
19 | # local | |
38 | from lexers import IPythonLexer |
|
|||
39 |
|
||||
40 | from converters.html import ConverterHTML |
|
20 | from converters.html import ConverterHTML | |
41 | from converters.markdown import ConverterMarkdown |
|
21 | from converters.markdown import ConverterMarkdown | |
42 | from converters.bloggerhtml import ConverterBloggerHTML |
|
22 | from converters.bloggerhtml import ConverterBloggerHTML | |
43 | from converters.rst import ConverterRST |
|
23 | from converters.rst import ConverterRST | |
44 | from converters.latex import ConverterLaTeX |
|
24 | from converters.latex import ConverterLaTeX | |
45 | from converters.notebook import ConverterNotebook |
|
25 | from converters.notebook import ConverterNotebook | |
46 | from converters.python import ConverterPy |
|
26 | from converters.python import ConverterPy | |
47 |
|
27 | |||
48 | known_formats = "rst (default), html, blogger-html, latex, markdown, py" |
|
28 | known_formats = "rst (default), html, blogger-html, latex, markdown, py" | |
49 |
|
29 | |||
50 | def main(infile, format='rst'): |
|
30 | def main(infile, format='rst'): | |
51 | """Convert a notebook to html in one step""" |
|
31 | """Convert a notebook to html in one step""" | |
52 | # XXX: this is just quick and dirty for now. When adding a new format, |
|
32 | # XXX: this is just quick and dirty for now. When adding a new format, | |
53 | # make sure to add it to the `known_formats` string above, which gets |
|
33 | # make sure to add it to the `known_formats` string above, which gets | |
54 | # printed in in the catch-all else, as well as in the help |
|
34 | # printed in in the catch-all else, as well as in the help | |
55 | if format == 'rst': |
|
35 | if format == 'rst': | |
56 | converter = ConverterRST(infile) |
|
36 | converter = ConverterRST(infile) | |
57 | converter.render() |
|
37 | converter.render() | |
58 | elif format == 'markdown': |
|
38 | elif format == 'markdown': | |
59 | converter = ConverterMarkdown(infile) |
|
39 | converter = ConverterMarkdown(infile) | |
60 | converter.render() |
|
40 | converter.render() | |
61 | elif format == 'html': |
|
41 | elif format == 'html': | |
62 | converter = ConverterHTML(infile) |
|
42 | converter = ConverterHTML(infile) | |
63 |
|
|
43 | converter.render() | |
64 | elif format == 'blogger-html': |
|
44 | elif format == 'blogger-html': | |
65 | converter = ConverterBloggerHTML(infile) |
|
45 | converter = ConverterBloggerHTML(infile) | |
66 |
|
|
46 | converter.render() | |
67 | elif format == 'latex': |
|
47 | elif format == 'latex': | |
68 | converter = ConverterLaTeX(infile) |
|
48 | converter = ConverterLaTeX(infile) | |
69 |
|
|
49 | converter.render() | |
70 | elif format == 'py': |
|
50 | elif format == 'py': | |
71 | converter = ConverterPy(infile) |
|
51 | converter = ConverterPy(infile) | |
72 | converter.render() |
|
52 | converter.render() | |
73 | else: |
|
53 | else: | |
74 | raise SystemExit("Unknown format '%s', " % format + |
|
54 | raise SystemExit("Unknown format '%s', " % format + | |
75 | "known formats are: " + known_formats) |
|
55 | "known formats are: " + known_formats) | |
76 |
|
56 | |||
77 | #----------------------------------------------------------------------------- |
|
57 | #----------------------------------------------------------------------------- | |
78 | # Script main |
|
58 | # Script main | |
79 | #----------------------------------------------------------------------------- |
|
59 | #----------------------------------------------------------------------------- | |
80 |
|
60 | |||
81 | if __name__ == '__main__': |
|
61 | if __name__ == '__main__': | |
82 | parser = argparse.ArgumentParser(description=__doc__, |
|
62 | parser = argparse.ArgumentParser(description=__doc__, | |
83 | formatter_class=argparse.RawTextHelpFormatter) |
|
63 | formatter_class=argparse.RawTextHelpFormatter) | |
84 | # TODO: consider passing file like object around, rather than filenames |
|
64 | # TODO: consider passing file like object around, rather than filenames | |
85 | # would allow us to process stdin, or even http streams |
|
65 | # would allow us to process stdin, or even http streams | |
86 | #parser.add_argument('infile', nargs='?', type=argparse.FileType('r'), default=sys.stdin) |
|
66 | #parser.add_argument('infile', nargs='?', type=argparse.FileType('r'), default=sys.stdin) | |
87 |
|
67 | |||
88 | #Require a filename as a positional argument |
|
68 | #Require a filename as a positional argument | |
89 | parser.add_argument('infile', nargs=1) |
|
69 | parser.add_argument('infile', nargs=1) | |
90 | parser.add_argument('-f', '--format', default='rst', |
|
70 | parser.add_argument('-f', '--format', default='rst', | |
91 | help='Output format. Supported formats: \n' + |
|
71 | help='Output format. Supported formats: \n' + | |
92 | known_formats) |
|
72 | known_formats) | |
93 | args = parser.parse_args() |
|
73 | args = parser.parse_args() | |
94 | main(infile=args.infile[0], format=args.format) |
|
74 | main(infile=args.infile[0], format=args.format) |
General Comments 0
You need to be logged in to leave comments.
Login now