|
@@
-15,6
+15,13
b' from __future__ import print_function'
|
|
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
|
|
|
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
|
|
@@
-44,44
+51,95
b" default_format = 'rst'"
|
|
44
|
51
|
known_formats = ', '.join([key + " (default)" if key == default_format else key
|
|
45
|
52
|
for key in converters])
|
|
46
|
53
|
|
|
|
54
|
class NbconvertApp(Application):
|
|
47
|
55
|
|
|
48
|
|
def main(infile, format='rst', preamble=None, exclude=[],
|
|
49
|
|
highlight_source=True):
|
|
50
|
|
"""Convert a notebook to html in one step"""
|
|
51
|
|
try:
|
|
52
|
|
ConverterClass = converters[format]
|
|
53
|
|
except KeyError:
|
|
54
|
|
raise SystemExit("Unknown format '%s', " % format +
|
|
55
|
|
"known formats are: " + known_formats)
|
|
|
56
|
name = Unicode('thisIsNbconvertApp',config=True)
|
|
|
57
|
|
|
|
58
|
fmt = CaselessStrEnum(converters.keys(),
|
|
|
59
|
default_value='rst',
|
|
|
60
|
config=True,
|
|
|
61
|
help="Supported conversion format")
|
|
|
62
|
|
|
|
63
|
preamble = Unicode("" ,
|
|
|
64
|
config=True,
|
|
|
65
|
help="Path to a user-specified preamble file")
|
|
|
66
|
|
|
|
67
|
highlight = Bool(True,
|
|
|
68
|
config=True,
|
|
|
69
|
help="Enable syntax highlighting for code blocks.")
|
|
|
70
|
|
|
|
71
|
exclude = List( [],
|
|
|
72
|
config=True,
|
|
|
73
|
help = 'list of cells to exclude while converting')
|
|
|
74
|
|
|
|
75
|
infile = Unicode("", config=True)
|
|
56
|
76
|
|
|
57
|
77
|
converter = ConverterClass(infile, highlight_source=highlight_source, exclude=exclude)
|
|
58
|
78
|
converter.render()
|
|
59
|
79
|
|
|
|
80
|
|
|
|
81
|
aliases = {
|
|
|
82
|
'format':'NbconvertApp.fmt',
|
|
|
83
|
'highlight':'NbconvertApp.highlight',
|
|
|
84
|
'preamble':'NbconvertApp.preamble',
|
|
|
85
|
'infile' : 'NbconvertApp.infile'
|
|
|
86
|
}
|
|
|
87
|
|
|
|
88
|
def __init__(self, **kwargs):
|
|
|
89
|
super(NbconvertApp, self).__init__(**kwargs)
|
|
|
90
|
|
|
|
91
|
def initialize(self, argv=None):
|
|
|
92
|
# don't hook up crash handler before parsing command-line
|
|
|
93
|
self.parse_command_line(argv)
|
|
|
94
|
cl_config = self.config
|
|
|
95
|
print(self.config)
|
|
|
96
|
self.update_config(cl_config)
|
|
|
97
|
#self.init_crash_handler()
|
|
|
98
|
#self.foo = Cnf(config=self.config)
|
|
|
99
|
#if self.subapp is not None:
|
|
|
100
|
# stop here if subapp is taking over
|
|
|
101
|
#return
|
|
|
102
|
#cl_config = self.config
|
|
|
103
|
#self.init_profile_dir()
|
|
|
104
|
#self.init_config_files()
|
|
|
105
|
#self.load_config_file()
|
|
|
106
|
# enforce cl-opts override configfile opts:
|
|
|
107
|
#self.update_config(cl_config)
|
|
|
108
|
|
|
|
109
|
|
|
|
110
|
def run(self):
|
|
|
111
|
"""Convert a notebook to html in one step"""
|
|
|
112
|
ConverterClass = converters[self.fmt]
|
|
|
113
|
|
|
|
114
|
converter = ConverterClass(self.infile, highlight_source=self.highlight, preamble=self.preamble, exclude=self.exclude)
|
|
|
115
|
converter.render()
|
|
|
116
|
|
|
|
117
|
def main():
|
|
|
118
|
"""Convert a notebook to html in one step"""
|
|
|
119
|
app = NbconvertApp.instance()
|
|
|
120
|
print(app.classes)
|
|
|
121
|
app.initialize()
|
|
|
122
|
app.start()
|
|
|
123
|
app.run()
|
|
60
|
124
|
#-----------------------------------------------------------------------------
|
|
61
|
125
|
# Script main
|
|
62
|
126
|
#-----------------------------------------------------------------------------
|
|
63
|
127
|
|
|
64
|
128
|
if __name__ == '__main__':
|
|
65
|
|
parser = argparse.ArgumentParser(description=__doc__,
|
|
66
|
|
formatter_class=argparse.RawTextHelpFormatter)
|
|
|
129
|
#parser = argparse.ArgumentParser(description=__doc__,
|
|
|
130
|
# formatter_class=argparse.RawTextHelpFormatter)
|
|
67
|
131
|
# TODO: consider passing file like object around, rather than filenames
|
|
68
|
132
|
# would allow us to process stdin, or even http streams
|
|
69
|
133
|
#parser.add_argument('infile', nargs='?', type=argparse.FileType('r'),
|
|
70
|
134
|
# default=sys.stdin)
|
|
71
|
135
|
|
|
72
|
136
|
#Require a filename as a positional argument
|
|
73
|
|
parser.add_argument('infile', nargs=1)
|
|
74
|
|
parser.add_argument('-f', '--format', default='rst',
|
|
75
|
|
help='Output format. Supported formats: \n' +
|
|
76
|
|
known_formats)
|
|
77
|
|
parser.add_argument('-p', '--preamble',
|
|
78
|
|
help='Path to a user-specified preamble file')
|
|
79
|
|
parser.add_argument('-e', '--exclude', default='',
|
|
80
|
|
help='Comma-separated list of cells to exclude')
|
|
81
|
|
parser.add_argument('-H', '--no-highlighting', action='store_false',
|
|
82
|
|
help='Disable syntax highlighting for code blocks.')
|
|
83
|
|
args = parser.parse_args()
|
|
84
|
|
exclude_cells = [s.strip() for s in args.exclude.split(',')]
|
|
85
|
|
|
|
86
|
|
main(infile=args.infile[0], format=args.format, preamble=args.preamble,
|
|
87
|
|
exclude=exclude_cells, highlight_source=args.no_highlighting)
|
|
|
137
|
#parser.add_argument('infile', nargs=1)
|
|
|
138
|
#parser.add_argument('-e', '--exclude', default='',
|
|
|
139
|
# help='Comma-separated list of cells to exclude')
|
|
|
140
|
#parser.add_argument('-H', '--no-highlighting', action='store_false',
|
|
|
141
|
# help='Disable syntax highlighting for code blocks.')
|
|
|
142
|
#args = parser.parse_args()
|
|
|
143
|
#exclude_cells = [s.strip() for s in args.exclude.split(',')]
|
|
|
144
|
|
|
|
145
|
main()
|