Show More
@@ -1,14 +1,12 b'' | |||||
1 |
""" |
|
1 | """Exporter for the notebook conversion pipeline. | |
2 |
|
2 | |||
3 |
This module defines |
|
3 | This module defines Exporter, a highly configurable converter | |
4 |
that uses Jinja2 to |
|
4 | that uses Jinja2 to export notebook files into different format. | |
5 |
|
5 | |||
6 | You can register both pre-transformers that will act on the notebook format |
|
6 | You can register both pre-transformers that will act on the notebook format | |
7 | befor conversion and jinja filter that would then be availlable in the templates |
|
7 | befor conversion and jinja filter that would then be availlable in the templates | |
8 | """ |
|
8 | """ | |
9 |
|
9 | |||
10 | from __future__ import absolute_import |
|
|||
11 |
|
||||
12 | #----------------------------------------------------------------------------- |
|
10 | #----------------------------------------------------------------------------- | |
13 | # Copyright (c) 2013, the IPython Development Team. |
|
11 | # Copyright (c) 2013, the IPython Development Team. | |
14 | # |
|
12 | # | |
@@ -20,33 +18,29 b' from __future__ import absolute_import' | |||||
20 | #----------------------------------------------------------------------------- |
|
18 | #----------------------------------------------------------------------------- | |
21 | # Imports |
|
19 | # Imports | |
22 | #----------------------------------------------------------------------------- |
|
20 | #----------------------------------------------------------------------------- | |
23 |
|
21 | from __future__ import print_function, absolute_import | ||
24 | from __future__ import print_function |
|
|||
25 | from __future__ import absolute_import |
|
|||
26 |
|
22 | |||
27 | # Stdlib imports |
|
23 | # Stdlib imports | |
28 | import io |
|
24 | import io | |
29 | import os |
|
25 | import os | |
30 |
|
26 | |||
31 | # IPython imports |
|
27 | # IPython imports | |
32 | from IPython.utils.traitlets import MetaHasTraits |
|
|||
33 | from IPython.utils.traitlets import (Unicode, List, Bool) |
|
|||
34 | from IPython.config.configurable import Configurable |
|
28 | from IPython.config.configurable import Configurable | |
35 | from IPython.nbformat import current as nbformat |
|
29 | from IPython.nbformat import current as nbformat | |
36 |
|
30 | from IPython.utils.traitlets import MetaHasTraits, Unicode, List, Bool | ||
37 |
|
31 | |||
38 | # other libs/dependencies |
|
32 | # other libs/dependencies | |
39 | from jinja2 import Environment, FileSystemLoader |
|
33 | from jinja2 import Environment, FileSystemLoader | |
40 |
|
34 | |||
41 |
|
||||
42 | # local import (pre-transformers) |
|
35 | # local import (pre-transformers) | |
43 |
from |
|
36 | from . import transformers as trans #TODO | |
|
37 | ||||
44 | try: |
|
38 | try: | |
45 | from .sphinx_transformer import (SphinxTransformer) |
|
39 | from .sphinx_transformer import (SphinxTransformer) #TODO | |
46 | except ImportError: |
|
40 | except ImportError: | |
47 | SphinxTransformer = None |
|
41 | SphinxTransformer = None | |
48 |
|
42 | |||
49 | from .latex_transformer import (LatexTransformer) |
|
43 | from .latex_transformer import (LatexTransformer) #TODO | |
50 |
|
44 | |||
51 | # some jinja filters |
|
45 | # some jinja filters | |
52 | from .jinja_filters import (python_comment, indent, |
|
46 | from .jinja_filters import (python_comment, indent, | |
@@ -55,9 +49,9 b' from .jinja_filters import (python_comment, indent,' | |||||
55 | rm_dollars, rm_math_space |
|
49 | rm_dollars, rm_math_space | |
56 | ) |
|
50 | ) | |
57 |
|
51 | |||
58 | from .utils import markdown2rst |
|
52 | from .utils import markdown2rst #TODO | |
59 |
|
53 | |||
60 | import textwrap |
|
54 | import textwrap #TODO | |
61 |
|
55 | |||
62 | def wrap(text, width=100): |
|
56 | def wrap(text, width=100): | |
63 | """ try to detect and wrap paragraph""" |
|
57 | """ try to detect and wrap paragraph""" | |
@@ -106,7 +100,7 b" texenv.filters['escape_tex'] = escape_tex" | |||||
106 | class ConversionException(Exception): |
|
100 | class ConversionException(Exception): | |
107 | pass |
|
101 | pass | |
108 |
|
102 | |||
109 |
class |
|
103 | class Exporter(Configurable): | |
110 | """ A Jinja2 base converter templates |
|
104 | """ A Jinja2 base converter templates | |
111 |
|
105 | |||
112 | Preprocess the ipynb files, feed it throug jinja templates, |
|
106 | Preprocess the ipynb files, feed it throug jinja templates, |
@@ -6,41 +6,54 b' Commandline interface for the NBConvert conversion utility. Read the' | |||||
6 | readme.rst for usage information |
|
6 | readme.rst for usage information | |
7 | """ |
|
7 | """ | |
8 | #----------------------------------------------------------------------------- |
|
8 | #----------------------------------------------------------------------------- | |
9 |
# |
|
9 | #Copyright (c) 2013, the IPython Development Team. | |
10 | # |
|
10 | # | |
11 |
# |
|
11 | #Distributed under the terms of the Modified BSD License. | |
12 | # |
|
12 | # | |
13 |
# |
|
13 | #The full license is in the file COPYING.txt, distributed with this software. | |
14 | #----------------------------------------------------------------------------- |
|
14 | #----------------------------------------------------------------------------- | |
15 |
|
15 | |||
16 | #----------------------------------------------------------------------------- |
|
16 | #----------------------------------------------------------------------------- | |
17 |
# |
|
17 | #Imports | |
18 | #----------------------------------------------------------------------------- |
|
18 | #----------------------------------------------------------------------------- | |
19 |
|
19 | |||
20 |
# |
|
20 | #Stdlib imports | |
21 | from __future__ import print_function |
|
21 | from __future__ import print_function | |
22 | import sys |
|
22 | import sys | |
23 | import io |
|
23 | import io | |
24 | import os |
|
24 | import os | |
25 |
|
25 | |||
26 |
# |
|
26 | #From IPython | |
27 |
# |
|
27 | #All the stuff needed for the configurable things | |
28 | from IPython.config.application import Application |
|
28 | from IPython.config.application import Application | |
29 | from IPython.config.loader import ConfigFileNotFound |
|
29 | from IPython.config.loader import ConfigFileNotFound | |
30 | from IPython.utils.traitlets import Unicode, Bool |
|
30 | from IPython.utils.traitlets import Unicode, Bool | |
31 |
|
31 | |||
32 |
# |
|
32 | #Local imports | |
33 | from converters.template import ConverterTemplate #TODO |
|
33 | from api.exporter import Exporter | |
34 | from converters.config import GlobalConfigurable #TODO |
|
34 | from converters.config import GlobalConfigurable #TODO | |
35 | from converters.transformers import (ExtractFigureTransformer) #TODO |
|
35 | from converters.transformers import (ExtractFigureTransformer) #TODO | |
36 |
|
36 | |||
37 | #----------------------------------------------------------------------------- |
|
37 | #----------------------------------------------------------------------------- | |
38 |
# |
|
38 | #Globals and constants | |
39 | #----------------------------------------------------------------------------- |
|
39 | #----------------------------------------------------------------------------- | |
40 | NBCONVERT_DIR = os.path.abspath(os.path.realpath(os.path.dirname(__file__))) |
|
40 | NBCONVERT_DIR = os.path.abspath(os.path.realpath(os.path.dirname(__file__))) | |
|
41 | ||||
|
42 | #'Keys in resources' user prompt. | |||
|
43 | KEYS_PROMPT_HEAD = "====================== Keys in Resources ==================================" | |||
|
44 | KEYS_PROMPT_BODY = """ | |||
|
45 | =========================================================================== | |||
|
46 | You are responsible for writting these files into the appropriate | |||
|
47 | directorie(s) if need be. If you do not want to see this message, enable | |||
|
48 | the 'write' (boolean) flag of the converter. | |||
|
49 | =========================================================================== | |||
|
50 | """ | |||
|
51 | ||||
|
52 | #Error Messages | |||
41 | ERROR_CONFIG_NOT_FOUND = "Config file for profile '%s' not found, giving up." |
|
53 | ERROR_CONFIG_NOT_FOUND = "Config file for profile '%s' not found, giving up." | |
|
54 | ||||
42 | #----------------------------------------------------------------------------- |
|
55 | #----------------------------------------------------------------------------- | |
43 |
# |
|
56 | #Classes and functions | |
44 | #----------------------------------------------------------------------------- |
|
57 | #----------------------------------------------------------------------------- | |
45 | class NbconvertApp(Application): |
|
58 | class NbconvertApp(Application): | |
46 | """A basic application to convert ipynb files""" |
|
59 | """A basic application to convert ipynb files""" | |
@@ -128,23 +141,31 b' class NbconvertApp(Application):' | |||||
128 | #The last arguments in chain of arguments will be used as conversion |
|
141 | #The last arguments in chain of arguments will be used as conversion | |
129 | ipynb_file = (self.extra_args or [None])[2] |
|
142 | ipynb_file = (self.extra_args or [None])[2] | |
130 |
|
143 | |||
131 |
# |
|
144 | #If you are writting a custom transformer, append it to the dictionary | |
132 |
# |
|
145 | #below. | |
133 | userpreprocessors = {} |
|
146 | userpreprocessors = {} | |
134 |
|
147 | |||
135 |
# |
|
148 | #Create the Jinja template exporter. TODO: Add ability to | |
136 | C = ConverterTemplate(config=self.config, preprocessors=userpreprocessors) |
|
149 | #import in IPYNB aswell | |
|
150 | exporter = Exporter(config=self.config, preprocessors=userpreprocessors) | |||
137 |
|
151 | |||
138 | output, resources = C.from_filename(ipynb_file) |
|
152 | #Export | |
|
153 | output, resources = exporter.from_filename(ipynb_file) | |||
139 | if self.stdout : |
|
154 | if self.stdout : | |
140 | print(output.encode('utf-8')) |
|
155 | print(output.encode('utf-8')) | |
141 |
|
156 | |||
|
157 | #Get the file name without the '.ipynb' (6 chars) extension and then | |||
|
158 | #remove any addition periods and spaces. The resulting name will | |||
|
159 | #be used to create the directory that the files will be exported | |||
|
160 | #into. | |||
142 | out_root = ipynb_file[:-6].replace('.', '_').replace(' ', '_') |
|
161 | out_root = ipynb_file[:-6].replace('.', '_').replace(' ', '_') | |
143 |
|
162 | |||
|
163 | #Write file output from conversion. | |||
144 | if self.write : |
|
164 | if self.write : | |
145 | with io.open(os.path.join(out_root+'.'+self.fileext), 'w') as f: |
|
165 | with io.open(os.path.join(out_root+'.'+self.fileext), 'w') as f: | |
146 | f.write(output) |
|
166 | f.write(output) | |
147 |
|
167 | |||
|
168 | #Output any associate figures into the same "root" directory. | |||
148 | binkeys = resources.get('figures', {}).get('binary',{}).keys() |
|
169 | binkeys = resources.get('figures', {}).get('binary',{}).keys() | |
149 | textkeys = resources.get('figures', {}).get('text',{}).keys() |
|
170 | textkeys = resources.get('figures', {}).get('text',{}).keys() | |
150 | if binkeys or textkeys : |
|
171 | if binkeys or textkeys : | |
@@ -159,22 +180,19 b' class NbconvertApp(Application):' | |||||
159 | with io.open(os.path.join(files_dir, key), 'w') as f: |
|
180 | with io.open(os.path.join(files_dir, key), 'w') as f: | |
160 | f.write(resources['figures']['text'][key]) |
|
181 | f.write(resources['figures']['text'][key]) | |
161 |
|
182 | |||
|
183 | #Figures that weren't exported which will need to be created by the | |||
|
184 | #user. Tell the user what figures these are. | |||
162 | elif self.stdout: |
|
185 | elif self.stdout: | |
163 | print('''====================== Keys in Resources ==================================''') |
|
186 | print(KEYS_PROMPT_HEAD) | |
164 | print(resources['figures'].keys()) |
|
187 | print(resources['figures'].keys()) | |
165 |
print( |
|
188 | print(KEYS_PROMPT_BODY) | |
166 | =========================================================================== |
|
|||
167 | you are responsible from writing those data do a file in the right place if |
|
|||
168 | they need to be. |
|
|||
169 | =========================================================================== |
|
|||
170 | """) |
|
|||
171 |
|
189 | |||
172 | #----------------------------------------------------------------------------- |
|
190 | #----------------------------------------------------------------------------- | |
173 |
# |
|
191 | #Script main | |
174 | #----------------------------------------------------------------------------- |
|
192 | #----------------------------------------------------------------------------- | |
175 | def main(): |
|
193 | def main(): | |
176 | """Convert a notebook in one step""" |
|
194 | """Convert a notebook in one step""" | |
177 |
|
195 | |||
178 | app = NbconvertApp.instance() |
|
196 | app = NbconvertApp.instance() | |
179 | app.description = __doc__ |
|
197 | app.description = __doc__ | |
180 | app.run(argv=sys.argv) |
|
198 | app.run(argv=sys.argv) |
General Comments 0
You need to be logged in to leave comments.
Login now