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