Show More
@@ -26,13 +26,10 b' import os' | |||||
26 | #From IPython |
|
26 | #From IPython | |
27 | #All the stuff needed for the configurable things |
|
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 |
|
|||
30 | from IPython.utils.traitlets import Unicode, Bool |
|
|||
31 |
|
29 | |||
32 | #Local imports |
|
30 | #Local imports | |
33 |
from api.e |
|
31 | from nbconvert.api.convert import export_by_name | |
34 | from converters.config import GlobalConfigurable #TODO |
|
32 | from nbconvert.api.exporter import Exporter | |
35 | from converters.transformers import (ExtractFigureTransformer) #TODO |
|
|||
36 |
|
33 | |||
37 | #----------------------------------------------------------------------------- |
|
34 | #----------------------------------------------------------------------------- | |
38 | #Globals and constants |
|
35 | #Globals and constants | |
@@ -78,8 +75,6 b' class NbconvertApp(Application):' | |||||
78 |
|
75 | |||
79 | #Register class here to have help with help all |
|
76 | #Register class here to have help with help all | |
80 | self.classes.insert(0, Exporter) |
|
77 | self.classes.insert(0, Exporter) | |
81 | self.classes.insert(0, ExtractFigureTransformer) |
|
|||
82 | self.classes.insert(0, GlobalConfigurable) |
|
|||
83 |
|
78 | |||
84 |
|
79 | |||
85 | def start(self, argv=None): |
|
80 | def start(self, argv=None): | |
@@ -91,51 +86,57 b' class NbconvertApp(Application):' | |||||
91 | #Call base |
|
86 | #Call base | |
92 | super(NbconvertApp, self).start() |
|
87 | super(NbconvertApp, self).start() | |
93 |
|
88 | |||
94 | #The last arguments in chain of arguments will be used as conversion |
|
89 | #The last arguments in chain of arguments will be used as conversion type | |
95 |
ipynb_file = (self.extra_args |
|
90 | ipynb_file = (self.extra_args)[2] | |
96 |
|
91 | export_type = (self.extra_args)[1] | ||
97 | #If you are writting a custom transformer, append it to the dictionary |
|
|||
98 | #below. |
|
|||
99 | userpreprocessors = {} |
|
|||
100 |
|
||||
101 | #Create the Jinja template exporter. TODO: Add ability to |
|
|||
102 | #import in IPYNB aswell |
|
|||
103 | exporter = Exporter(config=self.config, preprocessors=userpreprocessors,export_format=export_format) |
|
|||
104 |
|
92 | |||
105 | #Export |
|
93 | #Export | |
106 |
output, resources = export |
|
94 | output, resources, exporter = export_by_name(ipynb_file, export_type) | |
107 | if exporter.stdout : |
|
95 | ||
|
96 | destination_filename = None | |||
|
97 | destination_directory = None | |||
|
98 | if exporter.write: | |||
|
99 | ||||
|
100 | #Get the file name without the '.ipynb' (6 chars) extension and then | |||
|
101 | #remove any addition periods and spaces. The resulting name will | |||
|
102 | #be used to create the directory that the files will be exported | |||
|
103 | #into. | |||
|
104 | out_root = ipynb_file[:-6].replace('.', '_').replace(' ', '_') | |||
|
105 | destination_filename = os.path.join(out_root+'.'+exporter.fileext) | |||
|
106 | ||||
|
107 | destination_directory = out_root+'_files' | |||
|
108 | if not os.path.exists(destination_directory): | |||
|
109 | os.mkdir(destination_directory) | |||
|
110 | ||||
|
111 | #Write the results | |||
|
112 | if exporter.stdout or exporter.write: | |||
|
113 | self._write_results(exporter.stdout, destination_filename, destination_directory, output, resources) | |||
|
114 | ||||
|
115 | ||||
|
116 | def _write_results(self, stdout, destination_filename, destination_directory, output, resources): | |||
|
117 | if stdout: | |||
108 | print(output.encode('utf-8')) |
|
118 | print(output.encode('utf-8')) | |
109 |
|
119 | |||
110 | #Get the file name without the '.ipynb' (6 chars) extension and then |
|
|||
111 | #remove any addition periods and spaces. The resulting name will |
|
|||
112 | #be used to create the directory that the files will be exported |
|
|||
113 | #into. |
|
|||
114 | out_root = ipynb_file[:-6].replace('.', '_').replace(' ', '_') |
|
|||
115 |
|
||||
116 | #Write file output from conversion. |
|
120 | #Write file output from conversion. | |
117 | if exporter.write : |
|
121 | if not destination_filename is None: | |
118 |
with io.open( |
|
122 | with io.open(destination_filename, 'w') as f: | |
119 | f.write(output) |
|
123 | f.write(output) | |
120 |
|
124 | |||
121 | #Output any associate figures into the same "root" directory. |
|
125 | #Output any associate figures into the same "root" directory. | |
122 | binkeys = resources.get('figures', {}).get('binary',{}).keys() |
|
126 | binkeys = resources.get('figures', {}).get('binary',{}).keys() | |
123 | textkeys = resources.get('figures', {}).get('text',{}).keys() |
|
127 | textkeys = resources.get('figures', {}).get('text',{}).keys() | |
124 | if binkeys or textkeys : |
|
128 | if binkeys or textkeys : | |
125 | if exporter.write: |
|
129 | if not destination_directory is None: | |
126 | files_dir = out_root+'_files' |
|
|||
127 | if not os.path.exists(out_root+'_files'): |
|
|||
128 | os.mkdir(files_dir) |
|
|||
129 | for key in binkeys: |
|
130 | for key in binkeys: | |
130 |
with io.open(os.path.join( |
|
131 | with io.open(os.path.join(destination_directory, key), 'wb') as f: | |
131 | f.write(resources['figures']['binary'][key]) |
|
132 | f.write(resources['figures']['binary'][key]) | |
132 | for key in textkeys: |
|
133 | for key in textkeys: | |
133 |
with io.open(os.path.join( |
|
134 | with io.open(os.path.join(destination_directory, key), 'w') as f: | |
134 | f.write(resources['figures']['text'][key]) |
|
135 | f.write(resources['figures']['text'][key]) | |
135 |
|
136 | |||
136 | #Figures that weren't exported which will need to be created by the |
|
137 | #Figures that weren't exported which will need to be created by the | |
137 | #user. Tell the user what figures these are. |
|
138 | #user. Tell the user what figures these are. | |
138 |
|
|
139 | if stdout: | |
139 | print(KEYS_PROMPT_HEAD) |
|
140 | print(KEYS_PROMPT_HEAD) | |
140 | print(resources['figures'].keys()) |
|
141 | print(resources['figures'].keys()) | |
141 | print(KEYS_PROMPT_BODY) |
|
142 | print(KEYS_PROMPT_BODY) |
@@ -11,11 +11,73 b' they are converted.' | |||||
11 | # The full license is in the file COPYING.txt, distributed with this software. |
|
11 | # The full license is in the file COPYING.txt, distributed with this software. | |
12 | #----------------------------------------------------------------------------- |
|
12 | #----------------------------------------------------------------------------- | |
13 |
|
13 | |||
|
14 | from .exporter import Exporter | |||
|
15 | ||||
|
16 | from .html import HtmlExporter | |||
|
17 | from .latex import LatexExporter | |||
|
18 | from .markdown import MarkdownExporter | |||
|
19 | from .python import PythonExporter | |||
|
20 | from .reveal import RevealExporter | |||
|
21 | from .rst import RstExporter | |||
|
22 | from .sphinx import SphinxExporter | |||
|
23 | ||||
|
24 | from IPython.nbformat.v3.nbbase import NotebookNode | |||
|
25 | ||||
14 | #----------------------------------------------------------------------------- |
|
26 | #----------------------------------------------------------------------------- | |
15 | # Functions |
|
27 | # Functions | |
16 | #----------------------------------------------------------------------------- |
|
28 | #----------------------------------------------------------------------------- | |
17 |
|
29 | |||
18 | def export_sphinx_report(nb, config=None): |
|
30 | def export(nb, config=None, transformers=None, filters=None, exporter_type=Exporter): | |
19 |
|
|
31 | ||
|
32 | #Check arguments | |||
|
33 | if exporter_type is None: | |||
|
34 | raise TypeError("Exporter is None") | |||
|
35 | elif not isinstance(exporter_type, Exporter): | |||
|
36 | raise TypeError("Exporter type does not inherit from Exporter (base)") | |||
|
37 | ||||
|
38 | if nb is None: | |||
|
39 | raise TypeError("nb is None") | |||
|
40 | ||||
|
41 | #Create the exporter | |||
|
42 | exporter_instance = exporter_type(transformers, filters, config) | |||
|
43 | ||||
|
44 | #Try to convert the notebook using the appropriate conversion function. | |||
|
45 | if isinstance(nb, NotebookNode): | |||
|
46 | return exporter_instance.from_notebook_node(nb), exporter_instance | |||
|
47 | elif isinstance(nb, str): | |||
|
48 | return exporter_instance.from_filename(nb), exporter_instance | |||
|
49 | else: | |||
|
50 | return exporter_instance.from_file(nb), exporter_instance | |||
|
51 | ||||
|
52 | ||||
|
53 | def export_sphinx(nb, config=None, transformers=None, filters=None): | |||
|
54 | return export(nb, config, transformers, filters, SphinxExporter) | |||
|
55 | ||||
|
56 | def export_html(nb, config=None, transformers=None, filters=None): | |||
|
57 | return export(nb, config, transformers, filters, HtmlExporter) | |||
|
58 | ||||
|
59 | def export_latex(nb, config=None, transformers=None, filters=None): | |||
|
60 | return export(nb, config, transformers, filters, LatexExporter) | |||
|
61 | ||||
|
62 | def export_markdown(nb, config=None, transformers=None, filters=None): | |||
|
63 | return export(nb, config, transformers, filters, MarkdownExporter) | |||
|
64 | ||||
|
65 | def export_python(nb, config=None, transformers=None, filters=None): | |||
|
66 | return export(nb, config, transformers, filters, PythonExporter) | |||
|
67 | ||||
|
68 | def export_reveal(nb, config=None, transformers=None, filters=None): | |||
|
69 | return export(nb, config, transformers, filters, RevealExporter) | |||
|
70 | ||||
|
71 | def export_rst(nb, config=None, transformers=None, filters=None): | |||
|
72 | return export(nb, config, transformers, filters, RstExporter) | |||
|
73 | ||||
|
74 | EXPORT_FUNCTIONS = {"sphinx": export_sphinx, | |||
|
75 | "html": export_html, | |||
|
76 | "latex": export_latex, | |||
|
77 | "markdown": export_markdown, | |||
|
78 | "python": export_python, | |||
|
79 | "reveal": export_reveal, | |||
|
80 | "rst": export_rst} | |||
20 |
|
81 | |||
21 | #TODO: Add basic export/import utility functions. |
|
82 | def export_by_name(nb, template_name, config=None, transformers=None, filters=None): | |
|
83 | return EXPORT_FUNCTIONS[template_name](nb, config, transformers, filters) No newline at end of file |
@@ -23,7 +23,6 b' from __future__ import print_function, absolute_import' | |||||
23 | # Stdlib imports |
|
23 | # Stdlib imports | |
24 | import io |
|
24 | import io | |
25 | import os |
|
25 | import os | |
26 | import copy |
|
|||
27 |
|
26 | |||
28 | # IPython imports |
|
27 | # IPython imports | |
29 | from IPython.config.configurable import Configurable |
|
28 | from IPython.config.configurable import Configurable | |
@@ -54,7 +53,6 b' import transformers.coalescestreams' | |||||
54 | #Standard Jinja2 environment constants |
|
53 | #Standard Jinja2 environment constants | |
55 | TEMPLATE_PATH = "/../templates/" |
|
54 | TEMPLATE_PATH = "/../templates/" | |
56 | TEMPLATE_SKELETON_PATH = "/../templates/skeleton/" |
|
55 | TEMPLATE_SKELETON_PATH = "/../templates/skeleton/" | |
57 | TEMPLATE_EXTENSION = ".tpl" |
|
|||
58 |
|
56 | |||
59 | #Jinja2 extensions to load. |
|
57 | #Jinja2 extensions to load. | |
60 | JINJA_EXTENSIONS = ['jinja2.ext.loopcontrols'] |
|
58 | JINJA_EXTENSIONS = ['jinja2.ext.loopcontrols'] | |
@@ -63,13 +61,6 b" JINJA_EXTENSIONS = ['jinja2.ext.loopcontrols']" | |||||
63 | # Classes and functions |
|
61 | # Classes and functions | |
64 | #----------------------------------------------------------------------------- |
|
62 | #----------------------------------------------------------------------------- | |
65 | class Exporter(Configurable): |
|
63 | class Exporter(Configurable): | |
66 | """ A Jinja2 base converter templates |
|
|||
67 |
|
||||
68 | Pre-process the IPYNB files, feed it through Jinja2 templates, |
|
|||
69 | and spit an converted files and a data object with other data |
|
|||
70 | should be mostly configurable |
|
|||
71 | """ |
|
|||
72 |
|
||||
73 | pre_transformer_order = List(['haspyout_transformer'], |
|
64 | pre_transformer_order = List(['haspyout_transformer'], | |
74 | config=True, |
|
65 | config=True, | |
75 | help= """ |
|
66 | help= """ | |
@@ -99,6 +90,9 b' class Exporter(Configurable):' | |||||
99 | along with potential extracted resources.""" |
|
90 | along with potential extracted resources.""" | |
100 | ) |
|
91 | ) | |
101 |
|
92 | |||
|
93 | #Extension that the template files use. | |||
|
94 | template_extension = ".tpl" | |||
|
95 | ||||
102 | #Processors that process the input data prior to the export, set in the |
|
96 | #Processors that process the input data prior to the export, set in the | |
103 | #constructor for this class. |
|
97 | #constructor for this class. | |
104 | preprocessors = [] |
|
98 | preprocessors = [] | |
@@ -106,32 +100,11 b' class Exporter(Configurable):' | |||||
106 | # Public Constructor ##################################################### |
|
100 | # Public Constructor ##################################################### | |
107 |
|
101 | |||
108 | def __init__(self, preprocessors=None, jinja_filters=None, config=None, **kw): |
|
102 | def __init__(self, preprocessors=None, jinja_filters=None, config=None, **kw): | |
109 | """ Init a new converter. |
|
|||
110 |
|
||||
111 | config: the Configurable config object to pass around. |
|
|||
112 |
|
||||
113 | preprocessors: dict of **available** key/value function to run on |
|
|||
114 | ipynb json data before conversion to extract/in-line file. |
|
|||
115 | See `transformer.py` and `ConfigurableTransformers` |
|
|||
116 |
|
||||
117 | set the order in which the transformers should apply |
|
|||
118 | with the `pre_transformer_order` trait of this class |
|
|||
119 |
|
||||
120 | transformers registered by this key will take precedence on |
|
|||
121 | default one. |
|
|||
122 |
|
||||
123 | jinja_filters: dict of supplementary jinja filter that should be made |
|
|||
124 | available in template. If those are of Configurable Class type, |
|
|||
125 | they will be instanciated with the config object as argument. |
|
|||
126 |
|
||||
127 | user defined filter will overwrite the one available by default. |
|
|||
128 | """ |
|
|||
129 |
|
103 | |||
130 | #Call the base class constructor |
|
104 | #Call the base class constructor | |
131 | super(Exporter, self).__init__(config=config, **kw) |
|
105 | super(Exporter, self).__init__(config=config, **kw) | |
132 |
|
106 | |||
133 | #Standard environment |
|
107 | #Standard environment | |
134 | self.ext = TEMPLATE_EXTENSION |
|
|||
135 | self._init_environment() |
|
108 | self._init_environment() | |
136 |
|
109 | |||
137 | #TODO: Implement reflection style methods to get user transformers. |
|
110 | #TODO: Implement reflection style methods to get user transformers. | |
@@ -156,63 +129,35 b' class Exporter(Configurable):' | |||||
156 | self.environment.filters[key] = user_filter(config=config) |
|
129 | self.environment.filters[key] = user_filter(config=config) | |
157 | else: |
|
130 | else: | |
158 | self.environment.filters[key] = user_filter |
|
131 | self.environment.filters[key] = user_filter | |
159 |
|
||||
160 |
|
||||
161 | #Set the default datatype priority. |
|
|||
162 | self._set_datatype_priority(['svg', 'png', 'latex', 'jpg', 'jpeg','text']) |
|
|||
163 |
|
||||
164 |
|
132 | |||
165 | # Public methods ######################################### |
|
133 | # Public methods ######################################### | |
166 |
|
134 | |||
167 | def from_notebook_node(self, nb): |
|
135 | def from_notebook_node(self, nb): | |
168 | """Export NotebookNode instance |
|
|||
169 |
|
||||
170 | nb: NotebookNode to export. |
|
|||
171 |
|
||||
172 | Returns both the converted ipynb file and a dict containing the |
|
|||
173 | resources created along the way via the transformers and Jinja2 |
|
|||
174 | processing. |
|
|||
175 | """ |
|
|||
176 |
|
||||
177 | nb, resources = self._preprocess(nb) |
|
136 | nb, resources = self._preprocess(nb) | |
178 |
|
137 | |||
179 | #Load the template file. |
|
138 | #Load the template file. | |
180 | self.template = self.environment.get_template(self.template_file+self.ext) |
|
139 | self.template = self.environment.get_template(self.template_file+self.template_extension) | |
181 |
|
140 | |||
182 | return self.template.render(nb=nb, resources=resources), resources |
|
141 | return self.template.render(nb=nb, resources=resources), resources | |
183 |
|
142 | |||
184 |
|
143 | |||
185 | def from_filename(self, filename): |
|
144 | def from_filename(self, filename): | |
186 | """Read and export a notebook from a filename |
|
|||
187 |
|
||||
188 | filename: Filename of the notebook file to export. |
|
|||
189 |
|
||||
190 | Returns both the converted ipynb file and a dict containing the |
|
|||
191 | resources created along the way via the transformers and Jinja2 |
|
|||
192 | processing. |
|
|||
193 | """ |
|
|||
194 | with io.open(filename) as f: |
|
145 | with io.open(filename) as f: | |
195 | return self.from_notebook_node(nbformat.read(f, 'json')) |
|
146 | return self.from_notebook_node(nbformat.read(f, 'json')) | |
196 |
|
147 | |||
197 |
|
148 | |||
198 | def from_file(self, file_stream): |
|
149 | def from_file(self, file_stream): | |
199 | """Read and export a notebook from a file stream |
|
|||
200 |
|
||||
201 | file_stream: File handle of file that contains notebook data. |
|
|||
202 |
|
||||
203 | Returns both the converted ipynb file and a dict containing the |
|
|||
204 | resources created along the way via the transformers and Jinja2 |
|
|||
205 | processing. |
|
|||
206 | """ |
|
|||
207 |
|
||||
208 | return self.from_notebook_node(nbformat.read(file_stream, 'json')) |
|
150 | return self.from_notebook_node(nbformat.read(file_stream, 'json')) | |
209 |
|
151 | |||
210 |
|
152 | |||
211 | def register_transformer(self, transformer): |
|
153 | def register_transformer(self, transformer): | |
212 | if MetaHasTraits(transformer): |
|
154 | if MetaHasTraits(transformer): | |
213 |
|
|
155 | transformer_instance = transformer(config=self.config) | |
|
156 | self.preprocessors.append(transformer_instance) | |||
|
157 | return transformer_instance | |||
214 | else: |
|
158 | else: | |
215 | self.preprocessors.append(transformer) |
|
159 | self.preprocessors.append(transformer) | |
|
160 | return transformer | |||
216 |
|
161 | |||
217 |
|
162 | |||
218 | def register_filter(self, name, filter): |
|
163 | def register_filter(self, name, filter): | |
@@ -222,6 +167,7 b' class Exporter(Configurable):' | |||||
222 | self.environment.filters[name] = filter |
|
167 | self.environment.filters[name] = filter | |
223 | return self.environment.filters[name] |
|
168 | return self.environment.filters[name] | |
224 |
|
169 | |||
|
170 | ||||
225 | # Protected and Private methods ######################################### |
|
171 | # Protected and Private methods ######################################### | |
226 |
|
172 | |||
227 | def _register_transformers(self): |
|
173 | def _register_transformers(self): | |
@@ -230,7 +176,6 b' class Exporter(Configurable):' | |||||
230 | #Remember the figure extraction transformer so it can be enabled and |
|
176 | #Remember the figure extraction transformer so it can be enabled and | |
231 | #disabled easily later. |
|
177 | #disabled easily later. | |
232 | self.extract_figure_transformer = self.register_transformer(transformers.extractfigure.ExtractFigureTransformer) |
|
178 | self.extract_figure_transformer = self.register_transformer(transformers.extractfigure.ExtractFigureTransformer) | |
233 | self.extract_figure_transformer.enabled = False |
|
|||
234 |
|
179 | |||
235 |
|
180 | |||
236 | def _register_filters(self): |
|
181 | def _register_filters(self): | |
@@ -250,11 +195,6 b' class Exporter(Configurable):' | |||||
250 | self.register_filter('rm_fake', filters.strings.rm_fake) |
|
195 | self.register_filter('rm_fake', filters.strings.rm_fake) | |
251 | self.register_filter('rm_math_space', filters.latex.rm_math_space) |
|
196 | self.register_filter('rm_math_space', filters.latex.rm_math_space) | |
252 | self.register_filter('wrap', filters.strings.wrap) |
|
197 | self.register_filter('wrap', filters.strings.wrap) | |
253 |
|
||||
254 |
|
||||
255 | def _set_datatype_priority(self, priority): |
|
|||
256 | self.extract_figure_transformer.display_data_priority=copy.copy(priority) |
|
|||
257 | self.display_data_priority=copy.copy(priority) |
|
|||
258 |
|
198 | |||
259 |
|
199 | |||
260 | def _init_environment(self): |
|
200 | def _init_environment(self): | |
@@ -268,11 +208,6 b' class Exporter(Configurable):' | |||||
268 |
|
208 | |||
269 |
|
209 | |||
270 | def _preprocess(self, nb): |
|
210 | def _preprocess(self, nb): | |
271 | """ Preprocess the notebook using the transformers specific |
|
|||
272 | for the current export format. |
|
|||
273 |
|
||||
274 | nb: Notebook to preprocess |
|
|||
275 | """ |
|
|||
276 |
|
211 | |||
277 | #Dict of 'resources' that can be filled by the preprocessors. |
|
212 | #Dict of 'resources' that can be filled by the preprocessors. | |
278 | resources = {} |
|
213 | resources = {} | |
@@ -282,4 +217,3 b' class Exporter(Configurable):' | |||||
282 | for transformer in self.preprocessors: |
|
217 | for transformer in self.preprocessors: | |
283 | nb, resources = transformer(nb, resources) |
|
218 | nb, resources = transformer(nb, resources) | |
284 | return nb, resources |
|
219 | return nb, resources | |
285 |
|
@@ -1,4 +1,4 b'' | |||||
1 |
|
|
1 | """TODO: Docstring | |
2 | """ |
|
2 | """ | |
3 |
|
3 | |||
4 | #----------------------------------------------------------------------------- |
|
4 | #----------------------------------------------------------------------------- | |
@@ -16,27 +16,22 b'' | |||||
16 | # local import |
|
16 | # local import | |
17 | import exporter |
|
17 | import exporter | |
18 | import transformers.csshtmlheader |
|
18 | import transformers.csshtmlheader | |
|
19 | from IPython.utils.traitlets import Unicode | |||
19 |
|
20 | |||
20 | #----------------------------------------------------------------------------- |
|
21 | #----------------------------------------------------------------------------- | |
21 | # Classes |
|
22 | # Classes | |
22 | #----------------------------------------------------------------------------- |
|
23 | #----------------------------------------------------------------------------- | |
23 | class HtmlExporter(exporter.Exporter): |
|
24 | class HtmlExporter(exporter.Exporter): | |
24 |
|
25 | |||
25 | def __init__(self, preprocessors=None, jinja_filters=None, config=None, full_html=True, **kw): |
|
26 | file_extension = Unicode( | |
26 |
|
27 | 'html', config=True, | ||
27 | #Call base class constructor. |
|
28 | help="Extension of the file that should be written to disk" | |
28 | super(exporter.Exporter, self).__init__(preprocessors, jinja_filters, config, **kw) |
|
29 | ) | |
|
30 | ||||
|
31 | template_file = Unicode( | |||
|
32 | 'fullhtml', config=True, | |||
|
33 | help="Name of the template file to use") | |||
29 |
|
34 | |||
30 | #Set defaults |
|
|||
31 | self.file_extension = "html" |
|
|||
32 | self.extract_figure_transformer.enabled = True |
|
|||
33 |
|
||||
34 | #Load the correct template |
|
|||
35 | if full_html: |
|
|||
36 | self.template_file = "fullhtml" |
|
|||
37 | else: |
|
|||
38 | self.template_file = "basichtml" |
|
|||
39 |
|
||||
40 | def _register_transformers(self): |
|
35 | def _register_transformers(self): | |
41 |
|
36 | |||
42 | #Register the transformers of the base class. |
|
37 | #Register the transformers of the base class. |
@@ -1,12 +1,3 b'' | |||||
1 | """Latex exporter for the notebook conversion pipeline. |
|
|||
2 |
|
||||
3 | This module defines Exporter, a highly configurable converter |
|
|||
4 | that uses Jinja2 to export notebook files into different format. |
|
|||
5 |
|
||||
6 | You can register both pre-transformers that will act on the notebook format |
|
|||
7 | before conversion and jinja filter that would then be available in the templates |
|
|||
8 | """ |
|
|||
9 |
|
||||
10 |
|
|
1 | #----------------------------------------------------------------------------- | |
11 | # Copyright (c) 2013, the IPython Development Team. |
|
2 | # Copyright (c) 2013, the IPython Development Team. | |
12 | # |
|
3 | # | |
@@ -20,18 +11,13 b' before conversion and jinja filter that would then be available in the templates' | |||||
20 | #----------------------------------------------------------------------------- |
|
11 | #----------------------------------------------------------------------------- | |
21 |
|
12 | |||
22 | # Stdlib imports |
|
13 | # Stdlib imports | |
23 | import io |
|
|||
24 | import os |
|
14 | import os | |
25 |
|
15 | |||
26 | # IPython imports |
|
16 | # IPython imports | |
27 |
from IPython. |
|
17 | from IPython.utils.traitlets import Unicode | |
28 | from IPython.nbformat import current as nbformat |
|
|||
29 | from IPython.utils.traitlets import MetaHasTraits, Unicode, List, Bool |
|
|||
30 | from IPython.utils.text import indent |
|
|||
31 |
|
18 | |||
32 | # other libs/dependencies |
|
19 | # other libs/dependencies | |
33 | from jinja2 import Environment, FileSystemLoader |
|
20 | from jinja2 import Environment, FileSystemLoader | |
34 | from markdown import markdown |
|
|||
35 |
|
21 | |||
36 | # local import |
|
22 | # local import | |
37 | import exporter |
|
23 | import exporter | |
@@ -45,7 +31,6 b' from transformers.latex import LatexTransformer' | |||||
45 | #Latex Jinja2 constants |
|
31 | #Latex Jinja2 constants | |
46 | LATEX_TEMPLATE_PATH = "/../templates/tex/" |
|
32 | LATEX_TEMPLATE_PATH = "/../templates/tex/" | |
47 | LATEX_TEMPLATE_SKELETON_PATH = "/../templates/tex/skeleton/" |
|
33 | LATEX_TEMPLATE_SKELETON_PATH = "/../templates/tex/skeleton/" | |
48 | LATEX_TEMPLATE_EXTENSION = ".tplx" |
|
|||
49 |
|
34 | |||
50 | #Special Jinja2 syntax that will not conflict when exporting latex. |
|
35 | #Special Jinja2 syntax that will not conflict when exporting latex. | |
51 | LATEX_JINJA_COMMENT_BLOCK = ["((=", "=))"] |
|
36 | LATEX_JINJA_COMMENT_BLOCK = ["((=", "=))"] | |
@@ -56,28 +41,28 b' LATEX_JINJA_LOGIC_BLOCK = ["((*", "*))"]' | |||||
56 | # Classes and functions |
|
41 | # Classes and functions | |
57 | #----------------------------------------------------------------------------- |
|
42 | #----------------------------------------------------------------------------- | |
58 | class LatexExporter(exporter.Exporter): |
|
43 | class LatexExporter(exporter.Exporter): | |
59 | """ A Jinja2 latex exporter |
|
|||
60 |
|
44 | |||
61 | Preprocess the ipynb files, feed it through jinja templates, |
|
45 | #Extension that the template files use. | |
62 | and spit an converted files and a data object with other data |
|
46 | template_extension = ".tplx" | |
63 | should be mostly configurable |
|
47 | ||
64 | """ |
|
48 | file_extension = Unicode( | |
|
49 | 'tex', config=True, | |||
|
50 | help="Extension of the file that should be written to disk") | |||
65 |
|
51 | |||
|
52 | template_file = Unicode( | |||
|
53 | 'latex_base', config=True, | |||
|
54 | help="Name of the template file to use") | |||
|
55 | ||||
66 | def __init__(self, preprocessors=None, jinja_filters=None, config=None, **kw): |
|
56 | def __init__(self, preprocessors=None, jinja_filters=None, config=None, **kw): | |
67 |
|
57 | |||
68 | #Call base class constructor. |
|
58 | #Call base class constructor. | |
69 | super(exporter.Exporter, self).__init__(preprocessors, jinja_filters, config, **kw) |
|
59 | super(exporter.Exporter, self).__init__(preprocessors, jinja_filters, config, **kw) | |
70 |
|
60 | |||
71 | #Set defaults |
|
61 | self.extract_figure_transformer.display_data_priority = ['latex', 'svg', 'png', 'jpg', 'jpeg' , 'text'] | |
72 | self.file_extension = "tex" |
|
|||
73 | self._set_datatype_priority(['latex', 'svg', 'png', 'jpg', 'jpeg' , 'text']) |
|
|||
74 | self.extract_figure_transformer.enabled = True |
|
|||
75 | self.extract_figure_transformer.extra_ext_map={'svg':'pdf'} |
|
62 | self.extract_figure_transformer.extra_ext_map={'svg':'pdf'} | |
76 | self.template_file = "latex_base" |
|
|||
77 |
|
63 | |||
78 |
|
64 | |||
79 | def _init_environment(self): |
|
65 | def _init_environment(self): | |
80 | self.ext = LATEX_TEMPLATE_EXTENSION |
|
|||
81 | self.environment = Environment( |
|
66 | self.environment = Environment( | |
82 | loader=FileSystemLoader([ |
|
67 | loader=FileSystemLoader([ | |
83 | os.path.dirname(os.path.realpath(__file__)) + LATEX_TEMPLATE_PATH, |
|
68 | os.path.dirname(os.path.realpath(__file__)) + LATEX_TEMPLATE_PATH, |
@@ -1,5 +1,3 b'' | |||||
1 | """TODO: Docstring |
|
|||
2 | """ |
|
|||
3 |
|
|
1 | ||
4 | #----------------------------------------------------------------------------- |
|
2 | #----------------------------------------------------------------------------- | |
5 | # Copyright (c) 2013, the IPython Development Team. |
|
3 | # Copyright (c) 2013, the IPython Development Team. | |
@@ -15,18 +13,17 b'' | |||||
15 |
|
13 | |||
16 | # local import |
|
14 | # local import | |
17 | import exporter |
|
15 | import exporter | |
|
16 | from IPython.utils.traitlets import Unicode | |||
18 |
|
17 | |||
19 | #----------------------------------------------------------------------------- |
|
18 | #----------------------------------------------------------------------------- | |
20 | # Classes |
|
19 | # Classes | |
21 | #----------------------------------------------------------------------------- |
|
20 | #----------------------------------------------------------------------------- | |
22 | class MarkdownExporter(exporter.Exporter): |
|
21 | class MarkdownExporter(exporter.Exporter): | |
23 |
|
22 | |||
24 | def __init__(self, preprocessors=None, jinja_filters=None, config=None, **kw): |
|
23 | file_extension = Unicode( | |
25 |
|
24 | 'md', config=True, | ||
26 | #Call base class constructor. |
|
25 | help="Extension of the file that should be written to disk") | |
27 | super(exporter.Exporter, self).__init__(preprocessors, jinja_filters, config, **kw) |
|
|||
28 |
|
26 | |||
29 | #Set defaults |
|
27 | template_file = Unicode( | |
30 | self.file_extension = "md" |
|
28 | 'markdown', config=True, | |
31 | self.extract_figure_transformer.enabled = True |
|
29 | help="Name of the template file to use") | |
32 | self.template_file = "markdown" |
|
@@ -15,20 +15,26 b'' | |||||
15 |
|
15 | |||
16 | # local import |
|
16 | # local import | |
17 | import exporter |
|
17 | import exporter | |
|
18 | from IPython.utils.traitlets import Unicode | |||
18 |
|
19 | |||
19 | #----------------------------------------------------------------------------- |
|
20 | #----------------------------------------------------------------------------- | |
20 | # Classes |
|
21 | # Classes | |
21 | #----------------------------------------------------------------------------- |
|
22 | #----------------------------------------------------------------------------- | |
22 | class PythonExporter(exporter.Exporter): |
|
23 | class PythonExporter(exporter.Exporter): | |
23 |
|
24 | |||
|
25 | file_extension = Unicode( | |||
|
26 | 'py', config=True, | |||
|
27 | help="Extension of the file that should be written to disk") | |||
|
28 | ||||
|
29 | template_file = Unicode( | |||
|
30 | 'python', config=True, | |||
|
31 | help="Name of the template file to use") | |||
|
32 | ||||
24 | def __init__(self, preprocessors=None, jinja_filters=None, config=None, armor=False, **kw): |
|
33 | def __init__(self, preprocessors=None, jinja_filters=None, config=None, armor=False, **kw): | |
25 |
|
34 | |||
26 | #Call base class constructor. |
|
35 | #Call base class constructor. | |
27 | super(exporter.Exporter, self).__init__(preprocessors, jinja_filters, config, **kw) |
|
36 | super(exporter.Exporter, self).__init__(preprocessors, jinja_filters, config, **kw) | |
28 |
|
37 | |||
29 | #Set defaults |
|
38 | #Set defaults | |
30 | self.file_extension = "py" |
|
39 | self.extract_figure_transformer.enabled = False | |
31 |
|
|
40 | ||
32 | self.template_file = "python-armor" |
|
|||
33 | else: |
|
|||
34 | self.template_file = "python" |
|
@@ -1,4 +1,4 b'' | |||||
1 |
|
|
1 | """TODO: Docstring | |
2 | """ |
|
2 | """ | |
3 |
|
3 | |||
4 | #----------------------------------------------------------------------------- |
|
4 | #----------------------------------------------------------------------------- | |
@@ -16,21 +16,21 b'' | |||||
16 | # local import |
|
16 | # local import | |
17 | import html_exporter |
|
17 | import html_exporter | |
18 | import transformers.revealhelp |
|
18 | import transformers.revealhelp | |
|
19 | from IPython.utils.traitlets import Unicode | |||
19 |
|
20 | |||
20 | #----------------------------------------------------------------------------- |
|
21 | #----------------------------------------------------------------------------- | |
21 | # Classes |
|
22 | # Classes | |
22 | #----------------------------------------------------------------------------- |
|
23 | #----------------------------------------------------------------------------- | |
23 | class RevealExporter(html_exporter.HtmlExporter): |
|
24 | class RevealExporter(html_exporter.HtmlExporter): | |
24 |
|
25 | |||
25 | def __init__(self, preprocessors=None, jinja_filters=None, config=None, **kw): |
|
26 | file_extension = Unicode( | |
26 |
|
27 | 'reveal.html', config=True, | ||
27 | #Call base class constructor. |
|
28 | help="Extension of the file that should be written to disk") | |
28 | super(html_exporter.HtmlExporter, self).__init__(preprocessors, jinja_filters, config, **kw) |
|
|||
29 |
|
||||
30 | #Set defaults |
|
|||
31 | self.file_extension = "reveal.html" |
|
|||
32 | self.template_file = "reveal" |
|
|||
33 |
|
29 | |||
|
30 | template_file = Unicode( | |||
|
31 | 'reveal', config=True, | |||
|
32 | help="Name of the template file to use") | |||
|
33 | ||||
34 | def _register_transformers(self): |
|
34 | def _register_transformers(self): | |
35 |
|
35 | |||
36 | #Register the transformers of the base class. |
|
36 | #Register the transformers of the base class. |
@@ -1,4 +1,4 b'' | |||||
1 |
|
|
1 | """TODO: Docstring | |
2 | """ |
|
2 | """ | |
3 |
|
3 | |||
4 | #----------------------------------------------------------------------------- |
|
4 | #----------------------------------------------------------------------------- | |
@@ -15,23 +15,21 b'' | |||||
15 |
|
15 | |||
16 | # local import |
|
16 | # local import | |
17 | import exporter |
|
17 | import exporter | |
|
18 | from IPython.utils.traitlets import Unicode | |||
18 |
|
19 | |||
19 | #----------------------------------------------------------------------------- |
|
20 | #----------------------------------------------------------------------------- | |
20 | # Classes |
|
21 | # Classes | |
21 | #----------------------------------------------------------------------------- |
|
22 | #----------------------------------------------------------------------------- | |
22 | class RstExporter(exporter.Exporter): |
|
23 | class RstExporter(exporter.Exporter): | |
23 |
|
24 | |||
24 | def __init__(self, preprocessors=None, jinja_filters=None, config=None, **kw): |
|
25 | file_extension = Unicode( | |
25 |
|
26 | 'rst', config=True, | ||
26 | #Call base class constructor. |
|
27 | help="Extension of the file that should be written to disk") | |
27 | super(exporter.Exporter, self).__init__(preprocessors, jinja_filters, config, **kw) |
|
|||
28 |
|
||||
29 | #Set defaults |
|
|||
30 | self.file_extension = "rst" |
|
|||
31 | self.template_file = "rst" |
|
|||
32 | self.extract_figure_transformer.enabled = True |
|
|||
33 |
|
||||
34 |
|
28 | |||
|
29 | template_file = Unicode( | |||
|
30 | 'rst', config=True, | |||
|
31 | help="Name of the template file to use") | |||
|
32 | ||||
35 | def _register_filters(self): |
|
33 | def _register_filters(self): | |
36 |
|
34 | |||
37 | #Register the filters of the base class. |
|
35 | #Register the filters of the base class. |
@@ -1,5 +1,3 b'' | |||||
1 | """TODO: Docstring |
|
|||
2 | """ |
|
|||
3 |
|
|
1 | ||
4 | #----------------------------------------------------------------------------- |
|
2 | #----------------------------------------------------------------------------- | |
5 | # Copyright (c) 2013, the IPython Development Team. |
|
3 | # Copyright (c) 2013, the IPython Development Team. | |
@@ -14,35 +12,23 b'' | |||||
14 | #----------------------------------------------------------------------------- |
|
12 | #----------------------------------------------------------------------------- | |
15 |
|
13 | |||
16 | # local import |
|
14 | # local import | |
17 |
import latex |
|
15 | import latex | |
18 |
|
16 | from IPython.utils.traitlets import Unicode | ||
|
17 | from transformers.sphinx import SphinxTransformer | |||
19 | #----------------------------------------------------------------------------- |
|
18 | #----------------------------------------------------------------------------- | |
20 | # Classes |
|
19 | # Classes | |
21 | #----------------------------------------------------------------------------- |
|
20 | #----------------------------------------------------------------------------- | |
22 |
class SphinxExporter(latex |
|
21 | class SphinxExporter(latex.LatexExporter): | |
23 |
|
||||
24 | def __init__(self, preprocessors=None, jinja_filters=None, config=None, sphinx_type="howto", **kw): |
|
|||
25 |
|
||||
26 | #Call base class constructor. |
|
|||
27 | super(latex_exporter.LatexExporter, self).__init__(preprocessors, jinja_filters, config, **kw) |
|
|||
28 |
|
||||
29 | #Defaults |
|
|||
30 | self.template_file = "latex_sphinx_" + sphinx_type |
|
|||
31 |
|
||||
32 | def _register_filters(self): |
|
|||
33 |
|
||||
34 | #Register the filters of the base class. |
|
|||
35 | super(latex_exporter.LatexExporter, self)._register_filters() |
|
|||
36 |
|
||||
37 | #Add latex filters to the Jinja2 environment |
|
|||
38 | #self.register_filter('escape_tex', filters.latex.escape_tex) |
|
|||
39 |
|
22 | |||
|
23 | template_file = Unicode( | |||
|
24 | 'sphinxhowto', config=True, | |||
|
25 | help="Name of the template file to use") | |||
40 |
|
26 | |||
41 | def _register_transformers(self): |
|
27 | def _register_transformers(self): | |
42 |
|
28 | |||
43 | #Register the transformers of the base class. |
|
29 | #Register the transformers of the base class. | |
44 |
super(latex |
|
30 | super(latex.LatexExporter, self)._register_transformers() | |
45 |
|
31 | |||
46 | #Register latex transformer |
|
32 | #Register sphinx latex transformer | |
47 |
|
|
33 | self.register_transformer(SphinxTransformer) | |
48 |
|
34 |
@@ -1,33 +1,10 b'' | |||||
1 | # ANSI color functions: |
|
|||
2 |
|
|
1 | import re | |
3 | def remove_ansi(src): |
|
|||
4 | """Strip all ANSI color escape sequences from input string. |
|
|||
5 |
|
||||
6 | Parameters |
|
|||
7 | ---------- |
|
|||
8 | src : string |
|
|||
9 |
|
2 | |||
10 | Returns |
|
3 | def remove_ansi(src): | |
11 | ------- |
|
|||
12 | string |
|
|||
13 | """ |
|
|||
14 | return re.sub(r'\033\[(0|\d;\d\d)m', '', src) |
|
4 | return re.sub(r'\033\[(0|\d;\d\d)m', '', src) | |
15 |
|
5 | |||
16 |
|
6 | |||
17 | def ansi2html(txt): |
|
7 | def ansi2html(txt): | |
18 | """Render ANSI colors as HTML colors |
|
|||
19 |
|
||||
20 | This is equivalent to util.fixConsole in utils.js |
|
|||
21 |
|
||||
22 | Parameters |
|
|||
23 | ---------- |
|
|||
24 | txt : string |
|
|||
25 |
|
||||
26 | Returns |
|
|||
27 | ------- |
|
|||
28 | string |
|
|||
29 | """ |
|
|||
30 |
|
||||
31 | ansi_colormap = { |
|
8 | ansi_colormap = { | |
32 | '30': 'ansiblack', |
|
9 | '30': 'ansiblack', | |
33 | '31': 'ansired', |
|
10 | '31': 'ansired', | |
@@ -49,6 +26,7 b' def ansi2html(txt):' | |||||
49 | '"': '"', |
|
26 | '"': '"', | |
50 | '`': '`', |
|
27 | '`': '`', | |
51 | } |
|
28 | } | |
|
29 | ||||
52 | for c, escape in html_escapes.iteritems(): |
|
30 | for c, escape in html_escapes.iteritems(): | |
53 | txt = txt.replace(c, escape) |
|
31 | txt = txt.replace(c, escape) | |
54 |
|
32 | |||
@@ -61,6 +39,7 b' def ansi2html(txt):' | |||||
61 | while m: |
|
39 | while m: | |
62 | cmds = m.groups()[0].split(';') |
|
40 | cmds = m.groups()[0].split(';') | |
63 | closer = '</span>' if opened else '' |
|
41 | closer = '</span>' if opened else '' | |
|
42 | ||||
64 | # True if there is there more than one element in cmds, *or* |
|
43 | # True if there is there more than one element in cmds, *or* | |
65 | # if there is only one but it is not equal to a string of zeroes. |
|
44 | # if there is only one but it is not equal to a string of zeroes. | |
66 | opened = len(cmds) > 1 or cmds[0] != '0' * len(cmds[0]) |
|
45 | opened = len(cmds) > 1 or cmds[0] != '0' * len(cmds[0]) | |
@@ -79,4 +58,4 b' def ansi2html(txt):' | |||||
79 |
|
58 | |||
80 | if opened: |
|
59 | if opened: | |
81 | txt += '</span>' |
|
60 | txt += '</span>' | |
82 |
return txt |
|
61 | return txt No newline at end of file |
@@ -1,4 +1,4 b'' | |||||
1 | """Filter used to select the first prefered 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 | |
@@ -16,11 +16,20 b' GlobalConfigurable.display_data_priority' | |||||
16 | #----------------------------------------------------------------------------- |
|
16 | #----------------------------------------------------------------------------- | |
17 | # Classes and functions |
|
17 | # Classes and functions | |
18 | #----------------------------------------------------------------------------- |
|
18 | #----------------------------------------------------------------------------- | |
19 |
class DataTypeFilter( |
|
19 | class DataTypeFilter(object): | |
20 | """ Returns the prefered display format """ |
|
20 | """ Returns the preferred display format """ | |
21 |
|
21 | |||
22 | def __init__(self, config=None, **kw): |
|
22 | display_data_priority = None | |
23 | super(DataTypeFilter, self).__init__(config=config, **kw) |
|
23 | ||
|
24 | def __init__(self, display_data_priority): | |||
|
25 | super(object, self).__init__() | |||
|
26 | ||||
|
27 | #Make sure that the display data priority variably is not None. | |||
|
28 | if self.display_data_priority is None: | |||
|
29 | raise TypeError | |||
|
30 | else: | |||
|
31 | self.display_data_priority = display_data_priority | |||
|
32 | ||||
24 |
|
33 | |||
25 | def __call__(self, output): |
|
34 | def __call__(self, output): | |
26 | """ Return the first available format in the priority """ |
|
35 | """ Return the first available format in the priority """ |
@@ -33,8 +33,6 b' LATEX_SUBS = (' | |||||
33 | #----------------------------------------------------------------------------- |
|
33 | #----------------------------------------------------------------------------- | |
34 | # Functions |
|
34 | # Functions | |
35 | #----------------------------------------------------------------------------- |
|
35 | #----------------------------------------------------------------------------- | |
36 |
|
||||
37 | #TODO: Comment me. |
|
|||
38 | def escape_tex(value): |
|
36 | def escape_tex(value): | |
39 | newval = value |
|
37 | newval = value | |
40 | for pattern, replacement in LATEX_SUBS: |
|
38 | for pattern, replacement in LATEX_SUBS: |
@@ -1,6 +1,6 b'' | |||||
1 | """String utilities. |
|
1 | """String utilities. | |
2 |
|
2 | |||
3 |
Contains a collection of useful |
|
3 | Contains a collection of useful string manipulations functions. | |
4 | """ |
|
4 | """ | |
5 | #----------------------------------------------------------------------------- |
|
5 | #----------------------------------------------------------------------------- | |
6 | # Copyright (c) 2013, the IPython Development Team. |
|
6 | # Copyright (c) 2013, the IPython Development Team. |
1 | NO CONTENT: file renamed from nbconvert/templates/latex/latex_base.tplx to nbconvert/templates/latex/base.tplx |
|
NO CONTENT: file renamed from nbconvert/templates/latex/latex_base.tplx to nbconvert/templates/latex/base.tplx |
1 | NO CONTENT: file renamed from nbconvert/templates/latex/latex_sphinx_base.tplx to nbconvert/templates/latex/sphinx_base.tplx |
|
NO CONTENT: file renamed from nbconvert/templates/latex/latex_sphinx_base.tplx to nbconvert/templates/latex/sphinx_base.tplx |
1 | NO CONTENT: file renamed from nbconvert/templates/latex/latex_sphinx_howto.tplx to nbconvert/templates/latex/sphinx_howto.tplx |
|
NO CONTENT: file renamed from nbconvert/templates/latex/latex_sphinx_howto.tplx to nbconvert/templates/latex/sphinx_howto.tplx |
1 | NO CONTENT: file renamed from nbconvert/templates/latex/latex_sphinx_manual.tplx to nbconvert/templates/latex/sphinx_manual.tplx |
|
NO CONTENT: file renamed from nbconvert/templates/latex/latex_sphinx_manual.tplx to nbconvert/templates/latex/sphinx_manual.tplx |
@@ -1,7 +1,7 b'' | |||||
1 |
from |
|
1 | from .base import ConfigurableTransformer | |
|
2 | from IPython.utils.traitlets import (Bool) | |||
2 |
|
3 | |||
3 |
|
4 | class ActivatableTransformer(ConfigurableTransformer): | ||
4 | class ActivatableTransformer(ConfigurableTransformers): |
|
|||
5 | """A simple ConfigurableTransformers that have an enabled flag |
|
5 | """A simple ConfigurableTransformers that have an enabled flag | |
6 |
|
6 | |||
7 | Inherit from that if you just want to have a transformer which is |
|
7 | Inherit from that if you just want to have a transformer which is |
@@ -9,11 +9,8 b' as well as decorator to simplify tasks.' | |||||
9 | from __future__ import print_function, absolute_import |
|
9 | from __future__ import print_function, absolute_import | |
10 |
|
10 | |||
11 | from IPython.config.configurable import Configurable |
|
11 | from IPython.config.configurable import Configurable | |
12 | from IPython.utils.traitlets import Unicode, Bool, Dict, List |
|
|||
13 |
|
12 | |||
14 | from .config import GlobalConfigurable |
|
13 | class ConfigurableTransformer(Configurable): | |
15 |
|
||||
16 | class ConfigurableTransformers(GlobalConfigurable): |
|
|||
17 | """ A configurable transformer |
|
14 | """ A configurable transformer | |
18 |
|
15 | |||
19 | Inherit from this class if you wish to have configurability for your |
|
16 | Inherit from this class if you wish to have configurability for your | |
@@ -29,7 +26,7 b' class ConfigurableTransformers(GlobalConfigurable):' | |||||
29 | """ |
|
26 | """ | |
30 |
|
27 | |||
31 | def __init__(self, config=None, **kw): |
|
28 | def __init__(self, config=None, **kw): | |
32 |
super(ConfigurableTransformer |
|
29 | super(ConfigurableTransformer, self).__init__(config=config, **kw) | |
33 |
|
30 | |||
34 | def __call__(self, nb, other): |
|
31 | def __call__(self, nb, other): | |
35 | """transformation to apply on each notebook. |
|
32 | """transformation to apply on each notebook. | |
@@ -60,62 +57,3 b' class ConfigurableTransformers(GlobalConfigurable):' | |||||
60 |
|
57 | |||
61 | raise NotImplementedError('should be implemented by subclass') |
|
58 | raise NotImplementedError('should be implemented by subclass') | |
62 | return cell, other |
|
59 | return cell, other | |
63 |
|
||||
64 | def cell_preprocessor(function): |
|
|||
65 | """ wrap a function to be executed on all cells of a notebook |
|
|||
66 |
|
||||
67 | wrapped function parameters : |
|
|||
68 | cell : the cell |
|
|||
69 | other : external resources |
|
|||
70 | index : index of the cell |
|
|||
71 | """ |
|
|||
72 | def wrappedfunc(nb, other): |
|
|||
73 | for worksheet in nb.worksheets : |
|
|||
74 | for index, cell in enumerate(worksheet.cells): |
|
|||
75 | worksheet.cells[index], other = function(cell, other, index) |
|
|||
76 | return nb, other |
|
|||
77 | return wrappedfunc |
|
|||
78 |
|
||||
79 |
|
||||
80 | @cell_preprocessor |
|
|||
81 | def haspyout_transformer(cell, other, count): |
|
|||
82 | """ |
|
|||
83 | Add a haspyout flag to cell that have it |
|
|||
84 |
|
||||
85 | Easier for templating, where you can't know in advance |
|
|||
86 | wether to write the out prompt |
|
|||
87 |
|
||||
88 | """ |
|
|||
89 | cell.type = cell.cell_type |
|
|||
90 | cell.haspyout = False |
|
|||
91 | for out in cell.get('outputs', []): |
|
|||
92 | if out.output_type == 'pyout': |
|
|||
93 | cell.haspyout = True |
|
|||
94 | break |
|
|||
95 | return cell, other |
|
|||
96 |
|
||||
97 | @cell_preprocessor |
|
|||
98 | def coalesce_streams(cell, other, count): |
|
|||
99 | """merge consecutive sequences of stream output into single stream |
|
|||
100 |
|
||||
101 | to prevent extra newlines inserted at flush calls |
|
|||
102 |
|
||||
103 | TODO: handle \r deletion |
|
|||
104 | """ |
|
|||
105 | outputs = cell.get('outputs', []) |
|
|||
106 | if not outputs: |
|
|||
107 | return cell, other |
|
|||
108 | new_outputs = [] |
|
|||
109 | last = outputs[0] |
|
|||
110 | new_outputs = [last] |
|
|||
111 | for output in outputs[1:]: |
|
|||
112 | if (output.output_type == 'stream' and |
|
|||
113 | last.output_type == 'stream' and |
|
|||
114 | last.stream == output.stream |
|
|||
115 | ): |
|
|||
116 | last.text += output.text |
|
|||
117 | else: |
|
|||
118 | new_outputs.append(output) |
|
|||
119 |
|
||||
120 | cell.outputs = new_outputs |
|
|||
121 | return cell, other |
|
@@ -26,7 +26,6 b' TODO: handle \\r deletion' | |||||
26 | outputs = cell.get('outputs', []) |
|
26 | outputs = cell.get('outputs', []) | |
27 | if not outputs: |
|
27 | if not outputs: | |
28 | return cell, other |
|
28 | return cell, other | |
29 | new_outputs = [] |
|
|||
30 | last = outputs[0] |
|
29 | last = outputs[0] | |
31 | new_outputs = [last] |
|
30 | new_outputs = [last] | |
32 | for output in outputs[1:]: |
|
31 | for output in outputs[1:]: |
@@ -1,4 +1,4 b'' | |||||
1 |
|
1 | from .activatable import ActivatableTransformer | ||
2 |
|
2 | |||
3 | class CSSHtmlHeaderTransformer(ActivatableTransformer): |
|
3 | class CSSHtmlHeaderTransformer(ActivatableTransformer): | |
4 |
|
4 | |||
@@ -31,7 +31,6 b' class CSSHtmlHeaderTransformer(ActivatableTransformer):' | |||||
31 | static = os.path.join(path.get_ipython_package_dir(), |
|
31 | static = os.path.join(path.get_ipython_package_dir(), | |
32 | 'frontend', 'html', 'notebook', 'static', |
|
32 | 'frontend', 'html', 'notebook', 'static', | |
33 | ) |
|
33 | ) | |
34 | here = os.path.split(os.path.realpath(__file__))[0] |
|
|||
35 | css = os.path.join(static, 'css') |
|
34 | css = os.path.join(static, 'css') | |
36 | for sheet in [ |
|
35 | for sheet in [ | |
37 | # do we need jquery and prettify? |
|
36 | # do we need jquery and prettify? |
@@ -1,4 +1,5 b'' | |||||
1 |
|
1 | from IPython.utils.traitlets import (Dict, List, Unicode) | ||
|
2 | from .activatable import ActivatableTransformer | |||
2 |
|
3 | |||
3 | class ExtractFigureTransformer(ActivatableTransformer): |
|
4 | class ExtractFigureTransformer(ActivatableTransformer): | |
4 |
|
5 | |||
@@ -18,6 +19,7 b' class ExtractFigureTransformer(ActivatableTransformer):' | |||||
18 | config=True, |
|
19 | config=True, | |
19 | ) |
|
20 | ) | |
20 |
|
21 | |||
|
22 | display_data_priority = List(['svg', 'png', 'latex', 'jpg', 'jpeg','text']) | |||
21 |
|
23 | |||
22 | #to do change this to .format {} syntax |
|
24 | #to do change this to .format {} syntax | |
23 | default_key_tpl = Unicode('_fig_{count:02d}.{ext}', config=True) |
|
25 | default_key_tpl = Unicode('_fig_{count:02d}.{ext}', config=True) |
@@ -18,7 +18,8 b' from __future__ import print_function, absolute_import' | |||||
18 |
|
18 | |||
19 | # Our own imports |
|
19 | # Our own imports | |
20 | # Needed to override transformer |
|
20 | # Needed to override transformer | |
21 |
from . |
|
21 | from .activatable import (ActivatableTransformer) | |
|
22 | from filters import latex | |||
22 |
|
23 | |||
23 | #----------------------------------------------------------------------------- |
|
24 | #----------------------------------------------------------------------------- | |
24 | # Classes |
|
25 | # Classes | |
@@ -38,5 +39,5 b' class LatexTransformer(ActivatableTransformer):' | |||||
38 | """ |
|
39 | """ | |
39 |
|
40 | |||
40 | if hasattr(cell, "source") and cell.cell_type == "markdown": |
|
41 | if hasattr(cell, "source") and cell.cell_type == "markdown": | |
41 | cell.source = rm_math_space(cell.source) |
|
42 | cell.source = latex.rm_math_space(cell.source) | |
42 | return cell, other |
|
43 | return cell, other |
@@ -1,6 +1,6 b'' | |||||
1 |
|
1 | from .base import ConfigurableTransformer | ||
2 |
|
2 | |||
3 |
class RevealHelpTransformer(ConfigurableTransformer |
|
3 | class RevealHelpTransformer(ConfigurableTransformer): | |
4 |
|
4 | |||
5 | def __call__(self, nb, other): |
|
5 | def __call__(self, nb, other): | |
6 | for worksheet in nb.worksheets : |
|
6 | for worksheet in nb.worksheets : |
@@ -34,7 +34,7 b' from pygments.formatters import LatexFormatter' | |||||
34 | from IPython.utils.traitlets import Unicode, Bool |
|
34 | from IPython.utils.traitlets import Unicode, Bool | |
35 |
|
35 | |||
36 | # Needed to override transformer |
|
36 | # Needed to override transformer | |
37 |
from . |
|
37 | from .activatable import (ActivatableTransformer) #TODO | |
38 |
|
38 | |||
39 | #----------------------------------------------------------------------------- |
|
39 | #----------------------------------------------------------------------------- | |
40 | # Classes and functions |
|
40 | # Classes and functions |
1 | NO CONTENT: file was removed |
|
NO CONTENT: file was removed | ||
This diff has been collapsed as it changes many lines, (683 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, (542 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 |
1 | NO CONTENT: file was removed |
|
NO CONTENT: file was removed |
General Comments 0
You need to be logged in to leave comments.
Login now