nbconvert.py
94 lines
| 3.2 KiB
| text/x-python
|
PythonLexer
|
r6220 | #!/usr/bin/env python | ||
|
r6280 | """Convert IPython notebooks to other formats, such as ReST, and HTML. | ||
|
r6220 | |||
|
r6280 | Example: | ||
|
r7914 | ./nbconvert.py --format rst file.ipynb | ||
|
r6220 | |||
|
r7914 | Produces 'file.rst', along with auto-generated figure files | ||
called nb_figure_NN.png. | ||||
|
r6220 | """ | ||
|
r6677 | #----------------------------------------------------------------------------- | ||
# Imports | ||||
#----------------------------------------------------------------------------- | ||||
|
r6671 | from __future__ import print_function | ||
|
r6220 | |||
|
r6677 | # Stdlib | ||
|
r6672 | import codecs | ||
|
r7914 | import io | ||
|
r6674 | import logging | ||
|
r6220 | import os | ||
|
r6671 | import pprint | ||
import re | ||||
|
r6220 | import subprocess | ||
import sys | ||||
|
r7366 | import json | ||
import copy | ||||
|
r8610 | from types import FunctionType | ||
|
r7366 | from shutil import rmtree | ||
|
r8163 | from markdown import markdown | ||
|
r6671 | |||
|
r6677 | # From IPython | ||
|
r6262 | from IPython.external import argparse | ||
|
r6220 | from IPython.nbformat import current as nbformat | ||
|
r6257 | from IPython.utils.text import indent | ||
|
r7366 | from IPython.nbformat.v3.nbjson import BytesEncoder | ||
|
r7914 | from IPython.utils import path, py3compat | ||
# local | ||||
from lexers import IPythonLexer | ||||
|
r8618 | from converters.html import ConverterHTML | ||
from converters.markdown import ConverterMarkdown | ||||
from converters.bloggerhtml import ConverterBloggerHTML | ||||
from converters.rst import ConverterRST | ||||
from converters.latex import ConverterLaTeX | ||||
|
r8620 | from converters.notebook import ConverterNotebook | ||
from converters.python import ConverterPy | ||||
|
r7366 | |||
|
r8432 | known_formats = "rst (default), html, blogger-html, latex, markdown, py" | ||
|
r6220 | |||
|
r6261 | def main(infile, format='rst'): | ||
|
r6220 | """Convert a notebook to html in one step""" | ||
|
r6280 | # XXX: this is just quick and dirty for now. When adding a new format, | ||
# make sure to add it to the `known_formats` string above, which gets | ||||
# printed in in the catch-all else, as well as in the help | ||||
|
r6261 | if format == 'rst': | ||
converter = ConverterRST(infile) | ||||
converter.render() | ||||
|
r7812 | elif format == 'markdown': | ||
converter = ConverterMarkdown(infile) | ||||
converter.render() | ||||
|
r6261 | elif format == 'html': | ||
|
r7914 | converter = ConverterHTML(infile) | ||
htmlfname = converter.render() | ||||
|
r8432 | elif format == 'blogger-html': | ||
converter = ConverterBloggerHTML(infile) | ||||
htmlfname = converter.render() | ||||
|
r6671 | elif format == 'latex': | ||
converter = ConverterLaTeX(infile) | ||||
latexfname = converter.render() | ||||
|
r8261 | elif format == 'py': | ||
converter = ConverterPy(infile) | ||||
converter.render() | ||||
|
r6280 | else: | ||
raise SystemExit("Unknown format '%s', " % format + | ||||
"known formats are: " + known_formats) | ||||
|
r6220 | |||
|
r6677 | #----------------------------------------------------------------------------- | ||
# Script main | ||||
#----------------------------------------------------------------------------- | ||||
|
r6261 | |||
|
r6280 | if __name__ == '__main__': | ||
parser = argparse.ArgumentParser(description=__doc__, | ||||
formatter_class=argparse.RawTextHelpFormatter) | ||||
|
r6261 | # TODO: consider passing file like object around, rather than filenames | ||
# would allow us to process stdin, or even http streams | ||||
#parser.add_argument('infile', nargs='?', type=argparse.FileType('r'), default=sys.stdin) | ||||
#Require a filename as a positional argument | ||||
parser.add_argument('infile', nargs=1) | ||||
|
r6265 | parser.add_argument('-f', '--format', default='rst', | ||
|
r6280 | help='Output format. Supported formats: \n' + | ||
known_formats) | ||||
|
r6261 | args = parser.parse_args() | ||
main(infile=args.infile[0], format=args.format) | ||||