##// END OF EJS Templates
nbconvert is now configurable app
Matthias BUSSONNIER -
Show More
@@ -131,7 +131,7 b' class Converter(object):'
131 user_preamble = None
131 user_preamble = None
132 raw_as_verbatim = False
132 raw_as_verbatim = False
133
133
134 def __init__(self, infile, highlight_source=True, exclude=[]):
134 def __init__(self, infile, highlight_source=True, exclude=[], **kw):
135 # N.B. Initialized in the same order as defined above. Please try to
135 # N.B. Initialized in the same order as defined above. Please try to
136 # keep in this way for readability's sake.
136 # keep in this way for readability's sake.
137 self.exclude_cells = exclude
137 self.exclude_cells = exclude
@@ -15,6 +15,13 b' from __future__ import print_function'
15 # From IPython
15 # From IPython
16 from IPython.external import argparse
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 # local
25 # local
19 from converters.html import ConverterHTML
26 from converters.html import ConverterHTML
20 from converters.markdown import ConverterMarkdown
27 from converters.markdown import ConverterMarkdown
@@ -44,44 +51,95 b" default_format = 'rst'"
44 known_formats = ', '.join([key + " (default)" if key == default_format else key
51 known_formats = ', '.join([key + " (default)" if key == default_format else key
45 for key in converters])
52 for key in converters])
46
53
54 class NbconvertApp(Application):
47
55
48 def main(infile, format='rst', preamble=None, exclude=[],
56 name = Unicode('thisIsNbconvertApp',config=True)
49 highlight_source=True):
57
50 """Convert a notebook to html in one step"""
58 fmt = CaselessStrEnum(converters.keys(),
51 try:
59 default_value='rst',
52 ConverterClass = converters[format]
60 config=True,
53 except KeyError:
61 help="Supported conversion format")
54 raise SystemExit("Unknown format '%s', " % format +
62
55 "known formats are: " + known_formats)
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 converter = ConverterClass(infile, highlight_source=highlight_source, exclude=exclude)
77 converter = ConverterClass(infile, highlight_source=highlight_source, exclude=exclude)
58 converter.render()
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 # Script main
125 # Script main
62 #-----------------------------------------------------------------------------
126 #-----------------------------------------------------------------------------
63
127
64 if __name__ == '__main__':
128 if __name__ == '__main__':
65 parser = argparse.ArgumentParser(description=__doc__,
129 #parser = argparse.ArgumentParser(description=__doc__,
66 formatter_class=argparse.RawTextHelpFormatter)
130 # formatter_class=argparse.RawTextHelpFormatter)
67 # TODO: consider passing file like object around, rather than filenames
131 # TODO: consider passing file like object around, rather than filenames
68 # would allow us to process stdin, or even http streams
132 # would allow us to process stdin, or even http streams
69 #parser.add_argument('infile', nargs='?', type=argparse.FileType('r'),
133 #parser.add_argument('infile', nargs='?', type=argparse.FileType('r'),
70 # default=sys.stdin)
134 # default=sys.stdin)
71
135
72 #Require a filename as a positional argument
136 #Require a filename as a positional argument
73 parser.add_argument('infile', nargs=1)
137 #parser.add_argument('infile', nargs=1)
74 parser.add_argument('-f', '--format', default='rst',
138 #parser.add_argument('-e', '--exclude', default='',
75 help='Output format. Supported formats: \n' +
139 # help='Comma-separated list of cells to exclude')
76 known_formats)
140 #parser.add_argument('-H', '--no-highlighting', action='store_false',
77 parser.add_argument('-p', '--preamble',
141 # help='Disable syntax highlighting for code blocks.')
78 help='Path to a user-specified preamble file')
142 #args = parser.parse_args()
79 parser.add_argument('-e', '--exclude', default='',
143 #exclude_cells = [s.strip() for s in args.exclude.split(',')]
80 help='Comma-separated list of cells to exclude')
144
81 parser.add_argument('-H', '--no-highlighting', action='store_false',
145 main()
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)
1 NO CONTENT: file was removed
NO CONTENT: file was removed
This diff has been collapsed as it changes many lines, (520 lines changed) Show them Hide them
1 NO CONTENT: file was removed
NO CONTENT: file was removed
1 NO CONTENT: file was removed
NO CONTENT: file was removed
This diff has been collapsed as it changes many lines, (701 lines changed) Show them Hide them
1 NO CONTENT: file was removed
NO CONTENT: file was removed
This diff has been collapsed as it changes many lines, (850 lines changed) Show them Hide them
1 NO CONTENT: file was removed
NO CONTENT: file was removed
This diff has been collapsed as it changes many lines, (1439 lines changed) Show them Hide them
General Comments 0
You need to be logged in to leave comments. Login now