Show More
@@ -28,6 +28,8 b' from types import FunctionType' | |||||
28 |
|
28 | |||
29 | # IPython imports |
|
29 | # IPython imports | |
30 | from IPython.nbformat import current as nbformat |
|
30 | from IPython.nbformat import current as nbformat | |
|
31 | from IPython.config.configurable import Configurable, SingletonConfigurable | |||
|
32 | from IPython.utils.traitlets import List, Unicode, Type, Bool, Dict, CaselessStrEnum | |||
31 |
|
33 | |||
32 | # Our own imports |
|
34 | # Our own imports | |
33 | from .utils import remove_fake_files_url |
|
35 | from .utils import remove_fake_files_url | |
@@ -94,8 +96,9 b' class DocStringInheritor(type):' | |||||
94 | return type.__new__(meta, classname, bases, newClassDict) |
|
96 | return type.__new__(meta, classname, bases, newClassDict) | |
95 |
|
97 | |||
96 |
|
98 | |||
97 | class Converter(object): |
|
99 | ||
98 | __metaclass__ = DocStringInheritor |
|
100 | class Converter(Configurable): | |
|
101 | #__metaclass__ = DocStringInheritor | |||
99 | #------------------------------------------------------------------------- |
|
102 | #------------------------------------------------------------------------- | |
100 | # Class-level attributes determining the behaviour of the class but |
|
103 | # Class-level attributes determining the behaviour of the class but | |
101 | # probably not varying from instance to instance. |
|
104 | # probably not varying from instance to instance. | |
@@ -110,33 +113,44 b' class Converter(object):' | |||||
110 | # Instance-level attributes that are set in the constructor for this |
|
113 | # Instance-level attributes that are set in the constructor for this | |
111 | # class. |
|
114 | # class. | |
112 | #------------------------------------------------------------------------- |
|
115 | #------------------------------------------------------------------------- | |
113 |
infile = |
|
116 | infile = Unicode() | |
114 | highlight_source = True |
|
117 | ||
115 | infile_dir = str() |
|
118 | highlight_source = Bool(True, | |
116 | infile_root = str() |
|
119 | config=True, | |
117 | clean_name = str() |
|
120 | help="Enable syntax highlighting for code blocks.") | |
118 | files_dir = str() |
|
121 | ||
119 | outbase = str() |
|
122 | preamble = Unicode("" , | |
|
123 | config=True, | |||
|
124 | help="Path to a user-specified preamble file") | |||
|
125 | ||||
|
126 | infile_dir = Unicode() | |||
|
127 | infile_root = Unicode() | |||
|
128 | clean_name = Unicode() | |||
|
129 | files_dir = Unicode() | |||
|
130 | outbase = Unicode() | |||
120 | #------------------------------------------------------------------------- |
|
131 | #------------------------------------------------------------------------- | |
121 | # Instance-level attributes that are set by other methods in the base |
|
132 | # Instance-level attributes that are set by other methods in the base | |
122 | # class. |
|
133 | # class. | |
123 | #------------------------------------------------------------------------- |
|
134 | #------------------------------------------------------------------------- | |
124 | figures_counter = 0 |
|
135 | figures_counter = 0 | |
125 |
output = |
|
136 | output = Unicode() | |
126 | #------------------------------------------------------------------------- |
|
137 | #------------------------------------------------------------------------- | |
127 | # Instance-level attributes that are not actually mentioned further |
|
138 | # Instance-level attributes that are not actually mentioned further | |
128 | # in this class. TODO: Could they be usefully moved to a subclass? |
|
139 | # in this class. TODO: Could they be usefully moved to a subclass? | |
129 | #------------------------------------------------------------------------- |
|
140 | #------------------------------------------------------------------------- | |
130 | with_preamble = True |
|
141 | with_preamble = Bool(True,config=True) | |
131 | user_preamble = None |
|
142 | user_preamble = None | |
132 | raw_as_verbatim = False |
|
143 | raw_as_verbatim = False | |
133 |
|
144 | |||
134 | def __init__(self, infile, highlight_source=True, exclude=[], **kw): |
|
145 | ||
|
146 | def __init__(self, infile=None, config=None, exclude=[] **kw): | |||
|
147 | super(Converter,self).__init__(config=config) | |||
|
148 | ||||
|
149 | #DocStringInheritor.__init__(self=config) | |||
135 | # N.B. Initialized in the same order as defined above. Please try to |
|
150 | # N.B. Initialized in the same order as defined above. Please try to | |
136 | # keep in this way for readability's sake. |
|
151 | # keep in this way for readability's sake. | |
137 | self.exclude_cells = exclude |
|
152 | self.exclude_cells = exclude | |
138 | self.infile = infile |
|
153 | self.infile = infile | |
139 | self.highlight_source = highlight_source |
|
|||
140 | self.infile_dir, infile_root = os.path.split(infile) |
|
154 | self.infile_dir, infile_root = os.path.split(infile) | |
141 | self.infile_root = os.path.splitext(infile_root)[0] |
|
155 | self.infile_root = os.path.splitext(infile_root)[0] | |
142 | self.clean_name = clean_filename(self.infile_root) |
|
156 | self.clean_name = clean_filename(self.infile_root) |
@@ -17,7 +17,7 b' from IPython.external import argparse' | |||||
17 |
|
17 | |||
18 | # All the stuff needed for the configurable things |
|
18 | # All the stuff needed for the configurable things | |
19 | from IPython.config.application import Application, catch_config_error |
|
19 | from IPython.config.application import Application, catch_config_error | |
20 | from IPython.config.configurable import Configurable |
|
20 | from IPython.config.configurable import Configurable, SingletonConfigurable | |
21 | from IPython.config.loader import Config, ConfigFileNotFound |
|
21 | from IPython.config.loader import Config, ConfigFileNotFound | |
22 | from IPython.utils.traitlets import List, Unicode, Type, Bool, Dict, CaselessStrEnum |
|
22 | from IPython.utils.traitlets import List, Unicode, Type, Bool, Dict, CaselessStrEnum | |
23 |
|
23 | |||
@@ -29,6 +29,7 b' from converters.bloggerhtml import ConverterBloggerHTML' | |||||
29 | from converters.rst import ConverterRST |
|
29 | from converters.rst import ConverterRST | |
30 | from converters.latex import ConverterLaTeX |
|
30 | from converters.latex import ConverterLaTeX | |
31 | from converters.python import ConverterPy |
|
31 | from converters.python import ConverterPy | |
|
32 | from converters.base import Converter | |||
32 |
|
33 | |||
33 |
|
34 | |||
34 | # When adding a new format, make sure to add it to the `converters` |
|
35 | # When adding a new format, make sure to add it to the `converters` | |
@@ -53,21 +54,12 b' known_formats = \', \'.join([key + " (default)" if key == default_format else key' | |||||
53 |
|
54 | |||
54 | class NbconvertApp(Application): |
|
55 | class NbconvertApp(Application): | |
55 |
|
56 | |||
56 | name = Unicode('thisIsNbconvertApp',config=True) |
|
|||
57 |
|
57 | |||
58 | fmt = CaselessStrEnum(converters.keys(), |
|
58 | fmt = CaselessStrEnum(converters.keys(), | |
59 |
default_value='rst', |
|
59 | default_value='rst', | |
60 | config=True, |
|
60 | config=True, | |
61 | help="Supported conversion format") |
|
61 | help="Supported conversion format") | |
62 |
|
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( [], |
|
63 | exclude = List( [], | |
72 | config=True, |
|
64 | config=True, | |
73 | help = 'list of cells to exclude while converting') |
|
65 | help = 'list of cells to exclude while converting') | |
@@ -80,19 +72,25 b' class NbconvertApp(Application):' | |||||
80 |
|
72 | |||
81 | aliases = { |
|
73 | aliases = { | |
82 | 'format':'NbconvertApp.fmt', |
|
74 | 'format':'NbconvertApp.fmt', | |
83 |
'highlight':' |
|
75 | 'highlight':'Converter.highlight_source', | |
84 |
'preamble':' |
|
76 | 'preamble':'Converter.preamble', | |
85 | 'infile' : 'NbconvertApp.infile' |
|
77 | 'infile' : 'NbconvertApp.infile' | |
86 | } |
|
78 | } | |
87 |
|
79 | |||
88 | def __init__(self, **kwargs): |
|
80 | def __init__(self, **kwargs): | |
89 | super(NbconvertApp, self).__init__(**kwargs) |
|
81 | super(NbconvertApp, self).__init__(**kwargs) | |
|
82 | # ensure those are registerd | |||
|
83 | self.classes.insert(0,Converter) | |||
|
84 | self.classes.insert(0,ConverterRST) | |||
|
85 | self.classes.insert(0,ConverterMarkdown) | |||
|
86 | self.classes.insert(0,ConverterBloggerHTML) | |||
|
87 | self.classes.insert(0,ConverterLaTeX) | |||
|
88 | self.classes.insert(0,ConverterPy) | |||
90 |
|
89 | |||
91 | def initialize(self, argv=None): |
|
90 | def initialize(self, argv=None): | |
92 | # don't hook up crash handler before parsing command-line |
|
91 | # don't hook up crash handler before parsing command-line | |
93 | self.parse_command_line(argv) |
|
92 | self.parse_command_line(argv) | |
94 | cl_config = self.config |
|
93 | cl_config = self.config | |
95 | print(self.config) |
|
|||
96 | self.update_config(cl_config) |
|
94 | self.update_config(cl_config) | |
97 | #self.init_crash_handler() |
|
95 | #self.init_crash_handler() | |
98 | #self.foo = Cnf(config=self.config) |
|
96 | #self.foo = Cnf(config=self.config) | |
@@ -110,14 +108,13 b' class NbconvertApp(Application):' | |||||
110 | def run(self): |
|
108 | def run(self): | |
111 | """Convert a notebook to html in one step""" |
|
109 | """Convert a notebook to html in one step""" | |
112 | ConverterClass = converters[self.fmt] |
|
110 | ConverterClass = converters[self.fmt] | |
113 |
|
111 | converter = ConverterClass(infile=self.extra_args[0], config=self.config) | ||
114 | converter = ConverterClass(self.infile, highlight_source=self.highlight, preamble=self.preamble, exclude=self.exclude) |
|
|||
115 | converter.render() |
|
112 | converter.render() | |
116 |
|
113 | |||
117 | def main(): |
|
114 | def main(): | |
118 | """Convert a notebook to html in one step""" |
|
115 | """Convert a notebook to html in one step""" | |
119 | app = NbconvertApp.instance() |
|
116 | app = NbconvertApp.instance() | |
120 | print(app.classes) |
|
117 | app.description = __doc__ | |
121 | app.initialize() |
|
118 | app.initialize() | |
122 | app.start() |
|
119 | app.start() | |
123 | app.run() |
|
120 | app.run() | |
@@ -126,20 +123,13 b' def main():' | |||||
126 | #----------------------------------------------------------------------------- |
|
123 | #----------------------------------------------------------------------------- | |
127 |
|
124 | |||
128 | if __name__ == '__main__': |
|
125 | if __name__ == '__main__': | |
129 | #parser = argparse.ArgumentParser(description=__doc__, |
|
|||
130 | # formatter_class=argparse.RawTextHelpFormatter) |
|
|||
131 | # TODO: consider passing file like object around, rather than filenames |
|
126 | # TODO: consider passing file like object around, rather than filenames | |
132 | # would allow us to process stdin, or even http streams |
|
127 | # would allow us to process stdin, or even http streams | |
133 | #parser.add_argument('infile', nargs='?', type=argparse.FileType('r'), |
|
128 | #parser.add_argument('infile', nargs='?', type=argparse.FileType('r'), | |
134 | # default=sys.stdin) |
|
129 | # default=sys.stdin) | |
135 |
|
130 | |||
136 | #Require a filename as a positional argument |
|
|||
137 | #parser.add_argument('infile', nargs=1) |
|
|||
138 | #parser.add_argument('-e', '--exclude', default='', |
|
131 | #parser.add_argument('-e', '--exclude', default='', | |
139 | # help='Comma-separated list of cells to exclude') |
|
132 | # 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(',')] |
|
133 | #exclude_cells = [s.strip() for s in args.exclude.split(',')] | |
144 |
|
134 | |||
145 | main() |
|
135 | main() |
General Comments 0
You need to be logged in to leave comments.
Login now