##// END OF EJS Templates
Rename GlobalConfigurable to NbConvertBase
Jonathan Frederic -
Show More
@@ -1,110 +1,110 b''
1 """
1 """
2 Exporter that allows Latex Jinja templates to work. Contains logic to
2 Exporter that allows Latex Jinja templates to work. Contains logic to
3 appropriately prepare IPYNB files for export to LaTeX. Including but
3 appropriately prepare IPYNB files for export to LaTeX. Including but
4 not limited to escaping LaTeX, fixing math region tags, using special
4 not limited to escaping LaTeX, fixing math region tags, using special
5 tags to circumvent Jinja/Latex syntax conflicts.
5 tags to circumvent Jinja/Latex syntax conflicts.
6 """
6 """
7 #-----------------------------------------------------------------------------
7 #-----------------------------------------------------------------------------
8 # Copyright (c) 2013, the IPython Development Team.
8 # Copyright (c) 2013, the IPython Development Team.
9 #
9 #
10 # Distributed under the terms of the Modified BSD License.
10 # Distributed under the terms of the Modified BSD License.
11 #
11 #
12 # The full license is in the file COPYING.txt, distributed with this software.
12 # The full license is in the file COPYING.txt, distributed with this software.
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14
14
15 #-----------------------------------------------------------------------------
15 #-----------------------------------------------------------------------------
16 # Imports
16 # Imports
17 #-----------------------------------------------------------------------------
17 #-----------------------------------------------------------------------------
18
18
19 # Stdlib imports
19 # Stdlib imports
20 import os
20 import os
21
21
22 # IPython imports
22 # IPython imports
23 from IPython.utils.traitlets import Unicode, List
23 from IPython.utils.traitlets import Unicode, List
24 from IPython.config import Config
24 from IPython.config import Config
25
25
26 from IPython.nbconvert import filters, transformers
26 from IPython.nbconvert import filters, transformers
27 from .exporter import Exporter
27 from .exporter import Exporter
28
28
29 #-----------------------------------------------------------------------------
29 #-----------------------------------------------------------------------------
30 # Classes and functions
30 # Classes and functions
31 #-----------------------------------------------------------------------------
31 #-----------------------------------------------------------------------------
32
32
33 class LatexExporter(Exporter):
33 class LatexExporter(Exporter):
34 """
34 """
35 Exports to a Latex template. Inherit from this class if your template is
35 Exports to a Latex template. Inherit from this class if your template is
36 LaTeX based and you need custom tranformers/filters. Inherit from it if
36 LaTeX based and you need custom tranformers/filters. Inherit from it if
37 you are writing your own HTML template and need custom tranformers/filters.
37 you are writing your own HTML template and need custom tranformers/filters.
38 If you don't need custom tranformers/filters, just change the
38 If you don't need custom tranformers/filters, just change the
39 'template_file' config option. Place your template in the special "/latex"
39 'template_file' config option. Place your template in the special "/latex"
40 subfolder of the "../templates" folder.
40 subfolder of the "../templates" folder.
41 """
41 """
42
42
43 file_extension = Unicode(
43 file_extension = Unicode(
44 'tex', config=True,
44 'tex', config=True,
45 help="Extension of the file that should be written to disk")
45 help="Extension of the file that should be written to disk")
46
46
47 template_file = Unicode(
47 template_file = Unicode(
48 'base', config=True,
48 'base', config=True,
49 help="Name of the template file to use")
49 help="Name of the template file to use")
50
50
51 #Latex constants
51 #Latex constants
52 template_path = Unicode(
52 template_path = Unicode(
53 os.path.join("..", "templates", "latex"), config=True,
53 os.path.join("..", "templates", "latex"), config=True,
54 help="Path where the template files are located.")
54 help="Path where the template files are located.")
55
55
56 template_skeleton_path = Unicode(
56 template_skeleton_path = Unicode(
57 os.path.join("..", "templates", "latex", "skeleton"), config=True,
57 os.path.join("..", "templates", "latex", "skeleton"), config=True,
58 help="Path where the template skeleton files are located.")
58 help="Path where the template skeleton files are located.")
59
59
60 #Special Jinja2 syntax that will not conflict when exporting latex.
60 #Special Jinja2 syntax that will not conflict when exporting latex.
61 jinja_comment_block_start = Unicode("((=", config=True)
61 jinja_comment_block_start = Unicode("((=", config=True)
62 jinja_comment_block_end = Unicode("=))", config=True)
62 jinja_comment_block_end = Unicode("=))", config=True)
63 jinja_variable_block_start = Unicode("(((", config=True)
63 jinja_variable_block_start = Unicode("(((", config=True)
64 jinja_variable_block_end = Unicode(")))", config=True)
64 jinja_variable_block_end = Unicode(")))", config=True)
65 jinja_logic_block_start = Unicode("((*", config=True)
65 jinja_logic_block_start = Unicode("((*", config=True)
66 jinja_logic_block_end = Unicode("*))", config=True)
66 jinja_logic_block_end = Unicode("*))", config=True)
67
67
68 #Extension that the template files use.
68 #Extension that the template files use.
69 template_extension = Unicode(".tplx", config=True)
69 template_extension = Unicode(".tplx", config=True)
70
70
71 default_transformers = List([transformers.ExtractFigureTransformer,
71 default_transformers = List([transformers.ExtractFigureTransformer,
72 transformers.CSSHTMLHeaderTransformer,
72 transformers.CSSHTMLHeaderTransformer,
73 transformers.LatexTransformer,
73 transformers.LatexTransformer,
74 transformers.Svg2PdfTransformer],
74 transformers.Svg2PdfTransformer],
75 config=True,
75 config=True,
76 help="""List of transformers available by default, by name, namespace,
76 help="""List of transformers available by default, by name, namespace,
77 instance, or type.""")
77 instance, or type.""")
78
78
79
79
80 def _init_filters(self):
80 def _init_filters(self):
81 """
81 """
82 Register all of the filters required for the exporter.
82 Register all of the filters required for the exporter.
83 """
83 """
84
84
85 #Register the filters of the base class.
85 #Register the filters of the base class.
86 super(LatexExporter, self)._init_filters()
86 super(LatexExporter, self)._init_filters()
87
87
88 #Add latex filters to the Jinja2 environment
88 #Add latex filters to the Jinja2 environment
89 self.register_filter('escape_tex', filters.escape_latex)
89 self.register_filter('escape_tex', filters.escape_latex)
90 self.register_filter('highlight', filters.highlight2latex)
90 self.register_filter('highlight', filters.highlight2latex)
91
91
92
92
93 @property
93 @property
94 def default_config(self):
94 def default_config(self):
95 c = Config({
95 c = Config({
96 'GlobalConfigurable': {
96 'NbConvertBase': {
97 'display_data_priority' : ['latex', 'png', 'jpg', 'svg', 'jpeg', 'text']
97 'display_data_priority' : ['latex', 'png', 'jpg', 'svg', 'jpeg', 'text']
98 },
98 },
99 'ExtractFigureTransformer': {
99 'ExtractFigureTransformer': {
100 'enabled':True
100 'enabled':True
101 },
101 },
102 'Svg2PdfTransformer': {
102 'Svg2PdfTransformer': {
103 'enabled':True
103 'enabled':True
104 },
104 },
105 'LatexTransformer': {
105 'LatexTransformer': {
106 'enabled':True
106 'enabled':True
107 }
107 }
108 })
108 })
109 c.merge(super(LatexExporter,self).default_config)
109 c.merge(super(LatexExporter,self).default_config)
110 return c
110 return c
@@ -1,33 +1,33 b''
1 """Filter used to select the first preferred output format available.
1 """Filter used to select the first preferred output format available.
2
2
3 The filter contained in the file allows the converter templates to select
3 The filter contained in the file allows the converter templates to select
4 the output format that is most valuable to the active export format. The
4 the output format that is most valuable to the active export format. The
5 value of the different formats is set via
5 value of the different formats is set via
6 GlobalConfigurable.display_data_priority
6 NbConvertBase.display_data_priority
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 # Classes and functions
17 # Classes and functions
18 #-----------------------------------------------------------------------------
18 #-----------------------------------------------------------------------------
19
19
20 from ..utils.config import GlobalConfigurable
20 from ..utils.config import NbConvertBase
21
21
22 __all__ = ['DataTypeFilter']
22 __all__ = ['DataTypeFilter']
23
23
24 class DataTypeFilter(GlobalConfigurable):
24 class DataTypeFilter(NbConvertBase):
25 """ Returns the preferred display format """
25 """ Returns the preferred display format """
26
26
27 def __call__(self, output):
27 def __call__(self, output):
28 """ Return the first available format in the priority """
28 """ Return the first available format in the priority """
29
29
30 for fmt in self.display_data_priority:
30 for fmt in self.display_data_priority:
31 if fmt in output:
31 if fmt in output:
32 return [fmt]
32 return [fmt]
33 return []
33 return []
@@ -1,197 +1,197 b''
1 #!/usr/bin/env python
1 #!/usr/bin/env python
2 """NBConvert is a utility for conversion of IPYNB files.
2 """NBConvert is a utility for conversion of IPYNB files.
3
3
4 Commandline interface for the NBConvert conversion utility. Read the
4 Commandline interface for the NBConvert conversion utility. Read the
5 readme.rst for usage information
5 readme.rst for usage information
6 """
6 """
7 #-----------------------------------------------------------------------------
7 #-----------------------------------------------------------------------------
8 #Copyright (c) 2013, the IPython Development Team.
8 #Copyright (c) 2013, the IPython Development Team.
9 #
9 #
10 #Distributed under the terms of the Modified BSD License.
10 #Distributed under the terms of the Modified BSD License.
11 #
11 #
12 #The full license is in the file COPYING.txt, distributed with this software.
12 #The full license is in the file COPYING.txt, distributed with this software.
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14
14
15 #-----------------------------------------------------------------------------
15 #-----------------------------------------------------------------------------
16 #Imports
16 #Imports
17 #-----------------------------------------------------------------------------
17 #-----------------------------------------------------------------------------
18
18
19 #Stdlib imports
19 #Stdlib imports
20 from __future__ import print_function
20 from __future__ import print_function
21 import sys
21 import sys
22 import os
22 import os
23 import glob
23 import glob
24
24
25 #From IPython
25 #From IPython
26 from IPython.core.application import BaseIPythonApplication
26 from IPython.core.application import BaseIPythonApplication
27 from IPython.config.application import catch_config_error
27 from IPython.config.application import catch_config_error
28 from IPython.utils.traitlets import Unicode, List, Instance, DottedObjectName, Type
28 from IPython.utils.traitlets import Unicode, List, Instance, DottedObjectName, Type
29 from IPython.utils.importstring import import_item
29 from IPython.utils.importstring import import_item
30
30
31 from .exporters.export import export_by_name, get_export_names, ExporterNameError
31 from .exporters.export import export_by_name, get_export_names, ExporterNameError
32 from .exporters.exporter import Exporter
32 from .exporters.exporter import Exporter
33 from .writers.base import WriterBase
33 from .writers.base import WriterBase
34 from .utils.config import GlobalConfigurable
34 from .utils.config import NbConvertBase
35
35
36 #-----------------------------------------------------------------------------
36 #-----------------------------------------------------------------------------
37 #Classes and functions
37 #Classes and functions
38 #-----------------------------------------------------------------------------
38 #-----------------------------------------------------------------------------
39
39
40 class NbConvertApp(BaseIPythonApplication):
40 class NbConvertApp(BaseIPythonApplication):
41 """Application used to convert to and from notebook file type (*.ipynb)"""
41 """Application used to convert to and from notebook file type (*.ipynb)"""
42
42
43 name = 'ipython-nbconvert'
43 name = 'ipython-nbconvert'
44
44
45 description = Unicode(
45 description = Unicode(
46 u"""This application is used to convert notebook files (*.ipynb).
46 u"""This application is used to convert notebook files (*.ipynb).
47 An ipython config file can be used to batch convert notebooks in the
47 An ipython config file can be used to batch convert notebooks in the
48 current directory.""")
48 current directory.""")
49
49
50 examples = Unicode(u"""
50 examples = Unicode(u"""
51 Running `ipython nbconvert` will read the directory config file and then
51 Running `ipython nbconvert` will read the directory config file and then
52 apply it to one or more notebooks.
52 apply it to one or more notebooks.
53
53
54 Multiple notebooks can be given at the command line in a couple of
54 Multiple notebooks can be given at the command line in a couple of
55 different ways:
55 different ways:
56
56
57 > ipython nbconvert notebook*.ipynb
57 > ipython nbconvert notebook*.ipynb
58 > ipython nbconvert notebook1.ipynb notebook2.ipynb
58 > ipython nbconvert notebook1.ipynb notebook2.ipynb
59 > ipython nbconvert # this will use the config file to fill in the notebooks
59 > ipython nbconvert # this will use the config file to fill in the notebooks
60 """)
60 """)
61
61
62 #Writer specific variables
62 #Writer specific variables
63 writer = Instance('IPython.nbconvert.writers.base.WriterBase',
63 writer = Instance('IPython.nbconvert.writers.base.WriterBase',
64 help="""Instance of the writer class used to write the
64 help="""Instance of the writer class used to write the
65 results of the conversion.""")
65 results of the conversion.""")
66 writer_class = DottedObjectName('FilesWriter', config=True,
66 writer_class = DottedObjectName('FilesWriter', config=True,
67 help="""Writer class used to write the
67 help="""Writer class used to write the
68 results of the conversion""")
68 results of the conversion""")
69 writer_aliases = {'FilesWriter': 'IPython.nbconvert.writers.files.FilesWriter',
69 writer_aliases = {'FilesWriter': 'IPython.nbconvert.writers.files.FilesWriter',
70 'DebugWriter': 'IPython.nbconvert.writers.debug.DebugWriter',
70 'DebugWriter': 'IPython.nbconvert.writers.debug.DebugWriter',
71 'StdoutWriter': 'IPython.nbconvert.writers.stdout.StdoutWriter'}
71 'StdoutWriter': 'IPython.nbconvert.writers.stdout.StdoutWriter'}
72 writer_factory = Type()
72 writer_factory = Type()
73
73
74 def _writer_class_changed(self, name, old, new):
74 def _writer_class_changed(self, name, old, new):
75 if new in self.writer_aliases:
75 if new in self.writer_aliases:
76 new = self.writer_aliases[new]
76 new = self.writer_aliases[new]
77 self.writer_factory = import_item(new)
77 self.writer_factory = import_item(new)
78
78
79
79
80 #Other configurable variables
80 #Other configurable variables
81 export_format = Unicode(
81 export_format = Unicode(
82 "", config=True,
82 "", config=True,
83 help="""If specified, nbconvert will convert the document(s) specified
83 help="""If specified, nbconvert will convert the document(s) specified
84 using this format.""")
84 using this format.""")
85
85
86 notebooks = List([], config=True, help="""List of notebooks to convert.
86 notebooks = List([], config=True, help="""List of notebooks to convert.
87 Search patterns are supported.""")
87 Search patterns are supported.""")
88
88
89 nbconvert_aliases = {'format':'NbConvertApp.export_format',
89 nbconvert_aliases = {'format':'NbConvertApp.export_format',
90 'notebooks':'NbConvertApp.notebooks',
90 'notebooks':'NbConvertApp.notebooks',
91 'writer':'NbConvertApp.writer_class'}
91 'writer':'NbConvertApp.writer_class'}
92
92
93
93
94 @catch_config_error
94 @catch_config_error
95 def initialize(self, argv=None):
95 def initialize(self, argv=None):
96 self.aliases.update(self.nbconvert_aliases)
96 self.aliases.update(self.nbconvert_aliases)
97
97
98 super(NbConvertApp, self).initialize(argv)
98 super(NbConvertApp, self).initialize(argv)
99
99
100 #Register class here to have help with help all
100 #Register class here to have help with help all
101 self.classes.insert(0, Exporter)
101 self.classes.insert(0, Exporter)
102 self.classes.insert(0, WriterBase)
102 self.classes.insert(0, WriterBase)
103 self.classes.insert(0, GlobalConfigurable)
103 self.classes.insert(0, NbConvertBase)
104
104
105 #Init
105 #Init
106 self.init_config(self.extra_args)
106 self.init_config(self.extra_args)
107 self.init_writer()
107 self.init_writer()
108
108
109
109
110 def init_config(self, extra_args):
110 def init_config(self, extra_args):
111 """
111 """
112 Add notebooks to the config if needed. Glob each notebook to replace
112 Add notebooks to the config if needed. Glob each notebook to replace
113 notebook patterns with filenames.
113 notebook patterns with filenames.
114 """
114 """
115
115
116 #Get any additional notebook patterns from the commandline
116 #Get any additional notebook patterns from the commandline
117 if len(extra_args) > 0:
117 if len(extra_args) > 0:
118 for pattern in extra_args:
118 for pattern in extra_args:
119 self.notebooks.append(pattern)
119 self.notebooks.append(pattern)
120
120
121 #Use glob to replace all the notebook patterns with filenames.
121 #Use glob to replace all the notebook patterns with filenames.
122 filenames = []
122 filenames = []
123 for pattern in self.notebooks:
123 for pattern in self.notebooks:
124 for filename in glob.glob(pattern):
124 for filename in glob.glob(pattern):
125 if not filename in filenames:
125 if not filename in filenames:
126 filenames.append(filename)
126 filenames.append(filename)
127 self.notebooks = filenames
127 self.notebooks = filenames
128
128
129
129
130 def init_writer(self):
130 def init_writer(self):
131 """
131 """
132 Initialize the writer (which is stateless)
132 Initialize the writer (which is stateless)
133 """
133 """
134 self._writer_class_changed(None, self.writer_class, self.writer_class)
134 self._writer_class_changed(None, self.writer_class, self.writer_class)
135 self.writer = self.writer_factory(parent=self)
135 self.writer = self.writer_factory(parent=self)
136
136
137
137
138 def start(self, argv=None):
138 def start(self, argv=None):
139 """
139 """
140 Ran after initiialization completed
140 Ran after initiialization completed
141 """
141 """
142 super(NbConvertApp, self).start()
142 super(NbConvertApp, self).start()
143 self.convert_notebooks()
143 self.convert_notebooks()
144
144
145
145
146 def convert_notebooks(self):
146 def convert_notebooks(self):
147 """
147 """
148 Convert the notebooks in the self.notebook traitlet
148 Convert the notebooks in the self.notebook traitlet
149 """
149 """
150 #Export each notebook
150 #Export each notebook
151 conversion_success = 0
151 conversion_success = 0
152 for notebook_filename in self.notebooks:
152 for notebook_filename in self.notebooks:
153
153
154 #Get a unique key for the notebook and set it in the resources object.
154 #Get a unique key for the notebook and set it in the resources object.
155 basename = os.path.basename(notebook_filename)
155 basename = os.path.basename(notebook_filename)
156 notebook_name = basename[:basename.rfind('.')]
156 notebook_name = basename[:basename.rfind('.')]
157 resources = {}
157 resources = {}
158 resources['unique_key'] = notebook_name
158 resources['unique_key'] = notebook_name
159
159
160 #Try to export
160 #Try to export
161 try:
161 try:
162 output, resources = export_by_name(self.export_format,
162 output, resources = export_by_name(self.export_format,
163 notebook_filename,
163 notebook_filename,
164 resources=resources,
164 resources=resources,
165 config=self.config)
165 config=self.config)
166 except ExporterNameError as e:
166 except ExporterNameError as e:
167 print("Error: '%s' exporter not found." % self.export_format,
167 print("Error: '%s' exporter not found." % self.export_format,
168 file=sys.stderr)
168 file=sys.stderr)
169 print("Known exporters are:",
169 print("Known exporters are:",
170 "\n\t" + "\n\t".join(get_export_names()),
170 "\n\t" + "\n\t".join(get_export_names()),
171 file=sys.stderr)
171 file=sys.stderr)
172 sys.exit(-1)
172 sys.exit(-1)
173 #except Exception as e:
173 #except Exception as e:
174 #print("Error: could not export '%s'" % notebook_filename, file=sys.stderr)
174 #print("Error: could not export '%s'" % notebook_filename, file=sys.stderr)
175 #print(e, file=sys.stderr)
175 #print(e, file=sys.stderr)
176 else:
176 else:
177 self.writer.write(output, resources, notebook_name=notebook_name)
177 self.writer.write(output, resources, notebook_name=notebook_name)
178 conversion_success += 1
178 conversion_success += 1
179
179
180 #If nothing was converted successfully, help the user.
180 #If nothing was converted successfully, help the user.
181 if conversion_success == 0:
181 if conversion_success == 0:
182
182
183 #No notebooks were specified, show help.
183 #No notebooks were specified, show help.
184 if len(self.notebooks) == 0:
184 if len(self.notebooks) == 0:
185 self.print_help()
185 self.print_help()
186
186
187 #Notebooks were specified, but not converted successfully. Show how
187 #Notebooks were specified, but not converted successfully. Show how
188 #to access help.
188 #to access help.
189 else:
189 else:
190 print('For help, use "ipython nbconvert --help"')
190 print('For help, use "ipython nbconvert --help"')
191
191
192
192
193 #-----------------------------------------------------------------------------
193 #-----------------------------------------------------------------------------
194 # Main entry point
194 # Main entry point
195 #-----------------------------------------------------------------------------
195 #-----------------------------------------------------------------------------
196
196
197 launch_new_instance = NbConvertApp.launch_instance
197 launch_new_instance = NbConvertApp.launch_instance
@@ -1,109 +1,109 b''
1 """
1 """
2 Module that re-groups transformer that would be applied to ipynb files
2 Module that re-groups transformer that would be applied to ipynb files
3 before going through the templating machinery.
3 before going through the templating machinery.
4
4
5 It exposes a convenient class to inherit from to access configurability.
5 It exposes a convenient class to inherit from to access configurability.
6 """
6 """
7 #-----------------------------------------------------------------------------
7 #-----------------------------------------------------------------------------
8 # Copyright (c) 2013, the IPython Development Team.
8 # Copyright (c) 2013, the IPython Development Team.
9 #
9 #
10 # Distributed under the terms of the Modified BSD License.
10 # Distributed under the terms of the Modified BSD License.
11 #
11 #
12 # The full license is in the file COPYING.txt, distributed with this software.
12 # The full license is in the file COPYING.txt, distributed with this software.
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14
14
15 #-----------------------------------------------------------------------------
15 #-----------------------------------------------------------------------------
16 # Imports
16 # Imports
17 #-----------------------------------------------------------------------------
17 #-----------------------------------------------------------------------------
18
18
19 from ..utils.config import GlobalConfigurable
19 from ..utils.config import NbConvertBase
20 from IPython.utils.traitlets import Bool
20 from IPython.utils.traitlets import Bool
21
21
22 #-----------------------------------------------------------------------------
22 #-----------------------------------------------------------------------------
23 # Classes and Functions
23 # Classes and Functions
24 #-----------------------------------------------------------------------------
24 #-----------------------------------------------------------------------------
25
25
26 class ConfigurableTransformer(GlobalConfigurable):
26 class ConfigurableTransformer(NbConvertBase):
27 """ A configurable transformer
27 """ A configurable transformer
28
28
29 Inherit from this class if you wish to have configurability for your
29 Inherit from this class if you wish to have configurability for your
30 transformer.
30 transformer.
31
31
32 Any configurable traitlets this class exposed will be configurable in profiles
32 Any configurable traitlets this class exposed will be configurable in profiles
33 using c.SubClassName.atribute=value
33 using c.SubClassName.atribute=value
34
34
35 you can overwrite transform_cell to apply a transformation independently on each cell
35 you can overwrite transform_cell to apply a transformation independently on each cell
36 or __call__ if you prefer your own logic. See corresponding docstring for informations.
36 or __call__ if you prefer your own logic. See corresponding docstring for informations.
37
37
38 Disabled by default and can be enabled via the config by
38 Disabled by default and can be enabled via the config by
39 'c.YourTransformerName.enabled = True'
39 'c.YourTransformerName.enabled = True'
40 """
40 """
41
41
42 enabled = Bool(False, config=True)
42 enabled = Bool(False, config=True)
43
43
44 def __init__(self, **kw):
44 def __init__(self, **kw):
45 """
45 """
46 Public constructor
46 Public constructor
47
47
48 Parameters
48 Parameters
49 ----------
49 ----------
50 config : Config
50 config : Config
51 Configuration file structure
51 Configuration file structure
52 **kw : misc
52 **kw : misc
53 Additional arguments
53 Additional arguments
54 """
54 """
55
55
56 super(ConfigurableTransformer, self).__init__(**kw)
56 super(ConfigurableTransformer, self).__init__(**kw)
57
57
58
58
59 def __call__(self, nb, resources):
59 def __call__(self, nb, resources):
60 if self.enabled:
60 if self.enabled:
61 return self.call(nb,resources)
61 return self.call(nb,resources)
62 else:
62 else:
63 return nb, resources
63 return nb, resources
64
64
65
65
66 def call(self, nb, resources):
66 def call(self, nb, resources):
67 """
67 """
68 Transformation to apply on each notebook.
68 Transformation to apply on each notebook.
69
69
70 You should return modified nb, resources.
70 You should return modified nb, resources.
71 If you wish to apply your transform on each cell, you might want to
71 If you wish to apply your transform on each cell, you might want to
72 overwrite transform_cell method instead.
72 overwrite transform_cell method instead.
73
73
74 Parameters
74 Parameters
75 ----------
75 ----------
76 nb : NotebookNode
76 nb : NotebookNode
77 Notebook being converted
77 Notebook being converted
78 resources : dictionary
78 resources : dictionary
79 Additional resources used in the conversion process. Allows
79 Additional resources used in the conversion process. Allows
80 transformers to pass variables into the Jinja engine.
80 transformers to pass variables into the Jinja engine.
81 """
81 """
82 try :
82 try :
83 for worksheet in nb.worksheets :
83 for worksheet in nb.worksheets :
84 for index, cell in enumerate(worksheet.cells):
84 for index, cell in enumerate(worksheet.cells):
85 worksheet.cells[index], resources = self.transform_cell(cell, resources, index)
85 worksheet.cells[index], resources = self.transform_cell(cell, resources, index)
86 return nb, resources
86 return nb, resources
87 except NotImplementedError:
87 except NotImplementedError:
88 raise NotImplementedError('should be implemented by subclass')
88 raise NotImplementedError('should be implemented by subclass')
89
89
90
90
91 def transform_cell(self, cell, resources, index):
91 def transform_cell(self, cell, resources, index):
92 """
92 """
93 Overwrite if you want to apply a transformation on each cell. You
93 Overwrite if you want to apply a transformation on each cell. You
94 should return modified cell and resource dictionary.
94 should return modified cell and resource dictionary.
95
95
96 Parameters
96 Parameters
97 ----------
97 ----------
98 cell : NotebookNode cell
98 cell : NotebookNode cell
99 Notebook cell being processed
99 Notebook cell being processed
100 resources : dictionary
100 resources : dictionary
101 Additional resources used in the conversion process. Allows
101 Additional resources used in the conversion process. Allows
102 transformers to pass variables into the Jinja engine.
102 transformers to pass variables into the Jinja engine.
103 index : int
103 index : int
104 Index of the cell being processed
104 Index of the cell being processed
105 """
105 """
106
106
107 raise NotImplementedError('should be implemented by subclass')
107 raise NotImplementedError('should be implemented by subclass')
108 return cell, resources
108 return cell, resources
109
109
@@ -1,37 +1,37 b''
1 """Global configuration class."""
1 """Global configuration class."""
2 #-----------------------------------------------------------------------------
2 #-----------------------------------------------------------------------------
3 # Copyright (c) 2013, the IPython Development Team.
3 # Copyright (c) 2013, the IPython Development Team.
4 #
4 #
5 # Distributed under the terms of the Modified BSD License.
5 # Distributed under the terms of the Modified BSD License.
6 #
6 #
7 # The full license is in the file COPYING.txt, distributed with this software.
7 # The full license is in the file COPYING.txt, distributed with this software.
8 #-----------------------------------------------------------------------------
8 #-----------------------------------------------------------------------------
9
9
10 #-----------------------------------------------------------------------------
10 #-----------------------------------------------------------------------------
11 # Imports
11 # Imports
12 #-----------------------------------------------------------------------------
12 #-----------------------------------------------------------------------------
13
13
14 from IPython.utils.traitlets import List
14 from IPython.utils.traitlets import List
15 from IPython.config.configurable import Configurable
15 from IPython.config.configurable import Configurable
16
16
17 #-----------------------------------------------------------------------------
17 #-----------------------------------------------------------------------------
18 # Classes and functions
18 # Classes and functions
19 #-----------------------------------------------------------------------------
19 #-----------------------------------------------------------------------------
20
20
21 class GlobalConfigurable(Configurable):
21 class NbConvertBase(Configurable):
22 """Global configurable class for shared config
22 """Global configurable class for shared config
23
23
24 Usefull for display data priority that might be use by many trasnformers
24 Usefull for display data priority that might be use by many trasnformers
25 """
25 """
26
26
27 display_data_priority = List(['html', 'pdf', 'svg', 'latex', 'png', 'jpg', 'jpeg' , 'text'],
27 display_data_priority = List(['html', 'pdf', 'svg', 'latex', 'png', 'jpg', 'jpeg' , 'text'],
28 config=True,
28 config=True,
29 help= """
29 help= """
30 An ordered list of prefered output type, the first
30 An ordered list of prefered output type, the first
31 encounterd will usually be used when converting discarding
31 encounterd will usually be used when converting discarding
32 the others.
32 the others.
33 """
33 """
34 )
34 )
35
35
36 def __init__(self, **kw):
36 def __init__(self, **kw):
37 super(GlobalConfigurable, self).__init__(**kw)
37 super(NbConvertBase, self).__init__(**kw)
@@ -1,57 +1,57 b''
1 #!/usr/bin/env python
1 #!/usr/bin/env python
2 """
2 """
3 Contains writer base class.
3 Contains writer base class.
4 """
4 """
5 #-----------------------------------------------------------------------------
5 #-----------------------------------------------------------------------------
6 #Copyright (c) 2013, the IPython Development Team.
6 #Copyright (c) 2013, the IPython Development Team.
7 #
7 #
8 #Distributed under the terms of the Modified BSD License.
8 #Distributed under the terms of the Modified BSD License.
9 #
9 #
10 #The full license is in the file COPYING.txt, distributed with this software.
10 #The full license is in the file COPYING.txt, distributed with this software.
11 #-----------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
12
12
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14 # Imports
14 # Imports
15 #-----------------------------------------------------------------------------
15 #-----------------------------------------------------------------------------
16
16
17 from IPython.utils.traitlets import List
17 from IPython.utils.traitlets import List
18
18
19 from ..utils.config import GlobalConfigurable
19 from ..utils.config import NbConvertBase
20
20
21 #-----------------------------------------------------------------------------
21 #-----------------------------------------------------------------------------
22 # Classes
22 # Classes
23 #-----------------------------------------------------------------------------
23 #-----------------------------------------------------------------------------
24
24
25 class WriterBase(GlobalConfigurable):
25 class WriterBase(NbConvertBase):
26 """Consumes output from nbconvert export...() methods and writes to a
26 """Consumes output from nbconvert export...() methods and writes to a
27 useful location. """
27 useful location. """
28
28
29
29
30 files = List([], config=True, help="""
30 files = List([], config=True, help="""
31 List of the files that the notebook references. Files will be
31 List of the files that the notebook references. Files will be
32 included with written output.""")
32 included with written output.""")
33
33
34
34
35 def __init__(self, config=None, **kw):
35 def __init__(self, config=None, **kw):
36 """
36 """
37 Constructor
37 Constructor
38 """
38 """
39 super(WriterBase, self).__init__(config=config, **kw)
39 super(WriterBase, self).__init__(config=config, **kw)
40
40
41
41
42 def write(self, output, resources, **kw):
42 def write(self, output, resources, **kw):
43 """
43 """
44 Consume and write Jinja output.
44 Consume and write Jinja output.
45
45
46 Parameters
46 Parameters
47 ----------
47 ----------
48 output : string
48 output : string
49 Conversion results. This string contains the file contents of the
49 Conversion results. This string contains the file contents of the
50 converted file.
50 converted file.
51 resources : dict
51 resources : dict
52 Resources created and filled by the nbconvert conversion process.
52 Resources created and filled by the nbconvert conversion process.
53 Includes output from transformers, such as the extract figure
53 Includes output from transformers, such as the extract figure
54 transformer.
54 transformer.
55 """
55 """
56
56
57 raise NotImplementedError()
57 raise NotImplementedError()
General Comments 0
You need to be logged in to leave comments. Login now