Show More
@@ -0,0 +1,5 b'' | |||
|
1 | """Utilities for converting notebooks to and from different formats.""" | |
|
2 | ||
|
3 | from .exporters import * | |
|
4 | import filters | |
|
5 | import transformers |
@@ -0,0 +1,10 b'' | |||
|
1 | from .basichtml import BasicHtmlExporter | |
|
2 | from .export import * | |
|
3 | from .exporter import Exporter | |
|
4 | from .fullhtml import FullHtmlExporter | |
|
5 | from .latex import LatexExporter | |
|
6 | from .markdown import MarkdownExporter | |
|
7 | from .python import PythonExporter | |
|
8 | from .rst import RstExporter | |
|
9 | from .sphinx_howto import SphinxHowtoExporter | |
|
10 | from .sphinx_manual import SphinxManualExporter |
@@ -0,0 +1,55 b'' | |||
|
1 | """ | |
|
2 | Exporter that exports Basic HTML. | |
|
3 | """ | |
|
4 | ||
|
5 | #----------------------------------------------------------------------------- | |
|
6 | # Copyright (c) 2013, the IPython Development Team. | |
|
7 | # | |
|
8 | # Distributed under the terms of the Modified BSD License. | |
|
9 | # | |
|
10 | # The full license is in the file COPYING.txt, distributed with this software. | |
|
11 | #----------------------------------------------------------------------------- | |
|
12 | ||
|
13 | #----------------------------------------------------------------------------- | |
|
14 | # Imports | |
|
15 | #----------------------------------------------------------------------------- | |
|
16 | ||
|
17 | from IPython.utils.traitlets import Unicode | |
|
18 | ||
|
19 | from ..transformers.csshtmlheader import CSSHtmlHeaderTransformer | |
|
20 | ||
|
21 | from .exporter import Exporter | |
|
22 | ||
|
23 | #----------------------------------------------------------------------------- | |
|
24 | # Classes | |
|
25 | #----------------------------------------------------------------------------- | |
|
26 | ||
|
27 | class BasicHtmlExporter(Exporter): | |
|
28 | """ | |
|
29 | Exports a basic HTML document. This exporter assists with the export of | |
|
30 | HTML. Inherit from it if you are writing your own HTML template and need | |
|
31 | custom transformers/filters. If you don't need custom transformers/ | |
|
32 | filters, just change the 'template_file' config option. | |
|
33 | """ | |
|
34 | ||
|
35 | file_extension = Unicode( | |
|
36 | 'html', config=True, | |
|
37 | help="Extension of the file that should be written to disk" | |
|
38 | ) | |
|
39 | ||
|
40 | template_file = Unicode( | |
|
41 | 'basichtml', config=True, | |
|
42 | help="Name of the template file to use") | |
|
43 | ||
|
44 | ||
|
45 | def _register_transformers(self): | |
|
46 | """ | |
|
47 | Register all of the transformers needed for this exporter. | |
|
48 | """ | |
|
49 | ||
|
50 | #Register the transformers of the base class. | |
|
51 | super(BasicHtmlExporter, self)._register_transformers() | |
|
52 | ||
|
53 | #Register CSSHtmlHeaderTransformer transformer | |
|
54 | self.register_transformer(CSSHtmlHeaderTransformer) | |
|
55 |
@@ -0,0 +1,225 b'' | |||
|
1 | """ | |
|
2 | Module containing single call export functions. | |
|
3 | """ | |
|
4 | #----------------------------------------------------------------------------- | |
|
5 | # Copyright (c) 2013, the IPython Development Team. | |
|
6 | # | |
|
7 | # Distributed under the terms of the Modified BSD License. | |
|
8 | # | |
|
9 | # The full license is in the file COPYING.txt, distributed with this software. | |
|
10 | #----------------------------------------------------------------------------- | |
|
11 | ||
|
12 | #----------------------------------------------------------------------------- | |
|
13 | # Imports | |
|
14 | #----------------------------------------------------------------------------- | |
|
15 | ||
|
16 | from functools import wraps | |
|
17 | ||
|
18 | from IPython.nbformat.v3.nbbase import NotebookNode | |
|
19 | ||
|
20 | from .exporter import Exporter | |
|
21 | from .basichtml import BasicHtmlExporter | |
|
22 | from .fullhtml import FullHtmlExporter | |
|
23 | from .latex import LatexExporter | |
|
24 | from .markdown import MarkdownExporter | |
|
25 | from .python import PythonExporter | |
|
26 | from .python_armor import PythonArmorExporter | |
|
27 | from .reveal import RevealExporter | |
|
28 | from .rst import RstExporter | |
|
29 | from .sphinx_howto import SphinxHowtoExporter | |
|
30 | from .sphinx_manual import SphinxManualExporter | |
|
31 | ||
|
32 | #----------------------------------------------------------------------------- | |
|
33 | # Classes | |
|
34 | #----------------------------------------------------------------------------- | |
|
35 | ||
|
36 | def DocDecorator(f): | |
|
37 | ||
|
38 | #Set docstring of function | |
|
39 | f.__doc__ = f.__doc__ + """ | |
|
40 | nb : Notebook node | |
|
41 | config : config | |
|
42 | User configuration instance. | |
|
43 | transformers : list[of transformer] | |
|
44 | Custom transformers to apply to the notebook prior to engaging | |
|
45 | the Jinja template engine. Any transformers specified here | |
|
46 | will override existing transformers if a naming conflict | |
|
47 | occurs. | |
|
48 | filters : list[of filter] | |
|
49 | Custom filters to make accessible to the Jinja templates. Any | |
|
50 | filters specified here will override existing filters if a | |
|
51 | naming conflict occurs. | |
|
52 | ||
|
53 | Returns | |
|
54 | ---------- | |
|
55 | tuple- output, resources, exporter_instance | |
|
56 | output : str | |
|
57 | Jinja 2 output. This is the resulting converted notebook. | |
|
58 | resources : dictionary | |
|
59 | Dictionary of resources used prior to and during the conversion | |
|
60 | process. | |
|
61 | exporter_instance : Exporter | |
|
62 | Instance of the Exporter class used to export the document. Useful | |
|
63 | to caller because it provides a 'file_extension' property which | |
|
64 | specifies what extension the output should be saved as.""" | |
|
65 | ||
|
66 | @wraps(f) | |
|
67 | def decorator(*args, **kwargs): | |
|
68 | return f(*args, **kwargs) | |
|
69 | ||
|
70 | return decorator | |
|
71 | ||
|
72 | ||
|
73 | #----------------------------------------------------------------------------- | |
|
74 | # Functions | |
|
75 | #----------------------------------------------------------------------------- | |
|
76 | ||
|
77 | __all__ = [ | |
|
78 | 'export', | |
|
79 | 'export_sphinx_manual', | |
|
80 | 'export_sphinx_howto', | |
|
81 | 'export_basic_html', | |
|
82 | 'export_full_html', | |
|
83 | 'export_latex', | |
|
84 | 'export_markdown', | |
|
85 | 'export_python', | |
|
86 | 'export_python_armor', | |
|
87 | 'export_reveal', | |
|
88 | 'export_rst', | |
|
89 | 'export_by_name' | |
|
90 | ] | |
|
91 | ||
|
92 | @DocDecorator | |
|
93 | def export(exporter_type, nb, config=None, transformers=None, filters=None): | |
|
94 | """ | |
|
95 | Export a notebook object using specific exporter class. | |
|
96 | ||
|
97 | exporter_type : Exporter class type | |
|
98 | Class type of the exporter that should be used. This method | |
|
99 | will initialize it's own instance of the class. It is | |
|
100 | ASSUMED that the class type provided exposes a | |
|
101 | constructor (__init__) with the same signature as the | |
|
102 | base Exporter class.} | |
|
103 | """ | |
|
104 | ||
|
105 | #Check arguments | |
|
106 | if exporter_type is None: | |
|
107 | raise TypeError("Exporter is None") | |
|
108 | elif not issubclass(exporter_type, Exporter): | |
|
109 | raise TypeError("Exporter type does not inherit from Exporter (base)") | |
|
110 | ||
|
111 | if nb is None: | |
|
112 | raise TypeError("nb is None") | |
|
113 | ||
|
114 | #Create the exporter | |
|
115 | exporter_instance = exporter_type(preprocessors=transformers, | |
|
116 | jinja_filters=filters, config=config) | |
|
117 | ||
|
118 | #Try to convert the notebook using the appropriate conversion function. | |
|
119 | if isinstance(nb, NotebookNode): | |
|
120 | output, resources = exporter_instance.from_notebook_node(nb) | |
|
121 | elif isinstance(nb, basestring): | |
|
122 | output, resources = exporter_instance.from_filename(nb) | |
|
123 | else: | |
|
124 | output, resources = exporter_instance.from_file(nb) | |
|
125 | return output, resources, exporter_instance | |
|
126 | ||
|
127 | ||
|
128 | @DocDecorator | |
|
129 | def export_sphinx_manual(nb, config=None, transformers=None, filters=None): | |
|
130 | """ | |
|
131 | Export a notebook object to Sphinx Manual LaTeX | |
|
132 | """ | |
|
133 | return export(SphinxManualExporter, nb, config, transformers, filters) | |
|
134 | ||
|
135 | ||
|
136 | @DocDecorator | |
|
137 | def export_sphinx_howto(nb, config=None, transformers=None, filters=None): | |
|
138 | """ | |
|
139 | Export a notebook object to Sphinx HowTo LaTeX | |
|
140 | """ | |
|
141 | return export(SphinxHowtoExporter, nb, config, transformers, filters) | |
|
142 | ||
|
143 | ||
|
144 | @DocDecorator | |
|
145 | def export_basic_html(nb, config=None, transformers=None, filters=None): | |
|
146 | """ | |
|
147 | Export a notebook object to Basic HTML | |
|
148 | """ | |
|
149 | return export(BasicHtmlExporter, nb, config, transformers, filters) | |
|
150 | ||
|
151 | ||
|
152 | @DocDecorator | |
|
153 | def export_full_html(nb, config=None, transformers=None, filters=None): | |
|
154 | """ | |
|
155 | Export a notebook object to Full HTML | |
|
156 | """ | |
|
157 | return export(FullHtmlExporter, nb, config, transformers, filters) | |
|
158 | ||
|
159 | ||
|
160 | @DocDecorator | |
|
161 | def export_latex(nb, config=None, transformers=None, filters=None): | |
|
162 | """ | |
|
163 | Export a notebook object to LaTeX | |
|
164 | """ | |
|
165 | return export(LatexExporter, nb, config, transformers, filters) | |
|
166 | ||
|
167 | ||
|
168 | @DocDecorator | |
|
169 | def export_markdown(nb, config=None, transformers=None, filters=None): | |
|
170 | """ | |
|
171 | Export a notebook object to Markdown | |
|
172 | """ | |
|
173 | return export(MarkdownExporter, nb, config, transformers, filters) | |
|
174 | ||
|
175 | ||
|
176 | @DocDecorator | |
|
177 | def export_python(nb, config=None, transformers=None, filters=None): | |
|
178 | """ | |
|
179 | Export a notebook object to Python | |
|
180 | """ | |
|
181 | return export(PythonExporter, nb, config, transformers, filters) | |
|
182 | ||
|
183 | ||
|
184 | @DocDecorator | |
|
185 | def export_python_armor(nb, config=None, transformers=None, filters=None): | |
|
186 | """ | |
|
187 | Export a notebook object to Python (Armor) | |
|
188 | """ | |
|
189 | return export(PythonArmorExporter, nb, config, transformers, filters) | |
|
190 | ||
|
191 | ||
|
192 | @DocDecorator | |
|
193 | def export_reveal(nb, config=None, transformers=None, filters=None): | |
|
194 | """ | |
|
195 | Export a notebook object to Reveal | |
|
196 | """ | |
|
197 | return export(RevealExporter, nb, config, transformers, filters) | |
|
198 | ||
|
199 | ||
|
200 | @DocDecorator | |
|
201 | def export_rst(nb, config=None, transformers=None, filters=None): | |
|
202 | """ | |
|
203 | Export a notebook object to RST | |
|
204 | """ | |
|
205 | return export(RstExporter, nb, config, transformers, filters) | |
|
206 | ||
|
207 | ||
|
208 | @DocDecorator | |
|
209 | def export_by_name(template_name, nb, config=None, transformers=None, filters=None): | |
|
210 | """ | |
|
211 | Export a notebook object to a template type by its name. Reflection | |
|
212 | (Inspect) is used to find the template's corresponding explicit export | |
|
213 | method defined in this module. That method is then called directly. | |
|
214 | ||
|
215 | template_name : str | |
|
216 | Name of the template style to export to. | |
|
217 | """ | |
|
218 | ||
|
219 | function_name = "export_" + template_name.lower() | |
|
220 | ||
|
221 | if function_name in globals(): | |
|
222 | return globals()[function_name](nb, config, transformers, filters) | |
|
223 | else: | |
|
224 | return None | |
|
225 |
@@ -0,0 +1,341 b'' | |||
|
1 | """This module defines Exporter, a highly configurable converter | |
|
2 | that uses Jinja2 to export notebook files into different formats. | |
|
3 | """ | |
|
4 | ||
|
5 | #----------------------------------------------------------------------------- | |
|
6 | # Copyright (c) 2013, the IPython Development Team. | |
|
7 | # | |
|
8 | # Distributed under the terms of the Modified BSD License. | |
|
9 | # | |
|
10 | # The full license is in the file COPYING.txt, distributed with this software. | |
|
11 | #----------------------------------------------------------------------------- | |
|
12 | ||
|
13 | #----------------------------------------------------------------------------- | |
|
14 | # Imports | |
|
15 | #----------------------------------------------------------------------------- | |
|
16 | ||
|
17 | from __future__ import print_function, absolute_import | |
|
18 | ||
|
19 | # Stdlib imports | |
|
20 | import io | |
|
21 | import os | |
|
22 | import inspect | |
|
23 | from copy import deepcopy | |
|
24 | ||
|
25 | # other libs/dependencies | |
|
26 | from jinja2 import Environment, FileSystemLoader | |
|
27 | from markdown import markdown | |
|
28 | ||
|
29 | # IPython imports | |
|
30 | from IPython.config.configurable import Configurable | |
|
31 | from IPython.config import Config | |
|
32 | from IPython.nbformat import current as nbformat | |
|
33 | from IPython.utils.traitlets import MetaHasTraits, Unicode | |
|
34 | from IPython.utils.text import indent | |
|
35 | ||
|
36 | from IPython.nbconvert import filters | |
|
37 | from IPython.nbconvert import transformers | |
|
38 | ||
|
39 | #----------------------------------------------------------------------------- | |
|
40 | # Globals and constants | |
|
41 | #----------------------------------------------------------------------------- | |
|
42 | ||
|
43 | #Jinja2 extensions to load. | |
|
44 | JINJA_EXTENSIONS = ['jinja2.ext.loopcontrols'] | |
|
45 | ||
|
46 | default_filters = { | |
|
47 | 'indent': indent, | |
|
48 | 'markdown': markdown, | |
|
49 | 'ansi2html': filters.ansi2html, | |
|
50 | 'filter_data_type': filters.DataTypeFilter, | |
|
51 | 'get_lines': filters.get_lines, | |
|
52 | 'highlight': filters.highlight, | |
|
53 | 'highlight2html': filters.highlight, | |
|
54 | 'highlight2latex': filters.highlight2latex, | |
|
55 | 'markdown2latex': filters.markdown2latex, | |
|
56 | 'markdown2rst': filters.markdown2rst, | |
|
57 | 'pycomment': filters.python_comment, | |
|
58 | 'rm_ansi': filters.remove_ansi, | |
|
59 | 'rm_dollars': filters.strip_dollars, | |
|
60 | 'rm_fake': filters.rm_fake, | |
|
61 | 'ansi2latex': filters.ansi2latex, | |
|
62 | 'rm_math_space': filters.rm_math_space, | |
|
63 | 'wrap': filters.wrap | |
|
64 | } | |
|
65 | ||
|
66 | #----------------------------------------------------------------------------- | |
|
67 | # Class | |
|
68 | #----------------------------------------------------------------------------- | |
|
69 | ||
|
70 | class Exporter(Configurable): | |
|
71 | """ | |
|
72 | Exports notebooks into other file formats. Uses Jinja 2 templating engine | |
|
73 | to output new formats. Inherit from this class if you are creating a new | |
|
74 | template type along with new filters/transformers. If the filters/ | |
|
75 | transformers provided by default suffice, there is no need to inherit from | |
|
76 | this class. Instead, override the template_file and file_extension | |
|
77 | traits via a config file. | |
|
78 | ||
|
79 | {filters} | |
|
80 | """ | |
|
81 | ||
|
82 | # finish the docstring | |
|
83 | __doc__ = __doc__.format(filters = '- '+'\n - '.join(default_filters.keys())) | |
|
84 | ||
|
85 | ||
|
86 | template_file = Unicode( | |
|
87 | '', config=True, | |
|
88 | help="Name of the template file to use") | |
|
89 | ||
|
90 | file_extension = Unicode( | |
|
91 | 'txt', config=True, | |
|
92 | help="Extension of the file that should be written to disk" | |
|
93 | ) | |
|
94 | ||
|
95 | template_path = Unicode( | |
|
96 | "/../templates/", config=True, | |
|
97 | help="Path where the template files are located.") | |
|
98 | ||
|
99 | template_skeleton_path = Unicode( | |
|
100 | "/../templates/skeleton/", config=True, | |
|
101 | help="Path where the template skeleton files are located.") | |
|
102 | ||
|
103 | #Jinja block definitions | |
|
104 | jinja_comment_block_start = Unicode("", config=True) | |
|
105 | jinja_comment_block_end = Unicode("", config=True) | |
|
106 | jinja_variable_block_start = Unicode("", config=True) | |
|
107 | jinja_variable_block_end = Unicode("", config=True) | |
|
108 | jinja_logic_block_start = Unicode("", config=True) | |
|
109 | jinja_logic_block_end = Unicode("", config=True) | |
|
110 | ||
|
111 | #Extension that the template files use. | |
|
112 | template_extension = Unicode(".tpl", config=True) | |
|
113 | ||
|
114 | #Processors that process the input data prior to the export, set in the | |
|
115 | #constructor for this class. | |
|
116 | transformers = None | |
|
117 | ||
|
118 | ||
|
119 | def __init__(self, transformers=None, filters=None, config=None, **kw): | |
|
120 | """ | |
|
121 | Public constructor | |
|
122 | ||
|
123 | Parameters | |
|
124 | ---------- | |
|
125 | transformers : list[of transformer] | |
|
126 | Custom transformers to apply to the notebook prior to engaging | |
|
127 | the Jinja template engine. Any transformers specified here | |
|
128 | will override existing transformers if a naming conflict | |
|
129 | occurs. | |
|
130 | filters : dict[of filter] | |
|
131 | filters specified here will override existing filters if a naming | |
|
132 | conflict occurs. Filters are availlable in jinja template through | |
|
133 | the name of the corresponding key. Cf class docstring for | |
|
134 | availlable default filters. | |
|
135 | config : config | |
|
136 | User configuration instance. | |
|
137 | """ | |
|
138 | ||
|
139 | #Call the base class constructor | |
|
140 | c = self.default_config | |
|
141 | if config: | |
|
142 | c.merge(config) | |
|
143 | ||
|
144 | super(Exporter, self).__init__(config=c, **kw) | |
|
145 | ||
|
146 | #Standard environment | |
|
147 | self._init_environment() | |
|
148 | ||
|
149 | #Add transformers | |
|
150 | self._register_transformers() | |
|
151 | ||
|
152 | #Add filters to the Jinja2 environment | |
|
153 | self._register_filters() | |
|
154 | ||
|
155 | #Load user transformers. Overwrite existing transformers if need be. | |
|
156 | if transformers : | |
|
157 | for transformer in transformers: | |
|
158 | self.register_transformer(transformer) | |
|
159 | ||
|
160 | #Load user filters. Overwrite existing filters if need be. | |
|
161 | if not filters is None: | |
|
162 | for key, user_filter in filters.iteritems(): | |
|
163 | if issubclass(user_filter, MetaHasTraits): | |
|
164 | self.environment.filters[key] = user_filter(config=config) | |
|
165 | else: | |
|
166 | self.environment.filters[key] = user_filter | |
|
167 | ||
|
168 | @property | |
|
169 | def default_config(self): | |
|
170 | return Config() | |
|
171 | ||
|
172 | ||
|
173 | ||
|
174 | def from_notebook_node(self, nb, resources=None): | |
|
175 | """ | |
|
176 | Convert a notebook from a notebook node instance. | |
|
177 | ||
|
178 | Parameters | |
|
179 | ---------- | |
|
180 | nb : Notebook node | |
|
181 | resources : a dict of additional resources that | |
|
182 | can be accessed read/write by transformers | |
|
183 | and filters. | |
|
184 | """ | |
|
185 | if resources is None: | |
|
186 | resources = {} | |
|
187 | nb, resources = self._preprocess(nb, resources) | |
|
188 | ||
|
189 | #Load the template file. | |
|
190 | self.template = self.environment.get_template(self.template_file+self.template_extension) | |
|
191 | ||
|
192 | return self.template.render(nb=nb, resources=resources), resources | |
|
193 | ||
|
194 | ||
|
195 | def from_filename(self, filename): | |
|
196 | """ | |
|
197 | Convert a notebook from a notebook file. | |
|
198 | ||
|
199 | Parameters | |
|
200 | ---------- | |
|
201 | filename : str | |
|
202 | Full filename of the notebook file to open and convert. | |
|
203 | """ | |
|
204 | ||
|
205 | with io.open(filename) as f: | |
|
206 | return self.from_notebook_node(nbformat.read(f, 'json')) | |
|
207 | ||
|
208 | ||
|
209 | def from_file(self, file_stream): | |
|
210 | """ | |
|
211 | Convert a notebook from a notebook file. | |
|
212 | ||
|
213 | Parameters | |
|
214 | ---------- | |
|
215 | file_stream : file-like object | |
|
216 | Notebook file-like object to convert. | |
|
217 | """ | |
|
218 | return self.from_notebook_node(nbformat.read(file_stream, 'json')) | |
|
219 | ||
|
220 | ||
|
221 | def register_transformer(self, transformer): | |
|
222 | """ | |
|
223 | Register a transformer. | |
|
224 | Transformers are classes that act upon the notebook before it is | |
|
225 | passed into the Jinja templating engine. Transformers are also | |
|
226 | capable of passing additional information to the Jinja | |
|
227 | templating engine. | |
|
228 | ||
|
229 | Parameters | |
|
230 | ---------- | |
|
231 | transformer : transformer | |
|
232 | """ | |
|
233 | if self.transformers is None: | |
|
234 | self.transformers = [] | |
|
235 | ||
|
236 | if inspect.isfunction(transformer): | |
|
237 | self.transformers.append(transformer) | |
|
238 | return transformer | |
|
239 | elif isinstance(transformer, MetaHasTraits): | |
|
240 | transformer_instance = transformer(config=self.config) | |
|
241 | self.transformers.append(transformer_instance) | |
|
242 | return transformer_instance | |
|
243 | else: | |
|
244 | transformer_instance = transformer() | |
|
245 | self.transformers.append(transformer_instance) | |
|
246 | return transformer_instance | |
|
247 | ||
|
248 | ||
|
249 | def register_filter(self, name, filter): | |
|
250 | """ | |
|
251 | Register a filter. | |
|
252 | A filter is a function that accepts and acts on one string. | |
|
253 | The filters are accesible within the Jinja templating engine. | |
|
254 | ||
|
255 | Parameters | |
|
256 | ---------- | |
|
257 | name : str | |
|
258 | name to give the filter in the Jinja engine | |
|
259 | filter : filter | |
|
260 | """ | |
|
261 | if inspect.isfunction(filter): | |
|
262 | self.environment.filters[name] = filter | |
|
263 | elif isinstance(filter, MetaHasTraits): | |
|
264 | self.environment.filters[name] = filter(config=self.config) | |
|
265 | else: | |
|
266 | self.environment.filters[name] = filter() | |
|
267 | return self.environment.filters[name] | |
|
268 | ||
|
269 | ||
|
270 | def _register_transformers(self): | |
|
271 | """ | |
|
272 | Register all of the transformers needed for this exporter. | |
|
273 | """ | |
|
274 | ||
|
275 | self.register_transformer(transformers.coalesce_streams) | |
|
276 | ||
|
277 | #Remember the figure extraction transformer so it can be enabled and | |
|
278 | #disabled easily later. | |
|
279 | self.extract_figure_transformer = self.register_transformer(transformers.ExtractFigureTransformer) | |
|
280 | ||
|
281 | ||
|
282 | def _register_filters(self): | |
|
283 | """ | |
|
284 | Register all of the filters required for the exporter. | |
|
285 | """ | |
|
286 | for k, v in default_filters.iteritems(): | |
|
287 | self.register_filter(k, v) | |
|
288 | ||
|
289 | ||
|
290 | def _init_environment(self): | |
|
291 | """ | |
|
292 | Create the Jinja templating environment. | |
|
293 | """ | |
|
294 | ||
|
295 | self.environment = Environment( | |
|
296 | loader=FileSystemLoader([ | |
|
297 | os.path.dirname(os.path.realpath(__file__)) + self.template_path, | |
|
298 | os.path.dirname(os.path.realpath(__file__)) + self.template_skeleton_path, | |
|
299 | ]), | |
|
300 | extensions=JINJA_EXTENSIONS | |
|
301 | ) | |
|
302 | ||
|
303 | #Set special Jinja2 syntax that will not conflict with latex. | |
|
304 | if self.jinja_logic_block_start: | |
|
305 | self.environment.block_start_string = self.jinja_logic_block_start | |
|
306 | if self.jinja_logic_block_end: | |
|
307 | self.environment.block_end_string = self.jinja_logic_block_end | |
|
308 | if self.jinja_variable_block_start: | |
|
309 | self.environment.variable_start_string = self.jinja_variable_block_start | |
|
310 | if self.jinja_variable_block_end: | |
|
311 | self.environment.variable_end_string = self.jinja_variable_block_end | |
|
312 | if self.jinja_comment_block_start: | |
|
313 | self.environment.comment_start_string = self.jinja_comment_block_start | |
|
314 | if self.jinja_comment_block_end: | |
|
315 | self.environment.comment_end_string = self.jinja_comment_block_end | |
|
316 | ||
|
317 | ||
|
318 | def _preprocess(self, nb, resources): | |
|
319 | """ | |
|
320 | Preprocess the notebook before passing it into the Jinja engine. | |
|
321 | To preprocess the notebook is to apply all of the | |
|
322 | ||
|
323 | Parameters | |
|
324 | ---------- | |
|
325 | nb : notebook node | |
|
326 | notebook that is being exported. | |
|
327 | resources : a dict of additional resources that | |
|
328 | can be accessed read/write by transformers | |
|
329 | and filters. | |
|
330 | """ | |
|
331 | ||
|
332 | # Do a deepcopy first, | |
|
333 | # we are never safe enough with what the transformers could do. | |
|
334 | nbc = deepcopy(nb) | |
|
335 | resc = deepcopy(resources) | |
|
336 | #Run each transformer on the notebook. Carry the output along | |
|
337 | #to each transformer | |
|
338 | for transformer in self.transformers: | |
|
339 | nb, resources = transformer(nbc, resc) | |
|
340 | return nb, resources | |
|
341 |
@@ -0,0 +1,39 b'' | |||
|
1 | """ | |
|
2 | Exporter for exporting full HTML documents. | |
|
3 | """ | |
|
4 | ||
|
5 | #----------------------------------------------------------------------------- | |
|
6 | # Copyright (c) 2013, the IPython Development Team. | |
|
7 | # | |
|
8 | # Distributed under the terms of the Modified BSD License. | |
|
9 | # | |
|
10 | # The full license is in the file COPYING.txt, distributed with this software. | |
|
11 | #----------------------------------------------------------------------------- | |
|
12 | ||
|
13 | #----------------------------------------------------------------------------- | |
|
14 | # Imports | |
|
15 | #----------------------------------------------------------------------------- | |
|
16 | ||
|
17 | from IPython.utils.traitlets import Unicode | |
|
18 | ||
|
19 | from .basichtml import BasicHtmlExporter | |
|
20 | from IPython.config import Config | |
|
21 | ||
|
22 | #----------------------------------------------------------------------------- | |
|
23 | # Classes | |
|
24 | #----------------------------------------------------------------------------- | |
|
25 | ||
|
26 | class FullHtmlExporter(BasicHtmlExporter): | |
|
27 | """ | |
|
28 | Exports a full HTML document. | |
|
29 | """ | |
|
30 | ||
|
31 | template_file = Unicode( | |
|
32 | 'fullhtml', config=True, | |
|
33 | help="Name of the template file to use") | |
|
34 | ||
|
35 | @property | |
|
36 | def default_config(self): | |
|
37 | c = Config({'CSSHtmlHeaderTransformer':{'enabled':True}}) | |
|
38 | c.merge(super(FullHtmlExporter,self).default_config) | |
|
39 | return c |
@@ -0,0 +1,105 b'' | |||
|
1 | """ | |
|
2 | Exporter that allows Latex Jinja templates to work. Contains logic to | |
|
3 | appropriately prepare IPYNB files for export to LaTeX. Including but | |
|
4 | not limited to escaping LaTeX, fixing math region tags, using special | |
|
5 | tags to circumvent Jinja/Latex syntax conflicts. | |
|
6 | """ | |
|
7 | #----------------------------------------------------------------------------- | |
|
8 | # Copyright (c) 2013, the IPython Development Team. | |
|
9 | # | |
|
10 | # Distributed under the terms of the Modified BSD License. | |
|
11 | # | |
|
12 | # The full license is in the file COPYING.txt, distributed with this software. | |
|
13 | #----------------------------------------------------------------------------- | |
|
14 | ||
|
15 | #----------------------------------------------------------------------------- | |
|
16 | # Imports | |
|
17 | #----------------------------------------------------------------------------- | |
|
18 | ||
|
19 | # IPython imports | |
|
20 | from IPython.utils.traitlets import Unicode | |
|
21 | from IPython.config import Config | |
|
22 | ||
|
23 | from IPython.nbconvert import filters, transformers | |
|
24 | from .exporter import Exporter | |
|
25 | ||
|
26 | #----------------------------------------------------------------------------- | |
|
27 | # Classes and functions | |
|
28 | #----------------------------------------------------------------------------- | |
|
29 | ||
|
30 | class LatexExporter(Exporter): | |
|
31 | """ | |
|
32 | Exports to a Latex template. Inherit from this class if your template is | |
|
33 | LaTeX based and you need custom tranformers/filters. Inherit from it if | |
|
34 | you are writing your own HTML template and need custom tranformers/filters. | |
|
35 | If you don't need custom tranformers/filters, just change the | |
|
36 | 'template_file' config option. Place your template in the special "/latex" | |
|
37 | subfolder of the "../templates" folder. | |
|
38 | """ | |
|
39 | ||
|
40 | file_extension = Unicode( | |
|
41 | 'tex', config=True, | |
|
42 | help="Extension of the file that should be written to disk") | |
|
43 | ||
|
44 | template_file = Unicode( | |
|
45 | 'base', config=True, | |
|
46 | help="Name of the template file to use") | |
|
47 | ||
|
48 | #Latex constants | |
|
49 | template_path = Unicode( | |
|
50 | "/../templates/latex/", config=True, | |
|
51 | help="Path where the template files are located.") | |
|
52 | ||
|
53 | template_skeleton_path = Unicode( | |
|
54 | "/../templates/latex/skeleton/", config=True, | |
|
55 | help="Path where the template skeleton files are located.") | |
|
56 | ||
|
57 | #Special Jinja2 syntax that will not conflict when exporting latex. | |
|
58 | jinja_comment_block_start = Unicode("((=", config=True) | |
|
59 | jinja_comment_block_end = Unicode("=))", config=True) | |
|
60 | jinja_variable_block_start = Unicode("(((", config=True) | |
|
61 | jinja_variable_block_end = Unicode(")))", config=True) | |
|
62 | jinja_logic_block_start = Unicode("((*", config=True) | |
|
63 | jinja_logic_block_end = Unicode("*))", config=True) | |
|
64 | ||
|
65 | #Extension that the template files use. | |
|
66 | template_extension = Unicode(".tplx", config=True) | |
|
67 | ||
|
68 | def _register_filters(self): | |
|
69 | """ | |
|
70 | Register all of the filters required for the exporter. | |
|
71 | """ | |
|
72 | ||
|
73 | #Register the filters of the base class. | |
|
74 | super(LatexExporter, self)._register_filters() | |
|
75 | ||
|
76 | #Add latex filters to the Jinja2 environment | |
|
77 | self.register_filter('escape_tex', filters.escape_latex) | |
|
78 | self.register_filter('highlight', filters.highlight2latex) | |
|
79 | ||
|
80 | ||
|
81 | def _register_transformers(self): | |
|
82 | """ | |
|
83 | Register all of the transformers needed for this exporter. | |
|
84 | """ | |
|
85 | ||
|
86 | #Register the transformers of the base class. | |
|
87 | super(LatexExporter, self)._register_transformers() | |
|
88 | ||
|
89 | #Register latex transformer | |
|
90 | self.register_transformer(transformers.LatexTransformer) | |
|
91 | ||
|
92 | @property | |
|
93 | def default_config(self): | |
|
94 | c = Config({ | |
|
95 | 'GlobalConfigurable': { | |
|
96 | 'display_data_priority' : ['latex', 'svg', 'png', 'jpg', 'jpeg' , 'text'] | |
|
97 | }, | |
|
98 | 'ExtractFigureTransformer': { | |
|
99 | 'enabled':True, | |
|
100 | 'extra_ext_map':{'svg':'pdf'}, | |
|
101 | } | |
|
102 | }) | |
|
103 | c.merge(super(LatexExporter,self).default_config) | |
|
104 | return c | |
|
105 |
@@ -0,0 +1,35 b'' | |||
|
1 | """ | |
|
2 | Exporter that will export your ipynb to Markdown. | |
|
3 | """ | |
|
4 | #----------------------------------------------------------------------------- | |
|
5 | # Copyright (c) 2013, the IPython Development Team. | |
|
6 | # | |
|
7 | # Distributed under the terms of the Modified BSD License. | |
|
8 | # | |
|
9 | # The full license is in the file COPYING.txt, distributed with this software. | |
|
10 | #----------------------------------------------------------------------------- | |
|
11 | ||
|
12 | #----------------------------------------------------------------------------- | |
|
13 | # Imports | |
|
14 | #----------------------------------------------------------------------------- | |
|
15 | ||
|
16 | from IPython.utils.traitlets import Unicode | |
|
17 | ||
|
18 | from .exporter import Exporter | |
|
19 | ||
|
20 | #----------------------------------------------------------------------------- | |
|
21 | # Classes | |
|
22 | #----------------------------------------------------------------------------- | |
|
23 | ||
|
24 | class MarkdownExporter(Exporter): | |
|
25 | """ | |
|
26 | Exports to a markdown document (.md) | |
|
27 | """ | |
|
28 | ||
|
29 | file_extension = Unicode( | |
|
30 | 'md', config=True, | |
|
31 | help="Extension of the file that should be written to disk") | |
|
32 | ||
|
33 | template_file = Unicode( | |
|
34 | 'markdown', config=True, | |
|
35 | help="Name of the template file to use") |
@@ -0,0 +1,35 b'' | |||
|
1 | """ | |
|
2 | Python exporter which exports Notebook code into a PY file. | |
|
3 | """ | |
|
4 | #----------------------------------------------------------------------------- | |
|
5 | # Copyright (c) 2013, the IPython Development Team. | |
|
6 | # | |
|
7 | # Distributed under the terms of the Modified BSD License. | |
|
8 | # | |
|
9 | # The full license is in the file COPYING.txt, distributed with this software. | |
|
10 | #----------------------------------------------------------------------------- | |
|
11 | ||
|
12 | #----------------------------------------------------------------------------- | |
|
13 | # Imports | |
|
14 | #----------------------------------------------------------------------------- | |
|
15 | ||
|
16 | from IPython.utils.traitlets import Unicode | |
|
17 | ||
|
18 | from .exporter import Exporter | |
|
19 | ||
|
20 | #----------------------------------------------------------------------------- | |
|
21 | # Classes | |
|
22 | #----------------------------------------------------------------------------- | |
|
23 | ||
|
24 | class PythonExporter(Exporter): | |
|
25 | """ | |
|
26 | Exports a Python code file. | |
|
27 | """ | |
|
28 | ||
|
29 | file_extension = Unicode( | |
|
30 | 'py', config=True, | |
|
31 | help="Extension of the file that should be written to disk") | |
|
32 | ||
|
33 | template_file = Unicode( | |
|
34 | 'python', config=True, | |
|
35 | help="Name of the template file to use") |
@@ -0,0 +1,32 b'' | |||
|
1 | """ | |
|
2 | Exporter that exports a Python-Armor code file (.py) | |
|
3 | """ | |
|
4 | ||
|
5 | #----------------------------------------------------------------------------- | |
|
6 | # Copyright (c) 2013, the IPython Development Team. | |
|
7 | # | |
|
8 | # Distributed under the terms of the Modified BSD License. | |
|
9 | # | |
|
10 | # The full license is in the file COPYING.txt, distributed with this software. | |
|
11 | #----------------------------------------------------------------------------- | |
|
12 | ||
|
13 | #----------------------------------------------------------------------------- | |
|
14 | # Imports | |
|
15 | #----------------------------------------------------------------------------- | |
|
16 | ||
|
17 | from IPython.utils.traitlets import Unicode | |
|
18 | ||
|
19 | from .python import PythonExporter | |
|
20 | ||
|
21 | #----------------------------------------------------------------------------- | |
|
22 | # Classes | |
|
23 | #----------------------------------------------------------------------------- | |
|
24 | ||
|
25 | class PythonArmorExporter(PythonExporter): | |
|
26 | """ | |
|
27 | Exports a Python-Armor code file (.py) | |
|
28 | """ | |
|
29 | ||
|
30 | template_file = Unicode( | |
|
31 | 'python_armor', config=True, | |
|
32 | help="Name of the template file to use") |
@@ -0,0 +1,54 b'' | |||
|
1 | """ | |
|
2 | Reveal slide show exporter. | |
|
3 | """ | |
|
4 | #----------------------------------------------------------------------------- | |
|
5 | # Copyright (c) 2013, the IPython Development Team. | |
|
6 | # | |
|
7 | # Distributed under the terms of the Modified BSD License. | |
|
8 | # | |
|
9 | # The full license is in the file COPYING.txt, distributed with this software. | |
|
10 | #----------------------------------------------------------------------------- | |
|
11 | ||
|
12 | #----------------------------------------------------------------------------- | |
|
13 | # Imports | |
|
14 | #----------------------------------------------------------------------------- | |
|
15 | ||
|
16 | from IPython.utils.traitlets import Unicode | |
|
17 | from IPython.config import Config | |
|
18 | ||
|
19 | from .basichtml import BasicHtmlExporter | |
|
20 | from IPython.nbconvert import transformers | |
|
21 | ||
|
22 | #----------------------------------------------------------------------------- | |
|
23 | # Classes | |
|
24 | #----------------------------------------------------------------------------- | |
|
25 | ||
|
26 | class RevealExporter(BasicHtmlExporter): | |
|
27 | """ | |
|
28 | Exports a Reveal slide show (.HTML) which may be rendered in a web browser. | |
|
29 | """ | |
|
30 | ||
|
31 | file_extension = Unicode( | |
|
32 | 'reveal.html', config=True, | |
|
33 | help="Extension of the file that should be written to disk") | |
|
34 | ||
|
35 | template_file = Unicode( | |
|
36 | 'reveal', config=True, | |
|
37 | help="Name of the template file to use") | |
|
38 | ||
|
39 | def _register_transformers(self): | |
|
40 | """ | |
|
41 | Register all of the transformers needed for this exporter. | |
|
42 | """ | |
|
43 | ||
|
44 | #Register the transformers of the base class. | |
|
45 | super(RevealExporter, self)._register_transformers() | |
|
46 | ||
|
47 | #Register reveal help transformer | |
|
48 | self.register_transformer(transformers.RevealHelpTransformer) | |
|
49 | ||
|
50 | @property | |
|
51 | def default_config(self): | |
|
52 | c = Config({'CSSHtmlHeaderTransformer':{'enabled':True}}) | |
|
53 | c.merge(super(RevealExporter,self).default_config) | |
|
54 | return c |
@@ -0,0 +1,42 b'' | |||
|
1 | """ | |
|
2 | Exporter for exporting notebooks to restructured text. | |
|
3 | """ | |
|
4 | #----------------------------------------------------------------------------- | |
|
5 | # Copyright (c) 2013, the IPython Development Team. | |
|
6 | # | |
|
7 | # Distributed under the terms of the Modified BSD License. | |
|
8 | # | |
|
9 | # The full license is in the file COPYING.txt, distributed with this software. | |
|
10 | #----------------------------------------------------------------------------- | |
|
11 | ||
|
12 | #----------------------------------------------------------------------------- | |
|
13 | # Imports | |
|
14 | #----------------------------------------------------------------------------- | |
|
15 | ||
|
16 | from IPython.utils.traitlets import Unicode | |
|
17 | from IPython.config import Config | |
|
18 | ||
|
19 | from .exporter import Exporter | |
|
20 | ||
|
21 | #----------------------------------------------------------------------------- | |
|
22 | # Classes | |
|
23 | #----------------------------------------------------------------------------- | |
|
24 | ||
|
25 | class RstExporter(Exporter): | |
|
26 | """ | |
|
27 | Exports restructured text documents. | |
|
28 | """ | |
|
29 | ||
|
30 | file_extension = Unicode( | |
|
31 | 'rst', config=True, | |
|
32 | help="Extension of the file that should be written to disk") | |
|
33 | ||
|
34 | template_file = Unicode( | |
|
35 | 'rst', config=True, | |
|
36 | help="Name of the template file to use") | |
|
37 | ||
|
38 | @property | |
|
39 | def default_config(self): | |
|
40 | c = Config({'ExtractFigureTransformer':{'enabled':True}}) | |
|
41 | c.merge(super(RstExporter,self).default_config) | |
|
42 | return c |
@@ -0,0 +1,54 b'' | |||
|
1 | """ | |
|
2 | Exporter for exporting notebooks to Sphinx 'HowTo' style latex. Latex | |
|
3 | formatted for use with PDFLatex. | |
|
4 | """ | |
|
5 | #----------------------------------------------------------------------------- | |
|
6 | # Copyright (c) 2013, the IPython Development Team. | |
|
7 | # | |
|
8 | # Distributed under the terms of the Modified BSD License. | |
|
9 | # | |
|
10 | # The full license is in the file COPYING.txt, distributed with this software. | |
|
11 | #----------------------------------------------------------------------------- | |
|
12 | ||
|
13 | #----------------------------------------------------------------------------- | |
|
14 | # Imports | |
|
15 | #----------------------------------------------------------------------------- | |
|
16 | ||
|
17 | from IPython.utils.traitlets import Unicode | |
|
18 | from IPython.config import Config | |
|
19 | ||
|
20 | # local import | |
|
21 | from .latex import LatexExporter | |
|
22 | ||
|
23 | from IPython.nbconvert import transformers | |
|
24 | ||
|
25 | #----------------------------------------------------------------------------- | |
|
26 | # Classes | |
|
27 | #----------------------------------------------------------------------------- | |
|
28 | ||
|
29 | class SphinxHowtoExporter(LatexExporter): | |
|
30 | """ | |
|
31 | Exports Sphinx "HowTo" LaTeX documents. The Sphinx "HowTo" exporter | |
|
32 | produces short document format latex for use with PDFLatex. | |
|
33 | """ | |
|
34 | ||
|
35 | template_file = Unicode( | |
|
36 | 'sphinx_howto', config=True, | |
|
37 | help="Name of the template file to use") | |
|
38 | ||
|
39 | def _register_transformers(self): | |
|
40 | ||
|
41 | #Register the transformers of the base class. | |
|
42 | super(SphinxHowtoExporter, self)._register_transformers() | |
|
43 | ||
|
44 | #Register sphinx latex transformer | |
|
45 | self.register_transformer(transformers.SphinxTransformer) | |
|
46 | ||
|
47 | @property | |
|
48 | def default_config(self): | |
|
49 | c = Config({ | |
|
50 | 'SphinxTransformer': {'enabled':True} | |
|
51 | }) | |
|
52 | c.merge(super(SphinxHowtoExporter,self).default_config) | |
|
53 | return c | |
|
54 |
@@ -0,0 +1,34 b'' | |||
|
1 | """ | |
|
2 | Exporter for exporting notebooks to Sphinx 'Manual' style latex. Latex | |
|
3 | formatted for use with PDFLatex. | |
|
4 | """ | |
|
5 | #----------------------------------------------------------------------------- | |
|
6 | # Copyright (c) 2013, the IPython Development Team. | |
|
7 | # | |
|
8 | # Distributed under the terms of the Modified BSD License. | |
|
9 | # | |
|
10 | # The full license is in the file COPYING.txt, distributed with this software. | |
|
11 | #----------------------------------------------------------------------------- | |
|
12 | ||
|
13 | #----------------------------------------------------------------------------- | |
|
14 | # Imports | |
|
15 | #----------------------------------------------------------------------------- | |
|
16 | ||
|
17 | from IPython.utils.traitlets import Unicode | |
|
18 | ||
|
19 | from .sphinx_howto import SphinxHowtoExporter | |
|
20 | ||
|
21 | #----------------------------------------------------------------------------- | |
|
22 | # Classes | |
|
23 | #----------------------------------------------------------------------------- | |
|
24 | ||
|
25 | class SphinxManualExporter(SphinxHowtoExporter): | |
|
26 | """ | |
|
27 | Exports Sphinx "Manual" LaTeX documents. The Sphinx "Manual" exporter | |
|
28 | produces book like latex output for use with PDFLatex. | |
|
29 | """ | |
|
30 | ||
|
31 | template_file = Unicode( | |
|
32 | 'sphinx_manual', config=True, | |
|
33 | help="Name of the template file to use") | |
|
34 | No newline at end of file |
@@ -0,0 +1,6 b'' | |||
|
1 | from .ansi import * | |
|
2 | from .datatypefilter import * | |
|
3 | from .highlight import * | |
|
4 | from .latex import * | |
|
5 | from .markdown import * | |
|
6 | from .strings import * No newline at end of file |
@@ -0,0 +1,145 b'' | |||
|
1 | """Filters for processing ANSI colors within Jinja templates. | |
|
2 | """ | |
|
3 | #----------------------------------------------------------------------------- | |
|
4 | # Copyright (c) 2013, the IPython Development Team. | |
|
5 | # | |
|
6 | # Distributed under the terms of the Modified BSD License. | |
|
7 | # | |
|
8 | # The full license is in the file COPYING.txt, distributed with this software. | |
|
9 | #----------------------------------------------------------------------------- | |
|
10 | ||
|
11 | #----------------------------------------------------------------------------- | |
|
12 | # Imports | |
|
13 | #----------------------------------------------------------------------------- | |
|
14 | ||
|
15 | import re | |
|
16 | from IPython.utils import coloransi | |
|
17 | ||
|
18 | #----------------------------------------------------------------------------- | |
|
19 | # Classes and functions | |
|
20 | #----------------------------------------------------------------------------- | |
|
21 | ||
|
22 | __all__ = [ | |
|
23 | 'remove_ansi', | |
|
24 | 'ansi2html', | |
|
25 | 'single_ansi2latex', | |
|
26 | 'ansi2latex' | |
|
27 | ] | |
|
28 | ||
|
29 | def remove_ansi(source): | |
|
30 | """ | |
|
31 | Remove ansi from text | |
|
32 | ||
|
33 | Parameters | |
|
34 | ---------- | |
|
35 | source : str | |
|
36 | Source to remove the ansi from | |
|
37 | """ | |
|
38 | ||
|
39 | return re.sub(r'\033\[(0|\d;\d\d)m', '', source) | |
|
40 | ||
|
41 | ||
|
42 | def ansi2html(text): | |
|
43 | """ | |
|
44 | Conver ansi colors to html colors. | |
|
45 | ||
|
46 | Parameters | |
|
47 | ---------- | |
|
48 | text : str | |
|
49 | Text containing ansi colors to convert to html | |
|
50 | """ | |
|
51 | ||
|
52 | ansi_colormap = { | |
|
53 | '30': 'ansiblack', | |
|
54 | '31': 'ansired', | |
|
55 | '32': 'ansigreen', | |
|
56 | '33': 'ansiyellow', | |
|
57 | '34': 'ansiblue', | |
|
58 | '35': 'ansipurple', | |
|
59 | '36': 'ansicyan', | |
|
60 | '37': 'ansigrey', | |
|
61 | '01': 'ansibold', | |
|
62 | } | |
|
63 | ||
|
64 | # do ampersand first | |
|
65 | text = text.replace('&', '&') | |
|
66 | html_escapes = { | |
|
67 | '<': '<', | |
|
68 | '>': '>', | |
|
69 | "'": ''', | |
|
70 | '"': '"', | |
|
71 | '`': '`', | |
|
72 | } | |
|
73 | ||
|
74 | for c, escape in html_escapes.iteritems(): | |
|
75 | text = text.replace(c, escape) | |
|
76 | ||
|
77 | ansi_re = re.compile('\x1b' + r'\[([\dA-Fa-f;]*?)m') | |
|
78 | m = ansi_re.search(text) | |
|
79 | opened = False | |
|
80 | cmds = [] | |
|
81 | opener = '' | |
|
82 | closer = '' | |
|
83 | while m: | |
|
84 | cmds = m.groups()[0].split(';') | |
|
85 | closer = '</span>' if opened else '' | |
|
86 | ||
|
87 | # True if there is there more than one element in cmds, *or* | |
|
88 | # if there is only one but it is not equal to a string of zeroes. | |
|
89 | opened = len(cmds) > 1 or cmds[0] != '0' * len(cmds[0]) | |
|
90 | classes = [] | |
|
91 | for cmd in cmds: | |
|
92 | if cmd in ansi_colormap: | |
|
93 | classes.append(ansi_colormap.get(cmd)) | |
|
94 | ||
|
95 | if classes: | |
|
96 | opener = '<span class="%s">' % (' '.join(classes)) | |
|
97 | else: | |
|
98 | opener = '' | |
|
99 | text = re.sub(ansi_re, closer + opener, text, 1) | |
|
100 | ||
|
101 | m = ansi_re.search(text) | |
|
102 | ||
|
103 | if opened: | |
|
104 | text += '</span>' | |
|
105 | return text | |
|
106 | ||
|
107 | ||
|
108 | def single_ansi2latex(code): | |
|
109 | """Converts single ansi markup to latex format | |
|
110 | ||
|
111 | Return latex code and number of open brackets. | |
|
112 | """ | |
|
113 | for color in coloransi.color_templates: | |
|
114 | colcode = getattr(coloransi.TermColors,color[0]) | |
|
115 | # regular fonts | |
|
116 | if code == colcode: | |
|
117 | return '\\'+color[0].lower()+'{', 1 | |
|
118 | # bold fonts | |
|
119 | if code == colcode[:3]+str(1)+colcode[3:]: | |
|
120 | return '\\textbf{\\textcolor{'+color[0].lower()+'}{', 2 | |
|
121 | return '', 0 | |
|
122 | ||
|
123 | def ansi2latex(text): | |
|
124 | """Converts ansi formated text to latex version | |
|
125 | ||
|
126 | based on https://bitbucket.org/birkenfeld/sphinx-contrib/ansi.py | |
|
127 | """ | |
|
128 | color_pattern = re.compile('\x1b\\[([^m]+)m') | |
|
129 | last_end = 0 | |
|
130 | openbrack = 0 | |
|
131 | outstring = '' | |
|
132 | for match in color_pattern.finditer(text): | |
|
133 | head = text[last_end:match.start()] | |
|
134 | outstring += head | |
|
135 | if openbrack: | |
|
136 | outstring += '}'*openbrack | |
|
137 | openbrack = 0 | |
|
138 | if match.group() <> coloransi.TermColors.Normal and not openbrack: | |
|
139 | texform, openbrack = single_ansi2latex(match.group()) | |
|
140 | outstring += texform | |
|
141 | last_end = match.end() | |
|
142 | if openbrack: | |
|
143 | outstring += '}'*openbrack | |
|
144 | outstring += text[last_end:] | |
|
145 | return outstring.strip() |
@@ -0,0 +1,33 b'' | |||
|
1 | """Filter used to select the first preferred output format available. | |
|
2 | ||
|
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 | |
|
5 | value of the different formats is set via | |
|
6 | GlobalConfigurable.display_data_priority | |
|
7 | """ | |
|
8 | #----------------------------------------------------------------------------- | |
|
9 | # Copyright (c) 2013, the IPython Development Team. | |
|
10 | # | |
|
11 | # Distributed under the terms of the Modified BSD License. | |
|
12 | # | |
|
13 | # The full license is in the file COPYING.txt, distributed with this software. | |
|
14 | #----------------------------------------------------------------------------- | |
|
15 | ||
|
16 | #----------------------------------------------------------------------------- | |
|
17 | # Classes and functions | |
|
18 | #----------------------------------------------------------------------------- | |
|
19 | ||
|
20 | from ..utils.config import GlobalConfigurable | |
|
21 | ||
|
22 | __all__ = ['DataTypeFilter'] | |
|
23 | ||
|
24 | class DataTypeFilter(GlobalConfigurable): | |
|
25 | """ Returns the preferred display format """ | |
|
26 | ||
|
27 | def __call__(self, output): | |
|
28 | """ Return the first available format in the priority """ | |
|
29 | ||
|
30 | for fmt in self.display_data_priority: | |
|
31 | if fmt in output: | |
|
32 | return [fmt] | |
|
33 | return [] |
@@ -0,0 +1,88 b'' | |||
|
1 | """ | |
|
2 | Module containing filter functions that allow code to be highlighted | |
|
3 | from within Jinja templates. | |
|
4 | """ | |
|
5 | #----------------------------------------------------------------------------- | |
|
6 | # Copyright (c) 2013, the IPython Development Team. | |
|
7 | # | |
|
8 | # Distributed under the terms of the Modified BSD License. | |
|
9 | # | |
|
10 | # The full license is in the file COPYING.txt, distributed with this software. | |
|
11 | #----------------------------------------------------------------------------- | |
|
12 | ||
|
13 | #----------------------------------------------------------------------------- | |
|
14 | # Imports | |
|
15 | #----------------------------------------------------------------------------- | |
|
16 | ||
|
17 | from pygments import highlight as pygements_highlight | |
|
18 | from pygments.lexers import get_lexer_by_name | |
|
19 | from pygments.formatters import HtmlFormatter | |
|
20 | from pygments.formatters import LatexFormatter | |
|
21 | ||
|
22 | # Our own imports | |
|
23 | from IPython.nbconvert.utils.lexers import IPythonLexer | |
|
24 | ||
|
25 | #----------------------------------------------------------------------------- | |
|
26 | # Globals and constants | |
|
27 | #----------------------------------------------------------------------------- | |
|
28 | ||
|
29 | MULTILINE_OUTPUTS = ['text', 'html', 'svg', 'latex', 'javascript', 'json'] | |
|
30 | ||
|
31 | #----------------------------------------------------------------------------- | |
|
32 | # Utility functions | |
|
33 | #----------------------------------------------------------------------------- | |
|
34 | ||
|
35 | __all__ = [ | |
|
36 | 'highlight', | |
|
37 | 'highlight2latex' | |
|
38 | ] | |
|
39 | ||
|
40 | ||
|
41 | def highlight(source, language='ipython'): | |
|
42 | """ | |
|
43 | Return a syntax-highlighted version of the input source as html output. | |
|
44 | ||
|
45 | Parameters | |
|
46 | ---------- | |
|
47 | source : str | |
|
48 | Source code to highlight the syntax of. | |
|
49 | language : str | |
|
50 | Language to highlight the syntax of. | |
|
51 | """ | |
|
52 | ||
|
53 | return _pygment_highlight(source, HtmlFormatter(), language) | |
|
54 | ||
|
55 | ||
|
56 | def highlight2latex(source, language='ipython'): | |
|
57 | """ | |
|
58 | Return a syntax-highlighted version of the input source as latex output. | |
|
59 | ||
|
60 | Parameters | |
|
61 | ---------- | |
|
62 | source : str | |
|
63 | Source code to highlight the syntax of. | |
|
64 | language : str | |
|
65 | Language to highlight the syntax of. | |
|
66 | """ | |
|
67 | return _pygment_highlight(source, LatexFormatter(), language) | |
|
68 | ||
|
69 | ||
|
70 | def _pygment_highlight(source, output_formatter, language='ipython'): | |
|
71 | """ | |
|
72 | Return a syntax-highlighted version of the input source | |
|
73 | ||
|
74 | Parameters | |
|
75 | ---------- | |
|
76 | source : str | |
|
77 | Source code to highlight the syntax of. | |
|
78 | output_formatter : Pygments formatter | |
|
79 | language : str | |
|
80 | Language to highlight the syntax of. | |
|
81 | """ | |
|
82 | ||
|
83 | if language == 'ipython': | |
|
84 | lexer = IPythonLexer() | |
|
85 | else: | |
|
86 | lexer = get_lexer_by_name(language, stripall=True) | |
|
87 | ||
|
88 | return pygements_highlight(source, lexer, output_formatter) |
@@ -0,0 +1,115 b'' | |||
|
1 | """Latex filters. | |
|
2 | ||
|
3 | Module of useful filters for processing Latex within Jinja latex templates. | |
|
4 | """ | |
|
5 | #----------------------------------------------------------------------------- | |
|
6 | # Copyright (c) 2013, the IPython Development Team. | |
|
7 | # | |
|
8 | # Distributed under the terms of the Modified BSD License. | |
|
9 | # | |
|
10 | # The full license is in the file COPYING.txt, distributed with this software. | |
|
11 | #----------------------------------------------------------------------------- | |
|
12 | ||
|
13 | #----------------------------------------------------------------------------- | |
|
14 | # Imports | |
|
15 | #----------------------------------------------------------------------------- | |
|
16 | import re | |
|
17 | ||
|
18 | #----------------------------------------------------------------------------- | |
|
19 | # Globals and constants | |
|
20 | #----------------------------------------------------------------------------- | |
|
21 | ||
|
22 | #Latex substitutions for escaping latex. | |
|
23 | LATEX_SUBS = ( | |
|
24 | (re.compile('\033\[[0-9;]+m'),''), # handle console escapes | |
|
25 | (re.compile(r'\\'), r'\\textbackslash'), | |
|
26 | (re.compile(r'([{}_#%&$])'), r'\\\1'), | |
|
27 | (re.compile(r'~'), r'\~{}'), | |
|
28 | (re.compile(r'\^'), r'\^{}'), | |
|
29 | (re.compile(r'"'), r"''"), | |
|
30 | (re.compile(r'\.\.\.+'), r'\\ldots'), | |
|
31 | ) | |
|
32 | ||
|
33 | #----------------------------------------------------------------------------- | |
|
34 | # Functions | |
|
35 | #----------------------------------------------------------------------------- | |
|
36 | ||
|
37 | __all__ = [ | |
|
38 | 'escape_latex', | |
|
39 | 'rm_math_space' | |
|
40 | ] | |
|
41 | ||
|
42 | ||
|
43 | def escape_latex(text): | |
|
44 | """ | |
|
45 | Escape characters that may conflict with latex. | |
|
46 | ||
|
47 | Parameters | |
|
48 | ---------- | |
|
49 | text : str | |
|
50 | Text containing characters that may conflict with Latex | |
|
51 | """ | |
|
52 | return_text = text | |
|
53 | for pattern, replacement in LATEX_SUBS: | |
|
54 | return_text = pattern.sub(replacement, return_text) | |
|
55 | return return_text | |
|
56 | ||
|
57 | ||
|
58 | def rm_math_space(text): | |
|
59 | """ | |
|
60 | Remove the space between latex math commands and enclosing $ symbols. | |
|
61 | This filter is important because latex isn't as flexible as the notebook | |
|
62 | front end when it comes to flagging math using ampersand symbols. | |
|
63 | ||
|
64 | Parameters | |
|
65 | ---------- | |
|
66 | text : str | |
|
67 | Text to filter. | |
|
68 | """ | |
|
69 | ||
|
70 | # First, scan through the markdown looking for $. If | |
|
71 | # a $ symbol is found, without a preceding \, assume | |
|
72 | # it is the start of a math block. UNLESS that $ is | |
|
73 | # not followed by another within two math_lines. | |
|
74 | math_regions = [] | |
|
75 | math_lines = 0 | |
|
76 | within_math = False | |
|
77 | math_start_index = 0 | |
|
78 | ptext = '' | |
|
79 | last_character = "" | |
|
80 | skip = False | |
|
81 | for index, char in enumerate(text): | |
|
82 | ||
|
83 | #Make sure the character isn't preceeded by a backslash | |
|
84 | if (char == "$" and last_character != "\\"): | |
|
85 | ||
|
86 | # Close the math region if this is an ending $ | |
|
87 | if within_math: | |
|
88 | within_math = False | |
|
89 | skip = True | |
|
90 | ptext = ptext+'$'+text[math_start_index+1:index].strip()+'$' | |
|
91 | math_regions.append([math_start_index, index+1]) | |
|
92 | else: | |
|
93 | ||
|
94 | # Start a new math region | |
|
95 | within_math = True | |
|
96 | math_start_index = index | |
|
97 | math_lines = 0 | |
|
98 | ||
|
99 | # If we are in a math region, count the number of lines parsed. | |
|
100 | # Cancel the math region if we find two line breaks! | |
|
101 | elif char == "\n": | |
|
102 | if within_math: | |
|
103 | math_lines += 1 | |
|
104 | if math_lines > 1: | |
|
105 | within_math = False | |
|
106 | ptext = ptext+text[math_start_index:index] | |
|
107 | ||
|
108 | # Remember the last character so we can easily watch | |
|
109 | # for backslashes | |
|
110 | last_character = char | |
|
111 | if not within_math and not skip: | |
|
112 | ptext = ptext+char | |
|
113 | if skip: | |
|
114 | skip = False | |
|
115 | return ptext |
@@ -0,0 +1,85 b'' | |||
|
1 | """Markdown filters | |
|
2 | This file contains a collection of utility filters for dealing with | |
|
3 | markdown within Jinja templates. | |
|
4 | """ | |
|
5 | #----------------------------------------------------------------------------- | |
|
6 | # Copyright (c) 2013, the IPython Development Team. | |
|
7 | # | |
|
8 | # Distributed under the terms of the Modified BSD License. | |
|
9 | # | |
|
10 | # The full license is in the file COPYING.txt, distributed with this software. | |
|
11 | #----------------------------------------------------------------------------- | |
|
12 | ||
|
13 | #----------------------------------------------------------------------------- | |
|
14 | # Imports | |
|
15 | #----------------------------------------------------------------------------- | |
|
16 | from __future__ import print_function | |
|
17 | ||
|
18 | # Stdlib imports | |
|
19 | import sys | |
|
20 | import subprocess | |
|
21 | ||
|
22 | #----------------------------------------------------------------------------- | |
|
23 | # Functions | |
|
24 | #----------------------------------------------------------------------------- | |
|
25 | ||
|
26 | __all__ = [ | |
|
27 | 'markdown2latex', | |
|
28 | 'markdown2rst' | |
|
29 | ] | |
|
30 | ||
|
31 | ||
|
32 | def markdown2latex(source): | |
|
33 | """Convert a markdown string to LaTeX via pandoc. | |
|
34 | ||
|
35 | This function will raise an error if pandoc is not installed. | |
|
36 | Any error messages generated by pandoc are printed to stderr. | |
|
37 | ||
|
38 | Parameters | |
|
39 | ---------- | |
|
40 | source : string | |
|
41 | Input string, assumed to be valid markdown. | |
|
42 | ||
|
43 | Returns | |
|
44 | ------- | |
|
45 | out : string | |
|
46 | Output as returned by pandoc. | |
|
47 | """ | |
|
48 | p = subprocess.Popen('pandoc -f markdown -t latex'.split(), | |
|
49 | stdin=subprocess.PIPE, stdout=subprocess.PIPE) | |
|
50 | ||
|
51 | out, err = p.communicate(source.encode('utf-8')) | |
|
52 | ||
|
53 | if err: | |
|
54 | print(err, file=sys.stderr) | |
|
55 | #print('*'*20+'\n', out, '\n'+'*'*20) # dbg | |
|
56 | ||
|
57 | return unicode(out, 'utf-8')[:-1] | |
|
58 | ||
|
59 | ||
|
60 | def markdown2rst(source): | |
|
61 | """Convert a markdown string to LaTeX via pandoc. | |
|
62 | ||
|
63 | This function will raise an error if pandoc is not installed. | |
|
64 | Any error messages generated by pandoc are printed to stderr. | |
|
65 | ||
|
66 | Parameters | |
|
67 | ---------- | |
|
68 | source : string | |
|
69 | Input string, assumed to be valid markdown. | |
|
70 | ||
|
71 | Returns | |
|
72 | ------- | |
|
73 | out : string | |
|
74 | Output as returned by pandoc. | |
|
75 | """ | |
|
76 | p = subprocess.Popen('pandoc -f markdown -t rst'.split(), | |
|
77 | stdin=subprocess.PIPE, stdout=subprocess.PIPE) | |
|
78 | ||
|
79 | out, err = p.communicate(source.encode('utf-8')) | |
|
80 | ||
|
81 | if err: | |
|
82 | print(err, file=sys.stderr) | |
|
83 | #print('*'*20+'\n', out, '\n'+'*'*20) # dbg | |
|
84 | ||
|
85 | return unicode(out, 'utf-8') |
@@ -0,0 +1,113 b'' | |||
|
1 | """String filters. | |
|
2 | ||
|
3 | Contains a collection of useful string manipulation filters for use in Jinja | |
|
4 | templates. | |
|
5 | """ | |
|
6 | #----------------------------------------------------------------------------- | |
|
7 | # Copyright (c) 2013, the IPython Development Team. | |
|
8 | # | |
|
9 | # Distributed under the terms of the Modified BSD License. | |
|
10 | # | |
|
11 | # The full license is in the file COPYING.txt, distributed with this software. | |
|
12 | #----------------------------------------------------------------------------- | |
|
13 | ||
|
14 | #----------------------------------------------------------------------------- | |
|
15 | # Imports | |
|
16 | #----------------------------------------------------------------------------- | |
|
17 | ||
|
18 | # Our own imports | |
|
19 | import textwrap | |
|
20 | ||
|
21 | #----------------------------------------------------------------------------- | |
|
22 | # Functions | |
|
23 | #----------------------------------------------------------------------------- | |
|
24 | ||
|
25 | __all__ = [ | |
|
26 | 'wrap', | |
|
27 | 'strip_dollars', | |
|
28 | 'rm_fake', | |
|
29 | 'python_comment', | |
|
30 | 'get_lines' | |
|
31 | ] | |
|
32 | ||
|
33 | ||
|
34 | def wrap(text, width=100): | |
|
35 | """ | |
|
36 | Intelligently wrap text. | |
|
37 | Wrap text without breaking words if possible. | |
|
38 | ||
|
39 | Parameters | |
|
40 | ---------- | |
|
41 | text : str | |
|
42 | Text to wrap. | |
|
43 | width : int, optional | |
|
44 | Number of characters to wrap to, default 100. | |
|
45 | """ | |
|
46 | ||
|
47 | split_text = text.split('\n') | |
|
48 | wrp = map(lambda x:textwrap.wrap(x,width), split_text) | |
|
49 | wrpd = map('\n'.join, wrp) | |
|
50 | return '\n'.join(wrpd) | |
|
51 | ||
|
52 | ||
|
53 | def strip_dollars(text): | |
|
54 | """ | |
|
55 | Remove all dollar symbols from text | |
|
56 | ||
|
57 | Parameters | |
|
58 | ---------- | |
|
59 | text : str | |
|
60 | Text to remove dollars from | |
|
61 | """ | |
|
62 | ||
|
63 | return text.strip('$') | |
|
64 | ||
|
65 | ||
|
66 | def rm_fake(text): | |
|
67 | """ | |
|
68 | Remove all occurrences of '/files/' from text | |
|
69 | ||
|
70 | Parameters | |
|
71 | ---------- | |
|
72 | text : str | |
|
73 | Text to remove '/files/' from | |
|
74 | """ | |
|
75 | return text.replace('/files/', '') | |
|
76 | ||
|
77 | ||
|
78 | def python_comment(text): | |
|
79 | """ | |
|
80 | Build a Python comment line from input text. | |
|
81 | ||
|
82 | Parameters | |
|
83 | ---------- | |
|
84 | text : str | |
|
85 | Text to comment out. | |
|
86 | """ | |
|
87 | ||
|
88 | #Replace line breaks with line breaks and comment symbols. | |
|
89 | #Also add a comment symbol at the beginning to comment out | |
|
90 | #the first line. | |
|
91 | return '# '+'\n# '.join(text.split('\n')) | |
|
92 | ||
|
93 | ||
|
94 | def get_lines(text, start=None,end=None): | |
|
95 | """ | |
|
96 | Split the input text into separate lines and then return the | |
|
97 | lines that the caller is interested in. | |
|
98 | ||
|
99 | Parameters | |
|
100 | ---------- | |
|
101 | text : str | |
|
102 | Text to parse lines from. | |
|
103 | start : int, optional | |
|
104 | First line to grab from. | |
|
105 | end : int, optional | |
|
106 | Last line to grab from. | |
|
107 | """ | |
|
108 | ||
|
109 | # Split the input into lines. | |
|
110 | lines = text.split("\n") | |
|
111 | ||
|
112 | # Return the right lines. | |
|
113 | return "\n".join(lines[start:end]) #re-join |
@@ -0,0 +1,212 b'' | |||
|
1 | #!/usr/bin/env python | |
|
2 | """NBConvert is a utility for conversion of IPYNB files. | |
|
3 | ||
|
4 | Commandline interface for the NBConvert conversion utility. Read the | |
|
5 | readme.rst for usage information | |
|
6 | """ | |
|
7 | #----------------------------------------------------------------------------- | |
|
8 | #Copyright (c) 2013, the IPython Development Team. | |
|
9 | # | |
|
10 | #Distributed under the terms of the Modified BSD License. | |
|
11 | # | |
|
12 | #The full license is in the file COPYING.txt, distributed with this software. | |
|
13 | #----------------------------------------------------------------------------- | |
|
14 | ||
|
15 | #----------------------------------------------------------------------------- | |
|
16 | #Imports | |
|
17 | #----------------------------------------------------------------------------- | |
|
18 | ||
|
19 | #Stdlib imports | |
|
20 | from __future__ import print_function | |
|
21 | import sys | |
|
22 | import io | |
|
23 | import os | |
|
24 | ||
|
25 | #From IPython | |
|
26 | from IPython.config.application import Application | |
|
27 | from IPython.utils.traitlets import Bool | |
|
28 | ||
|
29 | from .exporters.export import export_by_name | |
|
30 | from .exporters.exporter import Exporter | |
|
31 | from .transformers import extractfigure | |
|
32 | from .utils.config import GlobalConfigurable | |
|
33 | ||
|
34 | #----------------------------------------------------------------------------- | |
|
35 | #Globals and constants | |
|
36 | #----------------------------------------------------------------------------- | |
|
37 | ||
|
38 | #'Keys in resources' user prompt. | |
|
39 | KEYS_PROMPT_HEAD = "====================== Keys in Resources ==================================" | |
|
40 | KEYS_PROMPT_BODY = """ | |
|
41 | =========================================================================== | |
|
42 | You are responsible for writting these files into the appropriate | |
|
43 | directorie(s) if need be. If you do not want to see this message, enable | |
|
44 | the 'write' (boolean) flag of the converter. | |
|
45 | =========================================================================== | |
|
46 | """ | |
|
47 | ||
|
48 | #----------------------------------------------------------------------------- | |
|
49 | #Classes and functions | |
|
50 | #----------------------------------------------------------------------------- | |
|
51 | ||
|
52 | class NbConvertApp(Application): | |
|
53 | """Application used to convert to and from notebook file type (*.ipynb)""" | |
|
54 | ||
|
55 | stdout = Bool( | |
|
56 | False, config=True, | |
|
57 | help="""Whether to print the converted IPYNB file to stdout | |
|
58 | use full do diff files without actually writing a new file""" | |
|
59 | ) | |
|
60 | ||
|
61 | write = Bool( | |
|
62 | True, config=True, | |
|
63 | help="""Should the converted notebook file be written to disk | |
|
64 | along with potential extracted resources.""" | |
|
65 | ) | |
|
66 | ||
|
67 | aliases = { | |
|
68 | 'stdout':'NbConvertApp.stdout', | |
|
69 | 'write':'NbConvertApp.write', | |
|
70 | } | |
|
71 | ||
|
72 | flags = {} | |
|
73 | ||
|
74 | flags['stdout'] = ( | |
|
75 | {'NbConvertApp' : {'stdout' : True}}, | |
|
76 | """Print converted file to stdout, equivalent to --stdout=True | |
|
77 | """ | |
|
78 | ) | |
|
79 | ||
|
80 | flags['no-write'] = ( | |
|
81 | {'NbConvertApp' : {'write' : True}}, | |
|
82 | """Do not write to disk, equivalent to --write=False | |
|
83 | """ | |
|
84 | ) | |
|
85 | ||
|
86 | ||
|
87 | def __init__(self, **kwargs): | |
|
88 | """Public constructor""" | |
|
89 | ||
|
90 | #Call base class | |
|
91 | super(NbConvertApp, self).__init__(**kwargs) | |
|
92 | ||
|
93 | #Register class here to have help with help all | |
|
94 | self.classes.insert(0, Exporter) | |
|
95 | self.classes.insert(0, GlobalConfigurable) | |
|
96 | ||
|
97 | ||
|
98 | def start(self, argv=None): | |
|
99 | """Entrypoint of NbConvert application. | |
|
100 | ||
|
101 | Parameters | |
|
102 | ---------- | |
|
103 | argv : list | |
|
104 | Commandline arguments | |
|
105 | """ | |
|
106 | ||
|
107 | #Parse the commandline options. | |
|
108 | self.parse_command_line(argv) | |
|
109 | ||
|
110 | #Call base | |
|
111 | super(NbConvertApp, self).start() | |
|
112 | ||
|
113 | #The last arguments in list will be used by nbconvert | |
|
114 | if len(self.extra_args) is not 3: | |
|
115 | print( "Wrong number of arguments, use --help flag for usage", file=sys.stderr) | |
|
116 | sys.exit(-1) | |
|
117 | export_type = (self.extra_args)[1] | |
|
118 | ipynb_file = (self.extra_args)[2] | |
|
119 | ||
|
120 | #Export | |
|
121 | return_value = export_by_name(export_type, ipynb_file) | |
|
122 | if return_value is None: | |
|
123 | print("Error: '%s' template not found." % export_type) | |
|
124 | return | |
|
125 | else: | |
|
126 | (output, resources, exporter) = return_value | |
|
127 | ||
|
128 | #TODO: Allow user to set output directory and file. | |
|
129 | destination_filename = None | |
|
130 | destination_directory = None | |
|
131 | if self.write: | |
|
132 | ||
|
133 | #Get the file name without the '.ipynb' (6 chars) extension and then | |
|
134 | #remove any addition periods and spaces. The resulting name will | |
|
135 | #be used to create the directory that the files will be exported | |
|
136 | #into. | |
|
137 | out_root = ipynb_file[:-6].replace('.', '_').replace(' ', '_') | |
|
138 | destination_filename = os.path.join(out_root+'.'+exporter.file_extension) | |
|
139 | ||
|
140 | destination_directory = out_root+'_files' | |
|
141 | if not os.path.exists(destination_directory): | |
|
142 | os.mkdir(destination_directory) | |
|
143 | ||
|
144 | #Write the results | |
|
145 | if self.stdout or not (destination_filename is None and destination_directory is None): | |
|
146 | self._write_results(output, resources, destination_filename, destination_directory) | |
|
147 | ||
|
148 | ||
|
149 | def _write_results(self, output, resources, destination_filename=None, destination_directory=None): | |
|
150 | """Output the conversion results to the console and/or filesystem | |
|
151 | ||
|
152 | Parameters | |
|
153 | ---------- | |
|
154 | output : str | |
|
155 | Output of conversion | |
|
156 | resources : dictionary | |
|
157 | Additional input/output used by the transformers. For | |
|
158 | example, the ExtractFigure transformer outputs the | |
|
159 | figures it extracts into this dictionary. This method | |
|
160 | relies on the figures being in this dictionary when | |
|
161 | attempting to write the figures to the file system. | |
|
162 | destination_filename : str, Optional | |
|
163 | Filename to write output into. If None, output is not | |
|
164 | written to a file. | |
|
165 | destination_directory : str, Optional | |
|
166 | Directory to write notebook data (i.e. figures) to. If | |
|
167 | None, figures are not written to the file system. | |
|
168 | """ | |
|
169 | ||
|
170 | if self.stdout: | |
|
171 | print(output.encode('utf-8')) | |
|
172 | ||
|
173 | #Write file output from conversion. | |
|
174 | if not destination_filename is None: | |
|
175 | with io.open(destination_filename, 'w') as f: | |
|
176 | f.write(output) | |
|
177 | ||
|
178 | #Get the key names used by the extract figure transformer | |
|
179 | figures_key = extractfigure.FIGURES_KEY | |
|
180 | binary_key = extractfigure.BINARY_KEY | |
|
181 | text_key = extractfigure.TEXT_KEY | |
|
182 | ||
|
183 | #Output any associate figures into the same "root" directory. | |
|
184 | binkeys = resources.get(figures_key, {}).get(binary_key,{}).keys() | |
|
185 | textkeys = resources.get(figures_key, {}).get(text_key,{}).keys() | |
|
186 | if binkeys or textkeys : | |
|
187 | if not destination_directory is None: | |
|
188 | for key in binkeys: | |
|
189 | with io.open(os.path.join(destination_directory, key), 'wb') as f: | |
|
190 | f.write(resources[figures_key][binary_key][key]) | |
|
191 | for key in textkeys: | |
|
192 | with io.open(os.path.join(destination_directory, key), 'w') as f: | |
|
193 | f.write(resources[figures_key][text_key][key]) | |
|
194 | ||
|
195 | #Figures that weren't exported which will need to be created by the | |
|
196 | #user. Tell the user what figures these are. | |
|
197 | if self.stdout: | |
|
198 | print(KEYS_PROMPT_HEAD, file=sys.stderr) | |
|
199 | print(resources[figures_key].keys(), file=sys.stderr) | |
|
200 | print(KEYS_PROMPT_BODY , file=sys.stderr) | |
|
201 | ||
|
202 | #----------------------------------------------------------------------------- | |
|
203 | # Main entry point | |
|
204 | #----------------------------------------------------------------------------- | |
|
205 | ||
|
206 | def launch_new_instance(): | |
|
207 | """Application entry point""" | |
|
208 | ||
|
209 | app = NbConvertApp.instance() | |
|
210 | app.description = __doc__ | |
|
211 | app.start(argv=sys.argv) | |
|
212 |
@@ -0,0 +1,17 b'' | |||
|
1 | {%- extends 'reveal_cells.tpl' -%} | |
|
2 | ||
|
3 | ||
|
4 | ||
|
5 | {%- block any_cell scoped -%} | |
|
6 | {%- if cell.metadata.align_type in ['Left'] -%} | |
|
7 | {{ super() }} | |
|
8 | {%- elif cell.metadata.align_type in ['center'] -%} | |
|
9 | <div style="text-align:center"> | |
|
10 | {{ super() }} | |
|
11 | </div> | |
|
12 | {%- elif cell.metadata.align_type in ['right'] -%} | |
|
13 | <div style="text-align:right"> | |
|
14 | {{ super() }} | |
|
15 | </div> | |
|
16 | {%- endif -%} | |
|
17 | {%- endblock any_cell -%} |
@@ -0,0 +1,143 b'' | |||
|
1 | {%- extends 'display_priority.tpl' -%} | |
|
2 | ||
|
3 | ||
|
4 | ||
|
5 | {% block codecell %} | |
|
6 | <div class="cell border-box-sizing code_cell vbox"> | |
|
7 | {{ super() }}</div> | |
|
8 | {%- endblock codecell %} | |
|
9 | ||
|
10 | {% block input_group -%} | |
|
11 | <div class="input hbox"> | |
|
12 | {{super()}} | |
|
13 | </div> | |
|
14 | {% endblock input_group %} | |
|
15 | ||
|
16 | {% block output_group %} | |
|
17 | <div class="vbox output_wrapper"> | |
|
18 | <div class="output vbox"> | |
|
19 | {{ super() }} | |
|
20 | </div> | |
|
21 | </div> | |
|
22 | {% endblock output_group %} | |
|
23 | ||
|
24 | {% block in_prompt -%} | |
|
25 | <div class="prompt input_prompt">In [{{cell.prompt_number}}]:</div> | |
|
26 | {%- endblock in_prompt %} | |
|
27 | ||
|
28 | {# | |
|
29 | output_prompt doesn't do anything in HTML, | |
|
30 | because there is a prompt div in each output area (see output block) | |
|
31 | #} | |
|
32 | {% block output_prompt %} | |
|
33 | {% endblock output_prompt %} | |
|
34 | ||
|
35 | {% block input %} | |
|
36 | <div class="input_area box-flex1"> | |
|
37 | {{cell.input | highlight }} | |
|
38 | </div> | |
|
39 | {%- endblock input %} | |
|
40 | ||
|
41 | {% block output %} | |
|
42 | <div class="hbox output_area"> | |
|
43 | {%- if output.output_type == 'pyout' -%} | |
|
44 | <div class="prompt output_prompt"> | |
|
45 | Out[{{cell.prompt_number}}]: | |
|
46 | {%- else -%} | |
|
47 | <div class="prompt"> | |
|
48 | {%- endif -%} | |
|
49 | </div> | |
|
50 | {{ super() }} | |
|
51 | </div> | |
|
52 | {% endblock output %} | |
|
53 | ||
|
54 | {% block markdowncell scoped %} | |
|
55 | <div class="text_cell_render border-box-sizing rendered_html"> | |
|
56 | {{ cell.source | markdown| rm_fake}} | |
|
57 | </div> | |
|
58 | {%- endblock markdowncell %} | |
|
59 | ||
|
60 | {% block headingcell scoped %} | |
|
61 | <div class="text_cell_render border-box-sizing rendered_html"> | |
|
62 | <h{{cell.level}}> | |
|
63 | {% set source = cell.source | replace(' ','_') %} | |
|
64 | <a class="heading-anchor" id="{{source}}" href="#{{source}}"> | |
|
65 | {{cell.source}} | |
|
66 | </a> | |
|
67 | </h{{cell.level}}> | |
|
68 | </div> | |
|
69 | {% endblock headingcell %} | |
|
70 | ||
|
71 | {% block rawcell scoped %} | |
|
72 | {{ cell.source }} | |
|
73 | {% endblock rawcell %} | |
|
74 | ||
|
75 | {% block unknowncell scoped %} | |
|
76 | unknown type {{cell.type}} | |
|
77 | {% endblock unknowncell %} | |
|
78 | ||
|
79 | ||
|
80 | {% block pyout -%} | |
|
81 | <div class="box-flex1 output_subarea output_pyout"> | |
|
82 | {% block data_priority scoped %}{{ super()}}{% endblock %} | |
|
83 | </div> | |
|
84 | {%- endblock pyout %} | |
|
85 | ||
|
86 | {% block stream_stdout -%} | |
|
87 | <div class="box-flex1 output_subarea output_stream output_stdout"> | |
|
88 | <pre>{{output.text |ansi2html}}</pre> | |
|
89 | </div> | |
|
90 | {%- endblock stream_stdout %} | |
|
91 | ||
|
92 | {% block stream_stderr -%} | |
|
93 | <div class="box-flex1 output_subarea output_stream output_stderr"> | |
|
94 | <pre>{{output.text |ansi2html}}</pre> | |
|
95 | </div> | |
|
96 | {%- endblock stream_stderr %} | |
|
97 | ||
|
98 | {% block data_svg -%} | |
|
99 | {{output.svg}} | |
|
100 | {%- endblock data_svg %} | |
|
101 | ||
|
102 | ||
|
103 | {% block data_html -%} | |
|
104 | <div class="output_html rendered_html"> | |
|
105 | {{output.html}} | |
|
106 | </div> | |
|
107 | {%- endblock data_html %} | |
|
108 | ||
|
109 | {% block data_png %} | |
|
110 | <img src="data:image/png;base64,{{output.png}}"> | |
|
111 | {%- endblock data_png %} | |
|
112 | ||
|
113 | ||
|
114 | {% block data_jpg %} | |
|
115 | <img src="data:image/jpeg;base64,{{output.jpeg}}"> | |
|
116 | {%- endblock data_jpg %} | |
|
117 | ||
|
118 | ||
|
119 | {% block data_latex %} | |
|
120 | {{output.latex}} | |
|
121 | {%- endblock data_latex %} | |
|
122 | ||
|
123 | {% block pyerr -%} | |
|
124 | <div class="box-flex1 output_subarea output_pyerr"> | |
|
125 | <pre>{{super()}}</pre> | |
|
126 | </div> | |
|
127 | {%- endblock pyerr %} | |
|
128 | ||
|
129 | {%- block traceback_line %} | |
|
130 | {{line| ansi2html}} | |
|
131 | {%- endblock traceback_line %} | |
|
132 | ||
|
133 | ||
|
134 | {%- block data_text %} | |
|
135 | <pre>{{output.text | ansi2html}}</pre> | |
|
136 | {%- endblock -%} | |
|
137 | ||
|
138 | ||
|
139 | {%- block display_data scoped -%} | |
|
140 | <div class="box-flex1 output_subarea output_display_data"> | |
|
141 | {{super()}} | |
|
142 | </div> | |
|
143 | {%- endblock display_data -%} |
@@ -0,0 +1,64 b'' | |||
|
1 | {%- extends 'basichtml.tpl' -%} | |
|
2 | ||
|
3 | {%- block header -%}<!DOCTYPE html> | |
|
4 | <html> | |
|
5 | <head> | |
|
6 | <meta charset="UTF-8"> | |
|
7 | <title>[{{nb.metadata.name}}]</title> | |
|
8 | {% for css in resources.inlining.css -%} | |
|
9 | <style type="text/css"> | |
|
10 | {{css}} | |
|
11 | </style> | |
|
12 | {% endfor %} | |
|
13 | ||
|
14 | <style type="text/css"> | |
|
15 | /* Overrides of notebook CSS for static HTML export */ | |
|
16 | body { | |
|
17 | overflow: visible; | |
|
18 | padding: 8px; | |
|
19 | } | |
|
20 | .input_area { | |
|
21 | padding: 0.2em; | |
|
22 | } | |
|
23 | ||
|
24 | pre { | |
|
25 | border: none; | |
|
26 | margin: 0px; | |
|
27 | font-size: 13px; | |
|
28 | } | |
|
29 | </style> | |
|
30 | ||
|
31 | <script src="https://c328740.ssl.cf1.rackcdn.com/mathjax/latest/MathJax.js?config=TeX-AMS_HTML" type="text/javascript"> | |
|
32 | ||
|
33 | </script> | |
|
34 | <script type="text/javascript"> | |
|
35 | init_mathjax = function() { | |
|
36 | if (window.MathJax) { | |
|
37 | // MathJax loaded | |
|
38 | MathJax.Hub.Config({ | |
|
39 | tex2jax: { | |
|
40 | inlineMath: [ ['$','$'], ["\\(","\\)"] ], | |
|
41 | displayMath: [ ['$$','$$'], ["\\[","\\]"] ] | |
|
42 | }, | |
|
43 | displayAlign: 'left', // Change this to 'center' to center equations. | |
|
44 | "HTML-CSS": { | |
|
45 | styles: {'.MathJax_Display': {"margin": 0}} | |
|
46 | } | |
|
47 | }); | |
|
48 | MathJax.Hub.Queue(["Typeset",MathJax.Hub]); | |
|
49 | } | |
|
50 | } | |
|
51 | init_mathjax(); | |
|
52 | </script> | |
|
53 | </head> | |
|
54 | {%- endblock header -%} | |
|
55 | ||
|
56 | ||
|
57 | {% block body %} | |
|
58 | <body>{{ super() }} | |
|
59 | </body> | |
|
60 | {%- endblock body %} | |
|
61 | ||
|
62 | ||
|
63 | {% block footer %} | |
|
64 | </html>{% endblock footer %} |
@@ -0,0 +1,263 b'' | |||
|
1 | ((*- extends 'display_priority.tplx' -*)) | |
|
2 | ||
|
3 | ((* block in_prompt *))((* endblock in_prompt *)) | |
|
4 | ||
|
5 | ((* block output_prompt *))((* endblock output_prompt *)) | |
|
6 | ||
|
7 | ((* block codecell *))\begin{codecell}((( super() ))) | |
|
8 | \end{codecell} | |
|
9 | ((* endblock *)) | |
|
10 | ||
|
11 | ((* block input *)) | |
|
12 | \begin{codeinput} | |
|
13 | \begin{lstlisting} | |
|
14 | ((( cell.input ))) | |
|
15 | \end{lstlisting} | |
|
16 | \end{codeinput} | |
|
17 | ((* endblock input *)) | |
|
18 | ||
|
19 | ||
|
20 | ((= Those Two are for error displaying | |
|
21 | even if the first one seem to do nothing, | |
|
22 | it introduces a new line | |
|
23 | ||
|
24 | =)) | |
|
25 | ((* block pyerr *)) | |
|
26 | \begin{traceback} | |
|
27 | \begin{verbatim}((( super() ))) | |
|
28 | \end{verbatim} | |
|
29 | \end{traceback} | |
|
30 | ((* endblock pyerr *)) | |
|
31 | ||
|
32 | ((* block traceback_line *)) | |
|
33 | ((( line |indent| rm_ansi )))((* endblock traceback_line *)) | |
|
34 | ((= .... =)) | |
|
35 | ||
|
36 | ||
|
37 | ((*- block output_group -*)) | |
|
38 | \begin{codeoutput} | |
|
39 | ((( super() ))) | |
|
40 | \end{codeoutput}((* endblock *)) | |
|
41 | ||
|
42 | ((*- block data_png -*)) | |
|
43 | \begin{center} | |
|
44 | \includegraphics[width=0.7\textwidth, height=0.9\textheight, keepaspectratio]{(((output.key_png)))} | |
|
45 | \par | |
|
46 | \end{center} | |
|
47 | ((*- endblock -*)) | |
|
48 | ||
|
49 | ((*- block data_jpg -*)) | |
|
50 | \begin{center} | |
|
51 | \includegraphics[width=0.7\textwidth, height=0.9\textheight, keepaspectratio]{(((output.key_jpeg)))} | |
|
52 | \par | |
|
53 | \end{center} | |
|
54 | ((*- endblock -*)) | |
|
55 | ||
|
56 | ((*- block data_svg -*)) | |
|
57 | \begin{center} | |
|
58 | \includegraphics[width=0.7\textwidth]{(((output.key_svg)))} | |
|
59 | \par | |
|
60 | \end{center} | |
|
61 | ((*- endblock -*)) | |
|
62 | ||
|
63 | ((* block pyout *)) | |
|
64 | ((* block data_priority scoped *))((( super() )))((* endblock *)) | |
|
65 | ((* endblock pyout *)) | |
|
66 | ||
|
67 | ((* block data_text *)) | |
|
68 | \begin{verbatim} | |
|
69 | ((( output.text ))) | |
|
70 | \end{verbatim} | |
|
71 | ((* endblock *)) | |
|
72 | ||
|
73 | ((* block data_latex -*)) | |
|
74 | ((*- if output.latex.startswith('$'): -*)) \begin{equation*} | |
|
75 | ((( output.latex | rm_dollars))) | |
|
76 | \end{equation*} | |
|
77 | ((*- else -*)) ((( output.latex ))) ((*- endif *)) | |
|
78 | ((* endblock *)) | |
|
79 | ||
|
80 | ((* block stream *)) | |
|
81 | \begin{Verbatim}[commandchars=\\\{\}] | |
|
82 | ((( output.text | ansi2latex))) | |
|
83 | \end{Verbatim} | |
|
84 | ((* endblock stream *)) | |
|
85 | ||
|
86 | ((* block markdowncell scoped *))((( cell.source | markdown2latex ))) | |
|
87 | ((* endblock markdowncell *)) | |
|
88 | ||
|
89 | ((* block headingcell scoped -*)) | |
|
90 | \ | |
|
91 | ((*- if cell.level == 1 -*)) | |
|
92 | ((* block h1 -*))section((* endblock h1 -*)) | |
|
93 | ((*- elif cell.level == 2 -*)) | |
|
94 | ((* block h2 -*))subsection((* endblock h2 -*)) | |
|
95 | ((*- elif cell.level == 3 -*)) | |
|
96 | ((* block h3 -*))subsubsection((* endblock h3 -*)) | |
|
97 | ((*- elif cell.level == 4 -*)) | |
|
98 | ((* block h4 -*))paragraph((* endblock h4 -*)) | |
|
99 | ((*- elif cell.level == 5 -*)) | |
|
100 | ((* block h5 -*))subparagraph((* endblock h5 -*)) | |
|
101 | ((*- elif cell.level == 6 -*)) | |
|
102 | ((* block h6 -*))subparagraph((* endblock h6 -*)) | |
|
103 | ((= 6th level not available in standard latex =)) | |
|
104 | ||
|
105 | ((*- endif -*)){((( cell.source | markdown2latex )))} | |
|
106 | ((* endblock headingcell *)) | |
|
107 | ||
|
108 | ((* block rawcell scoped *)) | |
|
109 | ((( cell.source | pycomment ))) | |
|
110 | ((* endblock rawcell *)) | |
|
111 | ||
|
112 | ((* block unknowncell scoped *)) | |
|
113 | unknown type (((cell.type))) | |
|
114 | ((* endblock unknowncell *)) | |
|
115 | ||
|
116 | ||
|
117 | ||
|
118 | ((* block body *)) | |
|
119 | ||
|
120 | ((* block bodyBegin *)) | |
|
121 | \begin{document} | |
|
122 | ((* endblock bodyBegin *)) | |
|
123 | ||
|
124 | ((( super() ))) | |
|
125 | ||
|
126 | ((* block bodyEnd *)) | |
|
127 | \end{document} | |
|
128 | ((* endblock bodyEnd *)) | |
|
129 | ((* endblock body *)) | |
|
130 | ||
|
131 | ((* block header *)) | |
|
132 | %% This file was auto-generated by IPython. | |
|
133 | %% Conversion from the original notebook file: | |
|
134 | %% | |
|
135 | \documentclass[11pt,english]{article} | |
|
136 | ||
|
137 | %% This is the automatic preamble used by IPython. Note that it does *not* | |
|
138 | %% include a documentclass declaration, that is added at runtime to the overall | |
|
139 | %% document. | |
|
140 | ||
|
141 | \usepackage{amsmath} | |
|
142 | \usepackage{amssymb} | |
|
143 | \usepackage{graphicx} | |
|
144 | \usepackage{ucs} | |
|
145 | \usepackage[utf8x]{inputenc} | |
|
146 | ||
|
147 | %fancy verbatim | |
|
148 | \usepackage{fancyvrb} | |
|
149 | % needed for markdown enumerations to work | |
|
150 | \usepackage{enumerate} | |
|
151 | ||
|
152 | % Slightly bigger margins than the latex defaults | |
|
153 | \usepackage{geometry} | |
|
154 | \geometry{verbose,tmargin=3cm,bmargin=3cm,lmargin=2.5cm,rmargin=2.5cm} | |
|
155 | ||
|
156 | % Define a few colors for use in code, links and cell shading | |
|
157 | \usepackage{color} | |
|
158 | \definecolor{orange}{cmyk}{0,0.4,0.8,0.2} | |
|
159 | \definecolor{darkorange}{rgb}{.71,0.21,0.01} | |
|
160 | \definecolor{darkgreen}{rgb}{.12,.54,.11} | |
|
161 | \definecolor{myteal}{rgb}{.26, .44, .56} | |
|
162 | \definecolor{gray}{gray}{0.45} | |
|
163 | \definecolor{lightgray}{gray}{.95} | |
|
164 | \definecolor{mediumgray}{gray}{.8} | |
|
165 | \definecolor{inputbackground}{rgb}{.95, .95, .85} | |
|
166 | \definecolor{outputbackground}{rgb}{.95, .95, .95} | |
|
167 | \definecolor{traceback}{rgb}{1, .95, .95} | |
|
168 | ||
|
169 | % new ansi colors | |
|
170 | \definecolor{brown}{rgb}{0.54,0.27,0.07} | |
|
171 | \definecolor{purple}{rgb}{0.5,0.0,0.5} | |
|
172 | \definecolor{darkgray}{gray}{0.25} | |
|
173 | \definecolor{lightred}{rgb}{1.0,0.39,0.28} | |
|
174 | \definecolor{lightgreen}{rgb}{0.48,0.99,0.0} | |
|
175 | \definecolor{lightblue}{rgb}{0.53,0.81,0.92} | |
|
176 | \definecolor{lightpurple}{rgb}{0.87,0.63,0.87} | |
|
177 | \definecolor{lightcyan}{rgb}{0.5,1.0,0.83} | |
|
178 | ||
|
179 | % Framed environments for code cells (inputs, outputs, errors, ...). The | |
|
180 | % various uses of \unskip (or not) at the end were fine-tuned by hand, so don't | |
|
181 | % randomly change them unless you're sure of the effect it will have. | |
|
182 | \usepackage{framed} | |
|
183 | ||
|
184 | % remove extraneous vertical space in boxes | |
|
185 | \setlength\fboxsep{0pt} | |
|
186 | ||
|
187 | % codecell is the whole input+output set of blocks that a Code cell can | |
|
188 | % generate. | |
|
189 | ||
|
190 | % TODO: unfortunately, it seems that using a framed codecell environment breaks | |
|
191 | % the ability of the frames inside of it to be broken across pages. This | |
|
192 | % causes at least the problem of having lots of empty space at the bottom of | |
|
193 | % pages as new frames are moved to the next page, and if a single frame is too | |
|
194 | % long to fit on a page, will completely stop latex from compiling the | |
|
195 | % document. So unless we figure out a solution to this, we'll have to instead | |
|
196 | % leave the codecell env. as empty. I'm keeping the original codecell | |
|
197 | % definition here (a thin vertical bar) for reference, in case we find a | |
|
198 | % solution to the page break issue. | |
|
199 | ||
|
200 | %% \newenvironment{codecell}{% | |
|
201 | %% \def\FrameCommand{\color{mediumgray} \vrule width 1pt \hspace{5pt}}% | |
|
202 | %% \MakeFramed{\vspace{-0.5em}}} | |
|
203 | %% {\unskip\endMakeFramed} | |
|
204 | ||
|
205 | % For now, make this a no-op... | |
|
206 | \newenvironment{codecell}{} | |
|
207 | ||
|
208 | \newenvironment{codeinput}{% | |
|
209 | \def\FrameCommand{\colorbox{inputbackground}}% | |
|
210 | \MakeFramed{\advance\hsize-\width \FrameRestore}} | |
|
211 | {\unskip\endMakeFramed} | |
|
212 | ||
|
213 | \newenvironment{codeoutput}{% | |
|
214 | \def\FrameCommand{\colorbox{outputbackground}}% | |
|
215 | \vspace{-1.4em} | |
|
216 | \MakeFramed{\advance\hsize-\width \FrameRestore}} | |
|
217 | {\unskip\medskip\endMakeFramed} | |
|
218 | ||
|
219 | \newenvironment{traceback}{% | |
|
220 | \def\FrameCommand{\colorbox{traceback}}% | |
|
221 | \MakeFramed{\advance\hsize-\width \FrameRestore}} | |
|
222 | {\endMakeFramed} | |
|
223 | ||
|
224 | % Use and configure listings package for nicely formatted code | |
|
225 | \usepackage{listingsutf8} | |
|
226 | \lstset{ | |
|
227 | language=python, | |
|
228 | inputencoding=utf8x, | |
|
229 | extendedchars=\true, | |
|
230 | aboveskip=\smallskipamount, | |
|
231 | belowskip=\smallskipamount, | |
|
232 | xleftmargin=2mm, | |
|
233 | breaklines=true, | |
|
234 | basicstyle=\small \ttfamily, | |
|
235 | showstringspaces=false, | |
|
236 | keywordstyle=\color{blue}\bfseries, | |
|
237 | commentstyle=\color{myteal}, | |
|
238 | stringstyle=\color{darkgreen}, | |
|
239 | identifierstyle=\color{darkorange}, | |
|
240 | columns=fullflexible, % tighter character kerning, like verb | |
|
241 | } | |
|
242 | ||
|
243 | % The hyperref package gives us a pdf with properly built | |
|
244 | % internal navigation ('pdf bookmarks' for the table of contents, | |
|
245 | % internal cross-reference links, web links for URLs, etc.) | |
|
246 | \usepackage{hyperref} | |
|
247 | \hypersetup{ | |
|
248 | breaklinks=true, % so long urls are correctly broken across lines | |
|
249 | colorlinks=true, | |
|
250 | urlcolor=blue, | |
|
251 | linkcolor=darkorange, | |
|
252 | citecolor=darkgreen, | |
|
253 | } | |
|
254 | ||
|
255 | % hardcode size of all verbatim environments to be a bit smaller | |
|
256 | \makeatletter | |
|
257 | \g@addto@macro\@verbatim\small\topsep=0.5em\partopsep=0pt | |
|
258 | \makeatother | |
|
259 | ||
|
260 | % Prevent overflowing lines due to urls and other hard-to-break entities. | |
|
261 | \sloppy | |
|
262 | ||
|
263 | ((* endblock *)) |
@@ -0,0 +1,39 b'' | |||
|
1 | ((= autogenerated file do not edit =)) | |
|
2 | ((*- extends 'null.tplx' -*)) | |
|
3 | ||
|
4 | ((=display data priority=)) | |
|
5 | ||
|
6 | ||
|
7 | ((*- block data_priority scoped -*)) | |
|
8 | ((*- for type in output | filter_data_type -*)) | |
|
9 | ((*- if type in ['pdf']*)) | |
|
10 | ((*- block data_pdf -*)) | |
|
11 | ((*- endblock -*)) | |
|
12 | ((*- endif -*)) | |
|
13 | ((*- if type in ['svg']*)) | |
|
14 | ((*- block data_svg -*)) | |
|
15 | ((*- endblock -*)) | |
|
16 | ((*- endif -*)) | |
|
17 | ((*- if type in ['png']*)) | |
|
18 | ((*- block data_png -*)) | |
|
19 | ((*- endblock -*)) | |
|
20 | ((*- endif -*)) | |
|
21 | ((*- if type in ['html']*)) | |
|
22 | ((*- block data_html -*)) | |
|
23 | ((*- endblock -*)) | |
|
24 | ((*- endif -*)) | |
|
25 | ((*- if type in ['jpeg']*)) | |
|
26 | ((*- block data_jpg -*)) | |
|
27 | ((*- endblock -*)) | |
|
28 | ((*- endif -*)) | |
|
29 | ((*- if type in ['text']*)) | |
|
30 | ((*- block data_text -*)) | |
|
31 | ((*- endblock -*)) | |
|
32 | ((*- endif -*)) | |
|
33 | ||
|
34 | ((*- if type in ['latex']*)) | |
|
35 | ((*- block data_latex -*)) | |
|
36 | ((*- endblock -*)) | |
|
37 | ((*- endif -*)) | |
|
38 | ((*- endfor -*)) | |
|
39 | ((*- endblock data_priority -*)) |
@@ -0,0 +1,92 b'' | |||
|
1 | ((= autogenerated file do not edit =)) | |
|
2 | ((= | |
|
3 | ||
|
4 | DO NOT USE THIS AS A BASE WORK, | |
|
5 | IF YOU ARE COPY AND PASTING THIS FILE | |
|
6 | YOU ARE PROBABLY DOING THINGS WRONG. | |
|
7 | ||
|
8 | Null template, Does nothing except defining a basic structure | |
|
9 | To layout the different blocks of a notebook. | |
|
10 | ||
|
11 | Subtemplates can override blocks to define their custom representation. | |
|
12 | ||
|
13 | If one of the block you do overwrite is not a leave block, consider | |
|
14 | calling super. | |
|
15 | ||
|
16 | ((*- block nonLeaveBlock -*)) | |
|
17 | #add stuff at beginning | |
|
18 | ((( super() ))) | |
|
19 | #add stuff at end | |
|
20 | ((*- endblock nonLeaveBlock -*)) | |
|
21 | ||
|
22 | consider calling super even if it is a leave block, we might insert more blocks later. | |
|
23 | ||
|
24 | =)) | |
|
25 | ((*- block header -*)) | |
|
26 | ((*- endblock header -*)) | |
|
27 | ((*- block body -*)) | |
|
28 | ((*- for worksheet in nb.worksheets -*)) | |
|
29 | ((*- for cell in worksheet.cells -*)) | |
|
30 | ((*- block any_cell scoped -*)) | |
|
31 | ((*- if cell.cell_type in ['code'] -*)) | |
|
32 | ((*- block codecell scoped -*)) | |
|
33 | ((*- block input_group -*)) | |
|
34 | ((*- block in_prompt -*))((*- endblock in_prompt -*)) | |
|
35 | ((*- block input -*))((*- endblock input -*)) | |
|
36 | ((*- endblock input_group -*)) | |
|
37 | ((*- if cell.outputs -*)) | |
|
38 | ((*- block output_group -*)) | |
|
39 | ((*- block output_prompt -*))((*- endblock output_prompt -*)) | |
|
40 | ((*- block outputs scoped -*)) | |
|
41 | ((*- for output in cell.outputs -*)) | |
|
42 | ((*- block output scoped -*)) | |
|
43 | ((*- if output.output_type in ['pyout'] -*)) | |
|
44 | ((*- block pyout scoped -*))((*- endblock pyout -*)) | |
|
45 | ((*- elif output.output_type in ['stream'] -*)) | |
|
46 | ((*- block stream scoped -*)) | |
|
47 | ((*- if output.stream in ['stdout'] -*)) | |
|
48 | ((*- block stream_stdout scoped -*)) | |
|
49 | ((*- endblock stream_stdout -*)) | |
|
50 | ((*- elif output.stream in ['stderr'] -*)) | |
|
51 | ((*- block stream_stderr scoped -*)) | |
|
52 | ((*- endblock stream_stderr -*)) | |
|
53 | ((*- endif -*)) | |
|
54 | ((*- endblock stream -*)) | |
|
55 | ((*- elif output.output_type in ['display_data'] -*)) | |
|
56 | ((*- block display_data scoped -*)) | |
|
57 | ((*- block data_priority scoped -*)) | |
|
58 | ((*- endblock data_priority -*)) | |
|
59 | ((*- endblock display_data -*)) | |
|
60 | ((*- elif output.output_type in ['pyerr'] -*)) | |
|
61 | ((*- block pyerr scoped -*)) | |
|
62 | ((*- for line in output.traceback -*)) | |
|
63 | ((*- block traceback_line scoped -*))((*- endblock traceback_line -*)) | |
|
64 | ((*- endfor -*)) | |
|
65 | ((*- endblock pyerr -*)) | |
|
66 | ((*- endif -*)) | |
|
67 | ((*- endblock output -*)) | |
|
68 | ((*- endfor -*)) | |
|
69 | ((*- endblock outputs -*)) | |
|
70 | ((*- endblock output_group -*)) | |
|
71 | ((*- endif -*)) | |
|
72 | ((*- endblock codecell -*)) | |
|
73 | ((*- elif cell.cell_type in ['markdown'] -*)) | |
|
74 | ((*- block markdowncell scoped-*)) | |
|
75 | ((*- endblock markdowncell -*)) | |
|
76 | ((*- elif cell.cell_type in ['heading'] -*)) | |
|
77 | ((*- block headingcell scoped-*)) | |
|
78 | ((*- endblock headingcell -*)) | |
|
79 | ((*- elif cell.cell_type in ['raw'] -*)) | |
|
80 | ((*- block rawcell scoped-*)) | |
|
81 | ((*- endblock rawcell -*)) | |
|
82 | ((*- else -*)) | |
|
83 | ((*- block unknowncell scoped-*)) | |
|
84 | ((*- endblock unknowncell -*)) | |
|
85 | ((*- endif -*)) | |
|
86 | ((*- endblock any_cell -*)) | |
|
87 | ((*- endfor -*)) | |
|
88 | ((*- endfor -*)) | |
|
89 | ((*- endblock body -*)) | |
|
90 | ||
|
91 | ((*- block footer -*)) | |
|
92 | ((*- endblock footer -*)) |
@@ -0,0 +1,442 b'' | |||
|
1 | ((= NBConvert Sphinx-Latex Template | |
|
2 | ||
|
3 | Purpose: Allow export of PDF friendly Latex inspired by Sphinx. Most of the | |
|
4 | template is derived directly from Sphinx source. | |
|
5 | ||
|
6 | Inheritance: null>display_priority | |
|
7 | ||
|
8 | Note: For best display, use latex syntax highlighting. =)) | |
|
9 | ||
|
10 | ((*- extends 'display_priority.tplx' -*)) | |
|
11 | ||
|
12 | %============================================================================== | |
|
13 | % Declarations | |
|
14 | %============================================================================== | |
|
15 | ||
|
16 | % In order to make sure that the input/output header follows the code it | |
|
17 | % preceeds, the needspace package is used to request that a certain | |
|
18 | % amount of lines (specified by this variable) are reserved. If those | |
|
19 | % lines aren't available on the current page, the documenter will break | |
|
20 | % to the next page and the header along with accomanying lines will be | |
|
21 | % rendered together. This value specifies the number of lines that | |
|
22 | % the header will be forced to group with without a page break. | |
|
23 | ((*- set min_header_lines = 4 -*)) | |
|
24 | ||
|
25 | % This is the number of characters that are permitted per line. It's | |
|
26 | % important that this limit is set so characters do not run off the | |
|
27 | % edges of latex pages (since latex does not always seem smart enough | |
|
28 | % to prevent this in some cases.) This is only applied to textual output | |
|
29 | ((* if resources.sphinx.outputstyle == 'simple' *)) | |
|
30 | ((*- set wrap_size = 85 -*)) | |
|
31 | ((* elif resources.sphinx.outputstyle == 'notebook' *)) | |
|
32 | ((*- set wrap_size = 70 -*)) | |
|
33 | ((* endif *)) | |
|
34 | ||
|
35 | %============================================================================== | |
|
36 | % Header | |
|
37 | %============================================================================== | |
|
38 | ((* block header *)) | |
|
39 | ||
|
40 | % Header, overrides base | |
|
41 | ||
|
42 | % Make sure that the sphinx doc style knows who it inherits from. | |
|
43 | \def\sphinxdocclass{(((parentdocumentclass)))} | |
|
44 | ||
|
45 | % Declare the document class | |
|
46 | \documentclass[letterpaper,10pt,english]{((( resources.sphinx.texinputs )))/sphinx(((documentclass)))} | |
|
47 | ||
|
48 | % Imports | |
|
49 | \usepackage[utf8]{inputenc} | |
|
50 | \DeclareUnicodeCharacter{00A0}{\\nobreakspace} | |
|
51 | \usepackage[T1]{fontenc} | |
|
52 | \usepackage{babel} | |
|
53 | \usepackage{times} | |
|
54 | \usepackage{import} | |
|
55 | \usepackage[((( resources.sphinx.chapterstyle )))]{((( resources.sphinx.texinputs )))/fncychap} | |
|
56 | \usepackage{longtable} | |
|
57 | \usepackage{((( resources.sphinx.texinputs )))/sphinx} | |
|
58 | \usepackage{multirow} | |
|
59 | ||
|
60 | \usepackage{amsmath} | |
|
61 | \usepackage{amssymb} | |
|
62 | \usepackage{ucs} | |
|
63 | \usepackage{enumerate} | |
|
64 | ||
|
65 | % Used to make the Input/Output rules follow around the contents. | |
|
66 | \usepackage{needspace} | |
|
67 | ||
|
68 | % Pygments requirements | |
|
69 | \usepackage{fancyvrb} | |
|
70 | \usepackage{color} | |
|
71 | % ansi colors additions | |
|
72 | \definecolor{darkgreen}{rgb}{.12,.54,.11} | |
|
73 | \definecolor{lightgray}{gray}{.95} | |
|
74 | \definecolor{brown}{rgb}{0.54,0.27,0.07} | |
|
75 | \definecolor{purple}{rgb}{0.5,0.0,0.5} | |
|
76 | \definecolor{darkgray}{gray}{0.25} | |
|
77 | \definecolor{lightred}{rgb}{1.0,0.39,0.28} | |
|
78 | \definecolor{lightgreen}{rgb}{0.48,0.99,0.0} | |
|
79 | \definecolor{lightblue}{rgb}{0.53,0.81,0.92} | |
|
80 | \definecolor{lightpurple}{rgb}{0.87,0.63,0.87} | |
|
81 | \definecolor{lightcyan}{rgb}{0.5,1.0,0.83} | |
|
82 | ||
|
83 | % Needed to box output/input | |
|
84 | \usepackage{tikz} | |
|
85 | \usetikzlibrary{calc,arrows,shadows} | |
|
86 | \usepackage[framemethod=tikz]{mdframed} | |
|
87 | ||
|
88 | \usepackage{alltt} | |
|
89 | ||
|
90 | % Used to load and display graphics | |
|
91 | \usepackage{graphicx} | |
|
92 | \graphicspath{ {figs/} } | |
|
93 | \usepackage[Export]{adjustbox} % To resize | |
|
94 | ||
|
95 | ||
|
96 | % For formatting output while also word wrapping. | |
|
97 | \usepackage{listings} | |
|
98 | \lstset{breaklines=true} | |
|
99 | \lstset{basicstyle=\small\ttfamily} | |
|
100 | \def\smaller{\fontsize{9.5pt}{9.5pt}\selectfont} | |
|
101 | ||
|
102 | %Pygments definitions | |
|
103 | ((( resources.sphinx.pygment_definitions ))) | |
|
104 | ||
|
105 | %Set pygments styles if needed... | |
|
106 | ((* if resources.sphinx.outputstyle == 'notebook' *)) | |
|
107 | \definecolor{nbframe-border}{rgb}{0.867,0.867,0.867} | |
|
108 | \definecolor{nbframe-bg}{rgb}{0.969,0.969,0.969} | |
|
109 | \definecolor{nbframe-in-prompt}{rgb}{0.0,0.0,0.502} | |
|
110 | \definecolor{nbframe-out-prompt}{rgb}{0.545,0.0,0.0} | |
|
111 | ||
|
112 | \newenvironment{ColorVerbatim} | |
|
113 | {\begin{mdframed}[% | |
|
114 | roundcorner=1.0pt, % | |
|
115 | backgroundcolor=nbframe-bg, % | |
|
116 | userdefinedwidth=1\linewidth, % | |
|
117 | leftmargin=0.1\linewidth, % | |
|
118 | innerleftmargin=0pt, % | |
|
119 | innerrightmargin=0pt, % | |
|
120 | linecolor=nbframe-border, % | |
|
121 | linewidth=1pt, % | |
|
122 | usetwoside=false, % | |
|
123 | everyline=true, % | |
|
124 | innerlinewidth=3pt, % | |
|
125 | innerlinecolor=nbframe-bg, % | |
|
126 | middlelinewidth=1pt, % | |
|
127 | middlelinecolor=nbframe-bg, % | |
|
128 | outerlinewidth=0.5pt, % | |
|
129 | outerlinecolor=nbframe-border, % | |
|
130 | needspace=0pt | |
|
131 | ]} | |
|
132 | {\end{mdframed}} | |
|
133 | ||
|
134 | \newenvironment{InvisibleVerbatim} | |
|
135 | {\begin{mdframed}[leftmargin=0.1\linewidth,innerleftmargin=3pt,innerrightmargin=3pt, userdefinedwidth=1\linewidth, linewidth=0pt, linecolor=white, usetwoside=false]} | |
|
136 | {\end{mdframed}} | |
|
137 | ||
|
138 | \renewenvironment{Verbatim}[1][\unskip] | |
|
139 | {\begin{alltt}\smaller} | |
|
140 | {\end{alltt}} | |
|
141 | ((* endif *)) | |
|
142 | ||
|
143 | % Help prevent overflowing lines due to urls and other hard-to-break | |
|
144 | % entities. This doesn't catch everything... | |
|
145 | \sloppy | |
|
146 | ||
|
147 | % Document level variables | |
|
148 | \title{((( nb.metadata.name | escape_tex )))} | |
|
149 | \date{((( nb.metadata._draft.date | escape_tex )))} | |
|
150 | \release{((( nb.metadata._draft.version | escape_tex )))} | |
|
151 | \author{((( nb.metadata._draft.author | escape_tex )))} | |
|
152 | \renewcommand{\releasename}{((( nb.metadata._draft.release | escape_tex )))} | |
|
153 | ||
|
154 | % TODO: Add option for the user to specify a logo for his/her export. | |
|
155 | \newcommand{\sphinxlogo}{} | |
|
156 | ||
|
157 | % Make the index page of the document. | |
|
158 | \makeindex | |
|
159 | ||
|
160 | % Import sphinx document type specifics. | |
|
161 | ((* block sphinxheader *))((* endblock sphinxheader *)) | |
|
162 | ((* endblock header *)) | |
|
163 | ||
|
164 | %============================================================================== | |
|
165 | % Body | |
|
166 | %============================================================================== | |
|
167 | ((* block body *)) | |
|
168 | ((* block bodyBegin *)) | |
|
169 | % Body | |
|
170 | ||
|
171 | % Start of the document | |
|
172 | \begin{document} | |
|
173 | ||
|
174 | ((* if resources.sphinx.header *)) | |
|
175 | \maketitle | |
|
176 | ((* endif *)) | |
|
177 | ||
|
178 | ((* block toc *)) | |
|
179 | \tableofcontents | |
|
180 | ((* endblock toc *)) | |
|
181 | ||
|
182 | ((* endblock bodyBegin *))((( super() )))((* block bodyEnd *)) | |
|
183 | ||
|
184 | \renewcommand{\indexname}{Index} | |
|
185 | \printindex | |
|
186 | ||
|
187 | % End of document | |
|
188 | \end{document} | |
|
189 | ((* endblock bodyEnd *)) | |
|
190 | ((* endblock body *)) | |
|
191 | ||
|
192 | %============================================================================== | |
|
193 | % Footer | |
|
194 | %============================================================================== | |
|
195 | ((* block footer *)) | |
|
196 | ((* endblock footer *)) | |
|
197 | ||
|
198 | %============================================================================== | |
|
199 | % Headings | |
|
200 | % | |
|
201 | % Purpose: Format pynb headers as sphinx headers. Depending on the Sphinx | |
|
202 | % style that is active, this will change. Thus sphinx styles will | |
|
203 | % override the values here. | |
|
204 | %============================================================================== | |
|
205 | ((* block headingcell -*)) | |
|
206 | \ | |
|
207 | ((*- if cell.level == 1 -*)) | |
|
208 | ((* block h1 -*))part((* endblock h1 -*)) | |
|
209 | ((*- elif cell.level == 2 -*)) | |
|
210 | ((* block h2 -*))chapter((* endblock h2 -*)) | |
|
211 | ((*- elif cell.level == 3 -*)) | |
|
212 | ((* block h3 -*))section((* endblock h3 -*)) | |
|
213 | ((*- elif cell.level == 4 -*)) | |
|
214 | ((* block h4 -*))subsection((* endblock h4 -*)) | |
|
215 | ((*- elif cell.level == 5 -*)) | |
|
216 | ((* block h5 -*))subsubsection((* endblock h5 -*)) | |
|
217 | ((*- elif cell.level == 6 -*)) | |
|
218 | ((* block h6 -*))paragraph((* endblock h6 -*)) | |
|
219 | ||
|
220 | ((= It's important to make sure that underscores (which tend to be common | |
|
221 | in IPYNB file titles) do not make their way into latex. Sometimes this | |
|
222 | causes latex to barf. =)) | |
|
223 | ((*- endif -*)){((( cell.source | markdown2latex )))} | |
|
224 | ((*- endblock headingcell *)) | |
|
225 | ||
|
226 | %============================================================================== | |
|
227 | % Markdown | |
|
228 | % | |
|
229 | % Purpose: Convert markdown to latex. Here markdown2latex is explicitly | |
|
230 | % called since we know we want latex output. | |
|
231 | %============================================================================== | |
|
232 | ((*- block markdowncell scoped-*)) | |
|
233 | ((( cell.source | markdown2latex ))) | |
|
234 | ((*- endblock markdowncell -*)) | |
|
235 | ||
|
236 | %============================================================================== | |
|
237 | % Rawcell | |
|
238 | % | |
|
239 | % Purpose: Raw text cells allow the user to manually inject document code that | |
|
240 | % will not get touched by the templating system. | |
|
241 | %============================================================================== | |
|
242 | ((*- block rawcell *)) | |
|
243 | ((( cell.source | wrap(wrap_size) ))) | |
|
244 | ((* endblock rawcell -*)) | |
|
245 | ||
|
246 | %============================================================================== | |
|
247 | % Unknowncell | |
|
248 | % | |
|
249 | % Purpose: This is the catch anything unhandled. To display this data, we | |
|
250 | % remove all possible latex conflicts and wrap the characters so they | |
|
251 | % can't flow off of the page. | |
|
252 | %============================================================================== | |
|
253 | ((* block unknowncell scoped*)) | |
|
254 | ||
|
255 | % Unsupported cell type, no formatting | |
|
256 | ((( cell.source | wrap | escape_tex ))) | |
|
257 | ((* endblock unknowncell *)) | |
|
258 | ||
|
259 | %============================================================================== | |
|
260 | % Input | |
|
261 | %============================================================================== | |
|
262 | ((* block input *)) | |
|
263 | ||
|
264 | % Make sure that atleast 4 lines are below the HR | |
|
265 | \needspace{((( min_header_lines )))\baselineskip} | |
|
266 | ||
|
267 | ((* if resources.sphinx.outputstyle == 'simple' *)) | |
|
268 | ||
|
269 | % Add a horizantal break, along with break title. | |
|
270 | \vspace{10pt} | |
|
271 | {\scriptsize Input}\\* | |
|
272 | \rule[10pt]{\linewidth}{0.5pt} | |
|
273 | \vspace{-25pt} | |
|
274 | ||
|
275 | % Add contents below. | |
|
276 | ((( cell.input | highlight ))) | |
|
277 | ||
|
278 | ((* elif resources.sphinx.outputstyle == 'notebook' *)) | |
|
279 | \vspace{6pt} | |
|
280 | ((( write_prompt("In", cell.prompt_number, "nbframe-in-prompt") ))) | |
|
281 | \vspace{-2.65\baselineskip} | |
|
282 | \begin{ColorVerbatim} | |
|
283 | \vspace{-0.7\baselineskip} | |
|
284 | ((( cell.input | highlight ))) | |
|
285 | ((* if cell.input == None or cell.input == '' *)) | |
|
286 | \vspace{0.3\baselineskip} | |
|
287 | ((* else *)) | |
|
288 | \vspace{-0.2\baselineskip} | |
|
289 | ((* endif *)) | |
|
290 | \end{ColorVerbatim} | |
|
291 | ((* endif *)) | |
|
292 | ((* endblock input *)) | |
|
293 | ||
|
294 | %============================================================================== | |
|
295 | % Output_Group | |
|
296 | % | |
|
297 | % Purpose: Make sure that only one header bar only attaches to the output | |
|
298 | % once. By keeping track of when an input group is started | |
|
299 | %============================================================================== | |
|
300 | ((* block output_group *)) | |
|
301 | ((* if cell.outputs.__len__() > 0 *)) | |
|
302 | ||
|
303 | % If the first block is an image, minipage the image. Else | |
|
304 | % request a certain amount of space for the input text. | |
|
305 | ((( iff_figure(cell.outputs[0], "\\begin{minipage}{1.0\\textwidth}", "\\needspace{" ~ min_header_lines ~ "\\baselineskip}") ))) | |
|
306 | ||
|
307 | ((* if resources.sphinx.outputstyle == 'simple' *)) | |
|
308 | ||
|
309 | % Add a horizantal break, along with break title. | |
|
310 | \vspace{10pt} | |
|
311 | {\scriptsize Output}\\* | |
|
312 | \rule[10pt]{\linewidth}{0.5pt} | |
|
313 | \vspace{-20pt} | |
|
314 | ||
|
315 | % Add the contents of the first block. | |
|
316 | ((( render_output(cell.outputs[0]) ))) | |
|
317 | ||
|
318 | % Close the minipage. | |
|
319 | ((( iff_figure(cell.outputs[0], "\\end{minipage}", "") ))) | |
|
320 | ||
|
321 | % Add remainer of the document contents below. | |
|
322 | ((* for output in cell.outputs[1:] *)) | |
|
323 | ((( render_output(output, cell.prompt_number) ))) | |
|
324 | ((* endfor *)) | |
|
325 | ((* elif resources.sphinx.outputstyle == 'notebook' *)) | |
|
326 | ||
|
327 | % Add document contents. | |
|
328 | ((* for output in cell.outputs *)) | |
|
329 | ((( render_output(output, cell.prompt_number) ))) | |
|
330 | ((* endfor *)) | |
|
331 | ((* endif *)) | |
|
332 | ((* endif *)) | |
|
333 | ((* endblock *)) | |
|
334 | ||
|
335 | %============================================================================== | |
|
336 | % Additional formating | |
|
337 | %============================================================================== | |
|
338 | ((* block data_text *)) | |
|
339 | ((( custom_verbatim(output.text) | ansi2latex))) | |
|
340 | ((* endblock *)) | |
|
341 | ||
|
342 | ((* block traceback_line *)) | |
|
343 | ((( conditionally_center_output(line | indent| rm_ansi) ))) | |
|
344 | ((* endblock traceback_line *)) | |
|
345 | ||
|
346 | %============================================================================== | |
|
347 | % Supported image formats | |
|
348 | %============================================================================== | |
|
349 | ((*- block data_png -*)) | |
|
350 | ((( conditionally_center_output(insert_graphics(output.key_png)) ))) | |
|
351 | ((*- endblock -*)) | |
|
352 | ||
|
353 | ((*- block data_svg -*)) | |
|
354 | ((( conditionally_center_output(insert_graphics(output.key_svg)) ))) | |
|
355 | ((*- endblock -*)) | |
|
356 | ||
|
357 | ((*- block data_latex *)) | |
|
358 | ((* if resources.sphinx.centeroutput *))\begin{center}((* endif -*))((( output.latex | rm_math_space )))((*- if resources.sphinx.centeroutput *))\end{center} ((* endif -*)) | |
|
359 | ((*- endblock -*)) | |
|
360 | ||
|
361 | %============================================================================== | |
|
362 | % Support Macros | |
|
363 | %============================================================================== | |
|
364 | ||
|
365 | % Name: write_prompt | |
|
366 | % Purpose: Renders an output/input prompt for notebook style pdfs | |
|
367 | ((* macro write_prompt(prompt, number, color) -*)) | |
|
368 | \makebox[0.1\linewidth]{\smaller\hfill\tt\color{((( color )))}((( prompt )))\hspace{4pt}{[}((( number ))){]}:\hspace{4pt}}\\* | |
|
369 | ((*- endmacro *)) | |
|
370 | ||
|
371 | % Name: render_output | |
|
372 | % Purpose: Renders an output block appropriately. | |
|
373 | ((* macro render_output(output, prompt_number) -*)) | |
|
374 | ((*- if output.output_type == 'pyerr' -*)) | |
|
375 | ((*- block pyerr scoped *)) | |
|
376 | ((( custom_verbatim(super()) ))) | |
|
377 | ((* endblock pyerr -*)) | |
|
378 | ((*- else -*)) | |
|
379 | ||
|
380 | ((* if resources.sphinx.outputstyle == 'notebook' *)) | |
|
381 | ((*- if output.output_type == 'pyout' -*)) | |
|
382 | ((( write_prompt("Out", prompt_number, "nbframe-out-prompt") ))) | |
|
383 | \vspace{-2.55\baselineskip} | |
|
384 | ((*- endif -*)) | |
|
385 | ||
|
386 | \begin{InvisibleVerbatim} | |
|
387 | \vspace{-0.5\baselineskip} | |
|
388 | ((*- endif -*)) | |
|
389 | ||
|
390 | ((*- block display_data scoped -*)) | |
|
391 | ((( super() ))) | |
|
392 | ((*- endblock display_data -*)) | |
|
393 | ||
|
394 | ((* if resources.sphinx.outputstyle == 'notebook' *)) | |
|
395 | \end{InvisibleVerbatim} | |
|
396 | ((*- endif -*)) | |
|
397 | ((*- endif -*)) | |
|
398 | ((*- endmacro *)) | |
|
399 | ||
|
400 | % Name: iff_figure | |
|
401 | % Purpose: If the output block provided is a figure type, the 'true_content' | |
|
402 | % parameter will be returned. Else, the 'false_content'. | |
|
403 | ((* macro iff_figure(output, true_content, false_content) -*)) | |
|
404 | ((*- set is_figure = false -*)) | |
|
405 | ((*- for type in output | filter_data_type -*)) | |
|
406 | ((*- if type in ['pdf', 'svg', 'png', 'jpeg','html']*)) | |
|
407 | ((*- set is_figure = true -*)) | |
|
408 | ((*- endif -*)) | |
|
409 | ((*- endfor -*)) | |
|
410 | ||
|
411 | ((* if is_figure -*)) | |
|
412 | ((( true_content ))) | |
|
413 | ((*- else -*)) | |
|
414 | ((( false_content ))) | |
|
415 | ((*- endif *)) | |
|
416 | ((*- endmacro *)) | |
|
417 | ||
|
418 | % Name: custom_verbatim | |
|
419 | % Purpose: This macro creates a verbatim style block that fits the existing | |
|
420 | % sphinx style more readily than standard verbatim blocks. | |
|
421 | ((* macro custom_verbatim(text) -*)) | |
|
422 | \begin{alltt} | |
|
423 | ((*- if resources.sphinx.centeroutput *))\begin{center} ((* endif -*)) | |
|
424 | ((( text | wrap(wrap_size) ))) | |
|
425 | ((*- if resources.sphinx.centeroutput *))\end{center}((* endif -*)) | |
|
426 | \end{alltt} | |
|
427 | ((*- endmacro *)) | |
|
428 | ||
|
429 | % Name: conditionally_center_output | |
|
430 | % Purpose: This macro centers the output if the output centering is enabled. | |
|
431 | ((* macro conditionally_center_output(text) -*)) | |
|
432 | ((* if resources.sphinx.centeroutput *)){\centering ((* endif *))((( text )))((* if resources.sphinx.centeroutput *))}((* endif *)) | |
|
433 | ((*- endmacro *)) | |
|
434 | ||
|
435 | % Name: insert_graphics | |
|
436 | % Purpose: This macro will insert an image in the latex document given a path. | |
|
437 | ((* macro insert_graphics(path) -*)) | |
|
438 | \begin{center} | |
|
439 | \includegraphics[max size={\textwidth}{\textheight}]{(((path)))} | |
|
440 | \par | |
|
441 | \end{center} | |
|
442 | ((*- endmacro *)) |
@@ -0,0 +1,25 b'' | |||
|
1 | ((============================================================================ | |
|
2 | NBConvert Sphinx-Latex HowTo Template | |
|
3 | ||
|
4 | Purpose: Allow export of PDF friendly Latex inspired by Sphinx HowTo | |
|
5 | document style. Most of the is derived directly from Sphinx source. | |
|
6 | ||
|
7 | Inheritance: null>display_priority>latex_base->latex_sphinx_base | |
|
8 | ||
|
9 | ==========================================================================)) | |
|
10 | ||
|
11 | ((*- extends 'sphinx_base.tplx' -*)) | |
|
12 | ||
|
13 | ((* set parentdocumentclass = 'article' *)) | |
|
14 | ((* set documentclass = 'howto' *)) | |
|
15 | ||
|
16 | ((* block h1 -*))part((* endblock h1 -*)) | |
|
17 | ((* block h2 -*))section((* endblock h2 -*)) | |
|
18 | ((* block h3 -*))subsection((* endblock h3 -*)) | |
|
19 | ((* block h4 -*))subsubsection((* endblock h4 -*)) | |
|
20 | ((* block h5 -*))paragraph((* endblock h5 -*)) | |
|
21 | ((* block h6 -*))subparagraph((* endblock h6 -*)) | |
|
22 | ||
|
23 | % Diasble table of contents for howto | |
|
24 | ((* block toc *)) | |
|
25 | ((* endblock toc *)) |
@@ -0,0 +1,21 b'' | |||
|
1 | ((============================================================================ | |
|
2 | NBConvert Sphinx-Latex Manual Template | |
|
3 | ||
|
4 | Purpose: Allow export of PDF friendly Latex inspired by Sphinx Manual | |
|
5 | document style. Most of the is derived directly from Sphinx source. | |
|
6 | ||
|
7 | Inheritance: null>display_priority>latex_base->latex_sphinx_base | |
|
8 | ||
|
9 | ==========================================================================)) | |
|
10 | ||
|
11 | ((*- extends 'sphinx_base.tplx' -*)) | |
|
12 | ||
|
13 | ((* set parentdocumentclass = 'report' *)) | |
|
14 | ((* set documentclass = 'manual' *)) | |
|
15 | ||
|
16 | ((* block h1 -*))part((* endblock h1 -*)) | |
|
17 | ((* block h2 -*))chapter((* endblock h2 -*)) | |
|
18 | ((* block h3 -*))section((* endblock h3 -*)) | |
|
19 | ((* block h4 -*))subsection((* endblock h4 -*)) | |
|
20 | ((* block h5 -*))subsubsection((* endblock h5 -*)) | |
|
21 | ((* block h6 -*))paragraph((* endblock h6 -*)) |
@@ -0,0 +1,73 b'' | |||
|
1 | {% extends 'display_priority.tpl' %} | |
|
2 | {% block in_prompt %} | |
|
3 | In[{{cell.prompt_number if cell.prompt_number else ' '}}]:{% endblock in_prompt %} | |
|
4 | ||
|
5 | {% block output_prompt %}{% if cell.haspyout %}Out[{{cell.prompt_number}}]: | |
|
6 | {%- endif %}{%- endblock output_prompt %} | |
|
7 | ||
|
8 | {% block input %} | |
|
9 | ``` | |
|
10 | {{ cell.input}} | |
|
11 | ``` | |
|
12 | {% endblock input %} | |
|
13 | ||
|
14 | {% block pyerr %} | |
|
15 | {{ super() }} | |
|
16 | {% endblock pyerr %} | |
|
17 | ||
|
18 | {% block traceback_line %} | |
|
19 | {{ line |indent| rm_ansi }}{% endblock traceback_line %} | |
|
20 | ||
|
21 | {% block pyout %} | |
|
22 | {% block data_priority scoped %}{{ super()}}{% endblock %} | |
|
23 | {% endblock pyout %} | |
|
24 | ||
|
25 | {% block stream %} | |
|
26 | {{ output.text| indent }} | |
|
27 | {% endblock stream %} | |
|
28 | ||
|
29 | ||
|
30 | ||
|
31 | ||
|
32 | {% block data_svg %} | |
|
33 | [!image]({{output.key_svg}}) | |
|
34 | {% endblock data_svg %} | |
|
35 | ||
|
36 | {% block data_png %} | |
|
37 | [!image]({{output.key_png}}) | |
|
38 | {% endblock data_png %} | |
|
39 | ||
|
40 | {% block data_jpg %} | |
|
41 | [!image]({{output.key_jpg}}) | |
|
42 | {% endblock data_jpg %} | |
|
43 | ||
|
44 | ||
|
45 | ||
|
46 | {% block data_latex %} | |
|
47 | $$ | |
|
48 | {{output.latex}} | |
|
49 | $$ | |
|
50 | {% endblock data_latex %} | |
|
51 | ||
|
52 | {% block data_text scoped %} | |
|
53 | ||
|
54 | {{output.text | indent}} | |
|
55 | ||
|
56 | {% endblock data_text %} | |
|
57 | ||
|
58 | {% block markdowncell scoped %} | |
|
59 | {{ cell.source | wrap(80)}} | |
|
60 | {% endblock markdowncell %} | |
|
61 | ||
|
62 | {% block headingcell scoped %} | |
|
63 | ||
|
64 | {{ '#' * cell.level }} {{ cell.source}} | |
|
65 | ||
|
66 | {% endblock headingcell %} | |
|
67 | ||
|
68 | {% block rawcell scoped %}{{ cell.source }} | |
|
69 | {% endblock rawcell %} | |
|
70 | ||
|
71 | {% block unknowncell scoped %} | |
|
72 | unknown type {{cell.type}} | |
|
73 | {% endblock unknowncell %} |
@@ -0,0 +1,56 b'' | |||
|
1 | {%- extends 'null.tpl' -%} | |
|
2 | ||
|
3 | {% block in_prompt %} | |
|
4 | # In[{{cell.prompt_number if cell.prompt_number else ' '}}]: | |
|
5 | {% endblock in_prompt %} | |
|
6 | ||
|
7 | {% block output_prompt %} | |
|
8 | # Out[{{cell.prompt_number}}]:{% endblock output_prompt %} | |
|
9 | ||
|
10 | {% block input %}{{ cell.input }} | |
|
11 | {% endblock input %} | |
|
12 | ||
|
13 | ||
|
14 | {# Those Two are for error displaying | |
|
15 | even if the first one seem to do nothing, | |
|
16 | it introduces a new line | |
|
17 | ||
|
18 | #} | |
|
19 | {% block pyerr %}{{ super() }} | |
|
20 | {% endblock pyerr %} | |
|
21 | ||
|
22 | {% block traceback_line %} | |
|
23 | {{ line |indent| rm_ansi }}{% endblock traceback_line %} | |
|
24 | {# .... #} | |
|
25 | ||
|
26 | ||
|
27 | {% block pyout %} | |
|
28 | {{ output.text| indent | pycomment}} | |
|
29 | {% endblock pyout %} | |
|
30 | ||
|
31 | {% block stream %} | |
|
32 | {{ output.text| indent | pycomment}} | |
|
33 | {% endblock stream %} | |
|
34 | ||
|
35 | ||
|
36 | ||
|
37 | ||
|
38 | {% block display_data scoped %} | |
|
39 | # image file: | |
|
40 | {% endblock display_data %} | |
|
41 | ||
|
42 | {% block markdowncell scoped %} | |
|
43 | {{ cell.source | pycomment }} | |
|
44 | {% endblock markdowncell %} | |
|
45 | ||
|
46 | {% block headingcell scoped %} | |
|
47 | {{ '#' * cell.level }}{{ cell.source | pycomment}} | |
|
48 | {% endblock headingcell %} | |
|
49 | ||
|
50 | {% block rawcell scoped %} | |
|
51 | {{ cell.source | pycomment }} | |
|
52 | {% endblock rawcell %} | |
|
53 | ||
|
54 | {% block unknowncell scoped %} | |
|
55 | unknown type {{cell.type}} | |
|
56 | {% endblock unknowncell %} |
@@ -0,0 +1,31 b'' | |||
|
1 | {%- extends 'python.tpl' -%} | |
|
2 | ||
|
3 | {#% block any_cell %} | |
|
4 | ============================== | |
|
5 | =======start {{cell.type}}========= | |
|
6 | {{ super() }} | |
|
7 | ======= end {{cell.type}} ========= | |
|
8 | =============================={% endblock any_cell %#} | |
|
9 | ||
|
10 | ||
|
11 | ||
|
12 | {% block markdowncell %}---- Start MD ----{{ super() }} | |
|
13 | ---- End MD ---- | |
|
14 | {% endblock markdowncell %} | |
|
15 | ||
|
16 | {% block codecell %}---- Start Code ----{{ super() }} | |
|
17 | ---- End Code ---- | |
|
18 | {% endblock codecell %} | |
|
19 | ||
|
20 | {% block headingcell scoped %}---- Start heading ----{{ super() }} | |
|
21 | ---- End heading ---- | |
|
22 | {% endblock headingcell %} | |
|
23 | ||
|
24 | {% block rawcell scoped %}---- Start Raw ---- | |
|
25 | {{ super() }} | |
|
26 | ---- End Raw ----{% endblock rawcell %} | |
|
27 | ||
|
28 | {% block unknowncell scoped %} | |
|
29 | unknown type {{cell.type}} | |
|
30 | {% endblock unknowncell %} | |
|
31 |
@@ -0,0 +1,180 b'' | |||
|
1 | {%- extends 'slides.tpl' -%} | |
|
2 | ||
|
3 | ||
|
4 | {% block header %} | |
|
5 | <!DOCTYPE html> | |
|
6 | <html> | |
|
7 | <head> | |
|
8 | ||
|
9 | <meta charset="utf-8" /> | |
|
10 | <meta http-equiv="X-UA-Compatible" content="chrome=1"> | |
|
11 | ||
|
12 | <meta name="apple-mobile-web-app-capable" content="yes" /> | |
|
13 | <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" /> | |
|
14 | ||
|
15 | <link rel="stylesheet" href="reveal.js/css/reveal.css"> | |
|
16 | <link rel="stylesheet" href="reveal.js/css/theme/simple.css" id="theme"> | |
|
17 | ||
|
18 | <!-- For syntax highlighting --> | |
|
19 | <link rel="stylesheet" href="reveal.js/lib/css/zenburn.css"> | |
|
20 | ||
|
21 | <!-- If the query includes 'print-pdf', use the PDF print sheet --> | |
|
22 | <script> | |
|
23 | document.write( '<link rel="stylesheet" href="reveal.js/css/print/' + ( window.location.search.match( /print-pdf/gi ) ? 'pdf' : 'paper' ) + '.css" type="text/css" media="print">' ); | |
|
24 | </script> | |
|
25 | ||
|
26 | <!--[if lt IE 9]> | |
|
27 | <script src="reveal.js/lib/js/html5shiv.js"></script> | |
|
28 | <![endif]--> | |
|
29 | ||
|
30 | {% for css in resources.inlining.css -%} | |
|
31 | <style type="text/css"> | |
|
32 | {{css}} | |
|
33 | </style> | |
|
34 | {% endfor %} | |
|
35 | ||
|
36 | <style type="text/css"> | |
|
37 | /* Overrides of notebook CSS for static HTML export */ | |
|
38 | .reveal { | |
|
39 | font-size: 20px; | |
|
40 | overflow-y: auto; | |
|
41 | overflow-x: hidden; | |
|
42 | } | |
|
43 | .reveal pre { | |
|
44 | width: 95%; | |
|
45 | padding: 0.4em; | |
|
46 | margin: 0px; | |
|
47 | font-family: monospace, sans-serif; | |
|
48 | font-size: 80%; | |
|
49 | box-shadow: 0px 0px 0px rgba(0, 0, 0, 0); | |
|
50 | } | |
|
51 | .reveal section img { | |
|
52 | border: 0px solid black; | |
|
53 | box-shadow: 0 0 10px rgba(0, 0, 0, 0); | |
|
54 | } | |
|
55 | .reveal .slides { | |
|
56 | text-align: left; | |
|
57 | } | |
|
58 | .reveal.fade { | |
|
59 | opacity: 1; | |
|
60 | } | |
|
61 | div.input_area { | |
|
62 | padding: 0.06em; | |
|
63 | } | |
|
64 | div.code_cell { | |
|
65 | background-color: transparent; | |
|
66 | } | |
|
67 | div.prompt { | |
|
68 | width: 11ex; | |
|
69 | padding: 0.4em; | |
|
70 | margin: 0px; | |
|
71 | font-family: monospace, sans-serif; | |
|
72 | font-size: 80%; | |
|
73 | text-align: right; | |
|
74 | } | |
|
75 | div.output_area pre { | |
|
76 | font-family: monospace, sans-serif; | |
|
77 | font-size: 80%; | |
|
78 | } | |
|
79 | div.output_prompt { | |
|
80 | /* 5px right shift to account for margin in parent container */ | |
|
81 | margin: 5px 5px 0 -5px; | |
|
82 | } | |
|
83 | .rendered_html p { | |
|
84 | text-align: inherit; | |
|
85 | } | |
|
86 | </style> | |
|
87 | </head> | |
|
88 | {% endblock header%} | |
|
89 | ||
|
90 | ||
|
91 | {% block body %} | |
|
92 | <body> | |
|
93 | <div class="reveal"><div class="slides"> | |
|
94 | ||
|
95 | {{ super() }} | |
|
96 | ||
|
97 | </div></div> | |
|
98 | ||
|
99 | <!-- | |
|
100 | Uncomment the following block and the addthis_widget.js (see below inside dependencies) | |
|
101 | to get enable social buttons. | |
|
102 | --> | |
|
103 | ||
|
104 | <!-- | |
|
105 | <div class="addthis_toolbox addthis_floating_style addthis_32x32_style" style="left:20px;top:20px;"> | |
|
106 | <a class="addthis_button_twitter"></a> | |
|
107 | <a class="addthis_button_google_plusone_share"></a> | |
|
108 | <a class="addthis_button_linkedin"></a> | |
|
109 | <a class="addthis_button_facebook"></a> | |
|
110 | <a class="addthis_button_more"></a> | |
|
111 | </div> | |
|
112 | --> | |
|
113 | ||
|
114 | <script src="reveal.js/lib/js/head.min.js"></script> | |
|
115 | ||
|
116 | <script src="reveal.js/js/reveal.min.js"></script> | |
|
117 | ||
|
118 | <script> | |
|
119 | ||
|
120 | // Full list of configuration options available here: https://github.com/hakimel/reveal.js#configuration | |
|
121 | Reveal.initialize({ | |
|
122 | controls: true, | |
|
123 | progress: true, | |
|
124 | history: true, | |
|
125 | ||
|
126 | theme: Reveal.getQueryHash().theme, // available themes are in /css/theme | |
|
127 | transition: Reveal.getQueryHash().transition || 'linear', // default/cube/page/concave/zoom/linear/none | |
|
128 | ||
|
129 | // Optional libraries used to extend on reveal.js | |
|
130 | dependencies: [ | |
|
131 | { src: 'reveal.js/lib/js/classList.js', condition: function() { return !document.body.classList; } }, | |
|
132 | { src: 'reveal.js/plugin/highlight/highlight.js', async: true, callback: function() { hljs.initHighlightingOnLoad(); } }, | |
|
133 | { src: 'reveal.js/plugin/notes/notes.js', async: true, condition: function() { return !!document.body.classList; } } | |
|
134 | // { src: 'http://s7.addthis.com/js/300/addthis_widget.js', async: true}, | |
|
135 | ] | |
|
136 | }); | |
|
137 | </script> | |
|
138 | ||
|
139 | <!-- MathJax configuration --> | |
|
140 | <script type="text/x-mathjax-config"> | |
|
141 | MathJax.Hub.Config({ | |
|
142 | tex2jax: { | |
|
143 | inlineMath: [ ['$','$'], ["\\(","\\)"] ], | |
|
144 | displayMath: [ ['$$','$$'], ["\\[","\\]"] ] | |
|
145 | }, | |
|
146 | displayAlign: 'left', // Change this to 'center' to center equations. | |
|
147 | "HTML-CSS": { | |
|
148 | styles: {'.MathJax_Display': {"margin": 0}} | |
|
149 | } | |
|
150 | }); | |
|
151 | </script> | |
|
152 | <!-- End of mathjax configuration --> | |
|
153 | ||
|
154 | <script> | |
|
155 | // We wait for the onload function to load MathJax after the page is completely loaded. | |
|
156 | // MathJax is loaded 1 unit of time after the page is ready. | |
|
157 | // This hack prevent problems when you load multiple js files (i.e. social button from addthis). | |
|
158 | // | |
|
159 | window.onload = function () { | |
|
160 | setTimeout(function () { | |
|
161 | var script = document.createElement("script"); | |
|
162 | script.type = "text/javascript"; | |
|
163 | script.src = "https://c328740.ssl.cf1.rackcdn.com/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"; | |
|
164 | document.getElementsByTagName("head")[0].appendChild(script); | |
|
165 | },1) | |
|
166 | } | |
|
167 | </script> | |
|
168 | ||
|
169 | <script> | |
|
170 | Reveal.addEventListener( 'slidechanged', function( event ) { | |
|
171 | MathJax.Hub.Rerender(event.currentSlide); | |
|
172 | }); | |
|
173 | </script> | |
|
174 | ||
|
175 | </body> | |
|
176 | {% endblock body %} | |
|
177 | ||
|
178 | {% block footer %} | |
|
179 | </html> | |
|
180 | {% endblock footer %} |
@@ -0,0 +1,21 b'' | |||
|
1 | {%- extends 'basichtml.tpl' -%} | |
|
2 | ||
|
3 | ||
|
4 | ||
|
5 | {%- block any_cell scoped -%} | |
|
6 | {%- if cell.metadata.slide_type in ['-', 'slide', 'subslide'] -%} | |
|
7 | {{ super() }} | |
|
8 | {%- elif cell.metadata.slide_type in ['skip'] -%} | |
|
9 | <div style=display:none> | |
|
10 | {{ super() }} | |
|
11 | </div> | |
|
12 | {%- elif cell.metadata.slide_type in ['notes'] -%} | |
|
13 | <aside class="notes"> | |
|
14 | {{ super() }} | |
|
15 | </aside> | |
|
16 | {%- elif cell.metadata.slide_type in ['fragment'] -%} | |
|
17 | <div class="fragment"> | |
|
18 | {{ super() }} | |
|
19 | </div> | |
|
20 | {%- endif -%} | |
|
21 | {%- endblock any_cell -%} |
@@ -0,0 +1,89 b'' | |||
|
1 | {%- extends 'display_priority.tpl' -%} | |
|
2 | {% block in_prompt -%} | |
|
3 | In[{{cell.prompt_number if cell.prompt_number else ' '}}]: | |
|
4 | ||
|
5 | .. code:: python | |
|
6 | ||
|
7 | {% endblock in_prompt %} | |
|
8 | ||
|
9 | {% block output_prompt %}{% if cell.haspyout -%} | |
|
10 | Out[{{cell.prompt_number}}]:{% endif %}{% endblock output_prompt %} | |
|
11 | ||
|
12 | {% block input %}{{ cell.input | indent}} | |
|
13 | ||
|
14 | {% endblock input %} | |
|
15 | ||
|
16 | {% block pyerr %}:: | |
|
17 | {{ super() }} | |
|
18 | {% endblock pyerr %} | |
|
19 | ||
|
20 | {% block traceback_line %} | |
|
21 | {{ line |indent| rm_ansi }}{% endblock traceback_line %} | |
|
22 | ||
|
23 | {% block pyout %} | |
|
24 | {% block data_priority scoped %}{{ super()}}{% endblock %} | |
|
25 | {% endblock pyout %} | |
|
26 | ||
|
27 | {% block stream %} | |
|
28 | .. parsed-literal:: | |
|
29 | ||
|
30 | {{ output.text| indent }} | |
|
31 | {% endblock stream %} | |
|
32 | ||
|
33 | ||
|
34 | ||
|
35 | ||
|
36 | {% block data_svg %}.. image:: {{output.key_svg}} | |
|
37 | ||
|
38 | {% endblock data_svg %} | |
|
39 | ||
|
40 | {% block data_png %}.. image:: {{output.key_png}} | |
|
41 | ||
|
42 | {% endblock data_png %} | |
|
43 | ||
|
44 | {% block data_jpg %}..jpg image:: {{output.key_jpg}} | |
|
45 | ||
|
46 | {% endblock data_jpg %} | |
|
47 | ||
|
48 | ||
|
49 | ||
|
50 | {% block data_latex %}.. math:: | |
|
51 | ||
|
52 | {{output.latex| indent}} | |
|
53 | ||
|
54 | {% endblock data_latex %} | |
|
55 | ||
|
56 | {% block data_text scoped %}.. parsed-literal:: | |
|
57 | ||
|
58 | {{output.text | indent}} | |
|
59 | ||
|
60 | {% endblock data_text %} | |
|
61 | ||
|
62 | {% block markdowncell scoped %}{{ cell.source | markdown2rst }} | |
|
63 | {% endblock markdowncell %} | |
|
64 | ||
|
65 | {% block headingcell scoped %} | |
|
66 | {%- set len = cell.source|length -%} | |
|
67 | {{ cell.source}} | |
|
68 | {% if cell.level == 1 %} | |
|
69 | {{- '=' * len }} | |
|
70 | {%- elif cell.level == 2 %} | |
|
71 | {{- '-' * len }} | |
|
72 | {%- elif cell.level == 3 %} | |
|
73 | {{- '~' * len }} | |
|
74 | {%- elif cell.level == 4 %} | |
|
75 | {{- '.' * len }} | |
|
76 | {%- elif cell.level == 5 %} | |
|
77 | {{- '\\' * len }} | |
|
78 | {%- elif cell.level == 6 %} | |
|
79 | {{- '`' * len }} | |
|
80 | {% endif %} | |
|
81 | ||
|
82 | {% endblock headingcell %} | |
|
83 | ||
|
84 | {% block rawcell scoped %}{{ cell.source }} | |
|
85 | {% endblock rawcell %} | |
|
86 | ||
|
87 | {% block unknowncell scoped %} | |
|
88 | unknown type {{cell.type}} | |
|
89 | {% endblock unknowncell %} |
@@ -0,0 +1,23 b'' | |||
|
1 | ||
|
2 | ||
|
3 | all: tex/null.tplx tex/display_priority.tplx | |
|
4 | ||
|
5 | # convert jinja syntax to tex | |
|
6 | # cf http://flask.pocoo.org/snippets/55/ | |
|
7 | tex/%.tplx: %.tpl | |
|
8 | @echo 'generating tex equivalent of $^: $@' | |
|
9 | @echo '((= autogenerated file do not edit =))' > $@ | |
|
10 | @sed \ | |
|
11 | -e 's/{%/((*/g' \ | |
|
12 | -e 's/%}/*))/g' \ | |
|
13 | -e 's/{{/(((/g' \ | |
|
14 | -e 's/}}/)))/g' \ | |
|
15 | -e 's/{#/((=/g' \ | |
|
16 | -e 's/#}/=))/g' \ | |
|
17 | -e "s/tpl'/tplx'/g" \ | |
|
18 | $^ >> $@ | |
|
19 | ||
|
20 | ||
|
21 | clean: | |
|
22 | @echo "cleaning generated tplx files..." | |
|
23 | @rm tex/* |
@@ -0,0 +1,6 b'' | |||
|
1 | ## Template skeleton | |
|
2 | ||
|
3 | This contain skeleton template that you probably don't want | |
|
4 | to inherit directly. | |
|
5 | ||
|
6 | do not moify the content of the 'tex' folder which is generated by running 'make' in this folder. |
@@ -0,0 +1,38 b'' | |||
|
1 | {%- extends 'null.tpl' -%} | |
|
2 | ||
|
3 | {#display data priority#} | |
|
4 | ||
|
5 | ||
|
6 | {%- block data_priority scoped -%} | |
|
7 | {%- for type in output | filter_data_type -%} | |
|
8 | {%- if type in ['pdf']%} | |
|
9 | {%- block data_pdf -%} | |
|
10 | {%- endblock -%} | |
|
11 | {%- endif -%} | |
|
12 | {%- if type in ['svg']%} | |
|
13 | {%- block data_svg -%} | |
|
14 | {%- endblock -%} | |
|
15 | {%- endif -%} | |
|
16 | {%- if type in ['png']%} | |
|
17 | {%- block data_png -%} | |
|
18 | {%- endblock -%} | |
|
19 | {%- endif -%} | |
|
20 | {%- if type in ['html']%} | |
|
21 | {%- block data_html -%} | |
|
22 | {%- endblock -%} | |
|
23 | {%- endif -%} | |
|
24 | {%- if type in ['jpeg']%} | |
|
25 | {%- block data_jpg -%} | |
|
26 | {%- endblock -%} | |
|
27 | {%- endif -%} | |
|
28 | {%- if type in ['text']%} | |
|
29 | {%- block data_text -%} | |
|
30 | {%- endblock -%} | |
|
31 | {%- endif -%} | |
|
32 | ||
|
33 | {%- if type in ['latex']%} | |
|
34 | {%- block data_latex -%} | |
|
35 | {%- endblock -%} | |
|
36 | {%- endif -%} | |
|
37 | {%- endfor -%} | |
|
38 | {%- endblock data_priority -%} |
@@ -0,0 +1,91 b'' | |||
|
1 | {# | |
|
2 | ||
|
3 | DO NOT USE THIS AS A BASE WORK, | |
|
4 | IF YOU ARE COPY AND PASTING THIS FILE | |
|
5 | YOU ARE PROBABLY DOING THINGS WRONG. | |
|
6 | ||
|
7 | Null template, Does nothing except defining a basic structure | |
|
8 | To layout the different blocks of a notebook. | |
|
9 | ||
|
10 | Subtemplates can override blocks to define their custom representation. | |
|
11 | ||
|
12 | If one of the block you do overwrite is not a leave block, consider | |
|
13 | calling super. | |
|
14 | ||
|
15 | {%- block nonLeaveBlock -%} | |
|
16 | #add stuff at beginning | |
|
17 | {{ super() }} | |
|
18 | #add stuff at end | |
|
19 | {%- endblock nonLeaveBlock -%} | |
|
20 | ||
|
21 | consider calling super even if it is a leave block, we might insert more blocks later. | |
|
22 | ||
|
23 | #} | |
|
24 | {%- block header -%} | |
|
25 | {%- endblock header -%} | |
|
26 | {%- block body -%} | |
|
27 | {%- for worksheet in nb.worksheets -%} | |
|
28 | {%- for cell in worksheet.cells -%} | |
|
29 | {%- block any_cell scoped -%} | |
|
30 | {%- if cell.cell_type in ['code'] -%} | |
|
31 | {%- block codecell scoped -%} | |
|
32 | {%- block input_group -%} | |
|
33 | {%- block in_prompt -%}{%- endblock in_prompt -%} | |
|
34 | {%- block input -%}{%- endblock input -%} | |
|
35 | {%- endblock input_group -%} | |
|
36 | {%- if cell.outputs -%} | |
|
37 | {%- block output_group -%} | |
|
38 | {%- block output_prompt -%}{%- endblock output_prompt -%} | |
|
39 | {%- block outputs scoped -%} | |
|
40 | {%- for output in cell.outputs -%} | |
|
41 | {%- block output scoped -%} | |
|
42 | {%- if output.output_type in ['pyout'] -%} | |
|
43 | {%- block pyout scoped -%}{%- endblock pyout -%} | |
|
44 | {%- elif output.output_type in ['stream'] -%} | |
|
45 | {%- block stream scoped -%} | |
|
46 | {%- if output.stream in ['stdout'] -%} | |
|
47 | {%- block stream_stdout scoped -%} | |
|
48 | {%- endblock stream_stdout -%} | |
|
49 | {%- elif output.stream in ['stderr'] -%} | |
|
50 | {%- block stream_stderr scoped -%} | |
|
51 | {%- endblock stream_stderr -%} | |
|
52 | {%- endif -%} | |
|
53 | {%- endblock stream -%} | |
|
54 | {%- elif output.output_type in ['display_data'] -%} | |
|
55 | {%- block display_data scoped -%} | |
|
56 | {%- block data_priority scoped -%} | |
|
57 | {%- endblock data_priority -%} | |
|
58 | {%- endblock display_data -%} | |
|
59 | {%- elif output.output_type in ['pyerr'] -%} | |
|
60 | {%- block pyerr scoped -%} | |
|
61 | {%- for line in output.traceback -%} | |
|
62 | {%- block traceback_line scoped -%}{%- endblock traceback_line -%} | |
|
63 | {%- endfor -%} | |
|
64 | {%- endblock pyerr -%} | |
|
65 | {%- endif -%} | |
|
66 | {%- endblock output -%} | |
|
67 | {%- endfor -%} | |
|
68 | {%- endblock outputs -%} | |
|
69 | {%- endblock output_group -%} | |
|
70 | {%- endif -%} | |
|
71 | {%- endblock codecell -%} | |
|
72 | {%- elif cell.cell_type in ['markdown'] -%} | |
|
73 | {%- block markdowncell scoped-%} | |
|
74 | {%- endblock markdowncell -%} | |
|
75 | {%- elif cell.cell_type in ['heading'] -%} | |
|
76 | {%- block headingcell scoped-%} | |
|
77 | {%- endblock headingcell -%} | |
|
78 | {%- elif cell.cell_type in ['raw'] -%} | |
|
79 | {%- block rawcell scoped-%} | |
|
80 | {%- endblock rawcell -%} | |
|
81 | {%- else -%} | |
|
82 | {%- block unknowncell scoped-%} | |
|
83 | {%- endblock unknowncell -%} | |
|
84 | {%- endif -%} | |
|
85 | {%- endblock any_cell -%} | |
|
86 | {%- endfor -%} | |
|
87 | {%- endfor -%} | |
|
88 | {%- endblock body -%} | |
|
89 | ||
|
90 | {%- block footer -%} | |
|
91 | {%- endblock footer -%} |
@@ -0,0 +1,17 b'' | |||
|
1 | {%- extends 'subslides.tpl' -%} | |
|
2 | ||
|
3 | ||
|
4 | ||
|
5 | {%- block any_cell scoped -%} | |
|
6 | {%- if cell.metadata.slide_type in ['slide'] -%} | |
|
7 | <section> | |
|
8 | <section> | |
|
9 | {%- endif -%} | |
|
10 | ||
|
11 | {{ super() }} | |
|
12 | ||
|
13 | {%- if cell.metadata.slide_helper in ['slide_end'] -%} | |
|
14 | </section> | |
|
15 | </section> | |
|
16 | {%- endif -%} | |
|
17 | {%- endblock any_cell -%} |
@@ -0,0 +1,15 b'' | |||
|
1 | {%- extends 'align_reveal_cells.tpl' -%} | |
|
2 | ||
|
3 | ||
|
4 | ||
|
5 | {%- block any_cell scoped -%} | |
|
6 | {%- if cell.metadata.slide_type in ['subslide'] -%} | |
|
7 | <section> | |
|
8 | {%- endif -%} | |
|
9 | ||
|
10 | {{ super() }} | |
|
11 | ||
|
12 | {%- if cell.metadata.slide_helper in ['subslide_end'] -%} | |
|
13 | </section> | |
|
14 | {%- endif -%} | |
|
15 | {%- endblock any_cell -%} |
@@ -0,0 +1,9 b'' | |||
|
1 | # Class base Transformers | |
|
2 | from .activatable import ActivatableTransformer | |
|
3 | from .base import ConfigurableTransformer | |
|
4 | from .extractfigure import ExtractFigureTransformer | |
|
5 | from .latex import LatexTransformer | |
|
6 | from .sphinx import SphinxTransformer | |
|
7 | ||
|
8 | # decorated function Transformers | |
|
9 | from .coalescestreams import coalesce_streams |
@@ -0,0 +1,53 b'' | |||
|
1 | """ | |
|
2 | Contains base transformer with an enable/disable flag. | |
|
3 | """ | |
|
4 | #----------------------------------------------------------------------------- | |
|
5 | # Copyright (c) 2013, the IPython Development Team. | |
|
6 | # | |
|
7 | # Distributed under the terms of the Modified BSD License. | |
|
8 | # | |
|
9 | # The full license is in the file COPYING.txt, distributed with this software. | |
|
10 | #----------------------------------------------------------------------------- | |
|
11 | ||
|
12 | #----------------------------------------------------------------------------- | |
|
13 | # Imports | |
|
14 | #----------------------------------------------------------------------------- | |
|
15 | ||
|
16 | from .base import ConfigurableTransformer | |
|
17 | from IPython.utils.traitlets import (Bool) | |
|
18 | ||
|
19 | #----------------------------------------------------------------------------- | |
|
20 | # Classes and Functions | |
|
21 | #----------------------------------------------------------------------------- | |
|
22 | ||
|
23 | class ActivatableTransformer(ConfigurableTransformer): | |
|
24 | """ConfigurableTransformer that has an enabled flag | |
|
25 | ||
|
26 | Inherit from this if you just want to have a transformer which is | |
|
27 | disable by default and can be enabled via the config by | |
|
28 | 'c.YourTransformerName.enabled = True' | |
|
29 | """ | |
|
30 | ||
|
31 | enabled = Bool(False, config=True) | |
|
32 | ||
|
33 | def __call__(self, nb, resources): | |
|
34 | """ | |
|
35 | Transformation to apply on each notebook. | |
|
36 | ||
|
37 | You should return modified nb, resources. | |
|
38 | If you wish to apply your transform on each cell, you might want to | |
|
39 | overwrite cell_transform method instead. | |
|
40 | ||
|
41 | Parameters | |
|
42 | ---------- | |
|
43 | nb : NotebookNode | |
|
44 | Notebook being converted | |
|
45 | resources : dictionary | |
|
46 | Additional resources used in the conversion process. Allows | |
|
47 | transformers to pass variables into the Jinja engine. | |
|
48 | """ | |
|
49 | ||
|
50 | if not self.enabled : | |
|
51 | return nb, resources | |
|
52 | else : | |
|
53 | return super(ActivatableTransformer, self).__call__(nb, resources) |
@@ -0,0 +1,99 b'' | |||
|
1 | """ | |
|
2 | Module that re-groups transformer that would be applied to ipynb files | |
|
3 | before going through the templating machinery. | |
|
4 | ||
|
5 | It exposes a convenient class to inherit from to access configurability. | |
|
6 | """ | |
|
7 | #----------------------------------------------------------------------------- | |
|
8 | # Copyright (c) 2013, the IPython Development Team. | |
|
9 | # | |
|
10 | # Distributed under the terms of the Modified BSD License. | |
|
11 | # | |
|
12 | # The full license is in the file COPYING.txt, distributed with this software. | |
|
13 | #----------------------------------------------------------------------------- | |
|
14 | ||
|
15 | #----------------------------------------------------------------------------- | |
|
16 | # Imports | |
|
17 | #----------------------------------------------------------------------------- | |
|
18 | ||
|
19 | from ..utils.config import GlobalConfigurable | |
|
20 | ||
|
21 | #----------------------------------------------------------------------------- | |
|
22 | # Classes and Functions | |
|
23 | #----------------------------------------------------------------------------- | |
|
24 | ||
|
25 | class ConfigurableTransformer(GlobalConfigurable): | |
|
26 | """ A configurable transformer | |
|
27 | ||
|
28 | Inherit from this class if you wish to have configurability for your | |
|
29 | transformer. | |
|
30 | ||
|
31 | Any configurable traitlets this class exposed will be configurable in profiles | |
|
32 | using c.SubClassName.atribute=value | |
|
33 | ||
|
34 | you can overwrite cell_transform to apply a transformation independently on each cell | |
|
35 | or __call__ if you prefer your own logic. See corresponding docstring for informations. | |
|
36 | """ | |
|
37 | ||
|
38 | def __init__(self, config=None, **kw): | |
|
39 | """ | |
|
40 | Public constructor | |
|
41 | ||
|
42 | Parameters | |
|
43 | ---------- | |
|
44 | config : Config | |
|
45 | Configuration file structure | |
|
46 | **kw : misc | |
|
47 | Additional arguments | |
|
48 | """ | |
|
49 | ||
|
50 | super(ConfigurableTransformer, self).__init__(config=config, **kw) | |
|
51 | ||
|
52 | ||
|
53 | def __call__(self, nb, resources): | |
|
54 | return self.call(nb,resources) | |
|
55 | ||
|
56 | def call(self, nb, resources): | |
|
57 | """ | |
|
58 | Transformation to apply on each notebook. | |
|
59 | ||
|
60 | You should return modified nb, resources. | |
|
61 | If you wish to apply your transform on each cell, you might want to | |
|
62 | overwrite cell_transform method instead. | |
|
63 | ||
|
64 | Parameters | |
|
65 | ---------- | |
|
66 | nb : NotebookNode | |
|
67 | Notebook being converted | |
|
68 | resources : dictionary | |
|
69 | Additional resources used in the conversion process. Allows | |
|
70 | transformers to pass variables into the Jinja engine. | |
|
71 | """ | |
|
72 | try : | |
|
73 | for worksheet in nb.worksheets : | |
|
74 | for index, cell in enumerate(worksheet.cells): | |
|
75 | worksheet.cells[index], resources = self.cell_transform(cell, resources, index) | |
|
76 | return nb, resources | |
|
77 | except NotImplementedError: | |
|
78 | raise NotImplementedError('should be implemented by subclass') | |
|
79 | ||
|
80 | ||
|
81 | def cell_transform(self, cell, resources, index): | |
|
82 | """ | |
|
83 | Overwrite if you want to apply a transformation on each cell. You | |
|
84 | should return modified cell and resource dictionary. | |
|
85 | ||
|
86 | Parameters | |
|
87 | ---------- | |
|
88 | cell : NotebookNode cell | |
|
89 | Notebook cell being processed | |
|
90 | resources : dictionary | |
|
91 | Additional resources used in the conversion process. Allows | |
|
92 | transformers to pass variables into the Jinja engine. | |
|
93 | index : int | |
|
94 | Index of the cell being processed | |
|
95 | """ | |
|
96 | ||
|
97 | raise NotImplementedError('should be implemented by subclass') | |
|
98 | return cell, resources | |
|
99 |
@@ -0,0 +1,75 b'' | |||
|
1 | """Module that allows latex output notebooks to be conditioned before | |
|
2 | they are converted. Exposes a decorator (@cell_preprocessor) in | |
|
3 | addition to the coalesce_streams pre-proccessor. | |
|
4 | """ | |
|
5 | #----------------------------------------------------------------------------- | |
|
6 | # Copyright (c) 2013, the IPython Development Team. | |
|
7 | # | |
|
8 | # Distributed under the terms of the Modified BSD License. | |
|
9 | # | |
|
10 | # The full license is in the file COPYING.txt, distributed with this software. | |
|
11 | #----------------------------------------------------------------------------- | |
|
12 | ||
|
13 | #----------------------------------------------------------------------------- | |
|
14 | # Functions | |
|
15 | #----------------------------------------------------------------------------- | |
|
16 | ||
|
17 | def cell_preprocessor(function): | |
|
18 | """ | |
|
19 | Wrap a function to be executed on all cells of a notebook | |
|
20 | ||
|
21 | Wrapped Parameters | |
|
22 | ---------- | |
|
23 | cell : NotebookNode cell | |
|
24 | Notebook cell being processed | |
|
25 | resources : dictionary | |
|
26 | Additional resources used in the conversion process. Allows | |
|
27 | transformers to pass variables into the Jinja engine. | |
|
28 | index : int | |
|
29 | Index of the cell being processed | |
|
30 | """ | |
|
31 | ||
|
32 | def wrappedfunc(nb, resources): | |
|
33 | for worksheet in nb.worksheets : | |
|
34 | for index, cell in enumerate(worksheet.cells): | |
|
35 | worksheet.cells[index], resources = function(cell, resources, index) | |
|
36 | return nb, resources | |
|
37 | return wrappedfunc | |
|
38 | ||
|
39 | ||
|
40 | @cell_preprocessor | |
|
41 | def coalesce_streams(cell, resources, index): | |
|
42 | """ | |
|
43 | Merge consecutive sequences of stream output into single stream | |
|
44 | to prevent extra newlines inserted at flush calls | |
|
45 | ||
|
46 | Parameters | |
|
47 | ---------- | |
|
48 | cell : NotebookNode cell | |
|
49 | Notebook cell being processed | |
|
50 | resources : dictionary | |
|
51 | Additional resources used in the conversion process. Allows | |
|
52 | transformers to pass variables into the Jinja engine. | |
|
53 | index : int | |
|
54 | Index of the cell being processed | |
|
55 | """ | |
|
56 | ||
|
57 | outputs = cell.get('outputs', []) | |
|
58 | if not outputs: | |
|
59 | return cell, resources | |
|
60 | ||
|
61 | last = outputs[0] | |
|
62 | new_outputs = [last] | |
|
63 | ||
|
64 | for output in outputs[1:]: | |
|
65 | if (output.output_type == 'stream' and | |
|
66 | last.output_type == 'stream' and | |
|
67 | last.stream == output.stream | |
|
68 | ): | |
|
69 | last.text += output.text | |
|
70 | else: | |
|
71 | new_outputs.append(output) | |
|
72 | ||
|
73 | cell.outputs = new_outputs | |
|
74 | return cell, resources | |
|
75 |
@@ -0,0 +1,105 b'' | |||
|
1 | """Module that pre-processes the notebook for export to HTML. | |
|
2 | """ | |
|
3 | #----------------------------------------------------------------------------- | |
|
4 | # Copyright (c) 2013, the IPython Development Team. | |
|
5 | # | |
|
6 | # Distributed under the terms of the Modified BSD License. | |
|
7 | # | |
|
8 | # The full license is in the file COPYING.txt, distributed with this software. | |
|
9 | #----------------------------------------------------------------------------- | |
|
10 | ||
|
11 | #----------------------------------------------------------------------------- | |
|
12 | # Imports | |
|
13 | #----------------------------------------------------------------------------- | |
|
14 | ||
|
15 | import os | |
|
16 | import io | |
|
17 | ||
|
18 | from pygments.formatters import HtmlFormatter | |
|
19 | ||
|
20 | from IPython.utils import path | |
|
21 | ||
|
22 | from .activatable import ActivatableTransformer | |
|
23 | ||
|
24 | #----------------------------------------------------------------------------- | |
|
25 | # Classes and functions | |
|
26 | #----------------------------------------------------------------------------- | |
|
27 | ||
|
28 | class CSSHtmlHeaderTransformer(ActivatableTransformer): | |
|
29 | """ | |
|
30 | Transformer used to pre-process notebook for HTML output. Adds IPython notebook | |
|
31 | front-end CSS and Pygments CSS to HTML output. | |
|
32 | """ | |
|
33 | ||
|
34 | header = [] | |
|
35 | ||
|
36 | def __init__(self, config=None, **kw): | |
|
37 | """ | |
|
38 | Public constructor | |
|
39 | ||
|
40 | Parameters | |
|
41 | ---------- | |
|
42 | config : Config | |
|
43 | Configuration file structure | |
|
44 | **kw : misc | |
|
45 | Additional arguments | |
|
46 | """ | |
|
47 | ||
|
48 | super(CSSHtmlHeaderTransformer, self).__init__(config=config, **kw) | |
|
49 | ||
|
50 | if self.enabled : | |
|
51 | self._regen_header() | |
|
52 | ||
|
53 | ||
|
54 | def __call__(self, nb, resources): | |
|
55 | """Fetch and add CSS to the resource dictionary | |
|
56 | ||
|
57 | Fetch CSS from IPython and Pygments to add at the beginning | |
|
58 | of the html files. Add this css in resources in the | |
|
59 | "inlining.css" key | |
|
60 | ||
|
61 | Parameters | |
|
62 | ---------- | |
|
63 | nb : NotebookNode | |
|
64 | Notebook being converted | |
|
65 | resources : dictionary | |
|
66 | Additional resources used in the conversion process. Allows | |
|
67 | transformers to pass variables into the Jinja engine. | |
|
68 | """ | |
|
69 | ||
|
70 | resources['inlining'] = {} | |
|
71 | resources['inlining']['css'] = self.header | |
|
72 | ||
|
73 | return nb, resources | |
|
74 | ||
|
75 | ||
|
76 | def _regen_header(self): | |
|
77 | """ | |
|
78 | Fills self.header with lines of CSS extracted from IPython | |
|
79 | and Pygments. | |
|
80 | """ | |
|
81 | ||
|
82 | #Clear existing header. | |
|
83 | header = [] | |
|
84 | ||
|
85 | #Construct path to IPy CSS | |
|
86 | sheet_filename = os.path.join(path.get_ipython_package_dir(), | |
|
87 | 'html', 'static', 'style', 'style.min.css') | |
|
88 | ||
|
89 | #Load style CSS file. | |
|
90 | try: | |
|
91 | with io.open(sheet_filename, encoding='utf-8') as file: | |
|
92 | file_text = file.read() | |
|
93 | header.append(file_text) | |
|
94 | except IOError: | |
|
95 | ||
|
96 | # New version of IPython with style.min.css, pass | |
|
97 | pass | |
|
98 | ||
|
99 | #Add pygments CSS | |
|
100 | pygments_css = HtmlFormatter().get_style_defs('.highlight') | |
|
101 | header.append(pygments_css) | |
|
102 | ||
|
103 | #Set header | |
|
104 | self.header = header | |
|
105 |
@@ -0,0 +1,143 b'' | |||
|
1 | """Module containing a transformer that extracts all of the figures from the | |
|
2 | notebook file. The extracted figures are returned in the 'resources' dictionary. | |
|
3 | """ | |
|
4 | #----------------------------------------------------------------------------- | |
|
5 | # Copyright (c) 2013, the IPython Development Team. | |
|
6 | # | |
|
7 | # Distributed under the terms of the Modified BSD License. | |
|
8 | # | |
|
9 | # The full license is in the file COPYING.txt, distributed with this software. | |
|
10 | #----------------------------------------------------------------------------- | |
|
11 | ||
|
12 | #----------------------------------------------------------------------------- | |
|
13 | # Imports | |
|
14 | #----------------------------------------------------------------------------- | |
|
15 | import itertools | |
|
16 | ||
|
17 | from IPython.utils.traitlets import Dict, Unicode | |
|
18 | from .activatable import ActivatableTransformer | |
|
19 | ||
|
20 | #----------------------------------------------------------------------------- | |
|
21 | # Constants | |
|
22 | #----------------------------------------------------------------------------- | |
|
23 | ||
|
24 | FIGURES_KEY = "figures" | |
|
25 | BINARY_KEY = "binary" | |
|
26 | TEXT_KEY = "text" | |
|
27 | ||
|
28 | #----------------------------------------------------------------------------- | |
|
29 | # Classes | |
|
30 | #----------------------------------------------------------------------------- | |
|
31 | ||
|
32 | class ExtractFigureTransformer(ActivatableTransformer): | |
|
33 | """ | |
|
34 | Extracts all of the figures from the notebook file. The extracted | |
|
35 | figures are returned in the 'resources' dictionary. | |
|
36 | """ | |
|
37 | ||
|
38 | extra_extension_map = Dict({}, | |
|
39 | config=True, | |
|
40 | help="""Extra map to override extension based on type. | |
|
41 | Useful for latex where SVG will be converted to PDF before inclusion | |
|
42 | """) | |
|
43 | ||
|
44 | key_format_map = Dict({}, config=True,) | |
|
45 | figure_name_format_map = Dict({}, config=True) | |
|
46 | ||
|
47 | #TODO: Change this to .format {} syntax | |
|
48 | default_key_template = Unicode('_fig_{index:02d}.{ext}', config=True) | |
|
49 | ||
|
50 | def __init__(self, config=None, **kw): | |
|
51 | """ | |
|
52 | Public constructor | |
|
53 | ||
|
54 | Parameters | |
|
55 | ---------- | |
|
56 | config : Config | |
|
57 | Configuration file structure | |
|
58 | **kw : misc | |
|
59 | Additional arguments | |
|
60 | """ | |
|
61 | ||
|
62 | super(ExtractFigureTransformer, self).__init__(config=config, **kw) | |
|
63 | ||
|
64 | # A unique index for association with extracted figures | |
|
65 | self.index_generator = itertools.count(1) | |
|
66 | ||
|
67 | def cell_transform(self, cell, resources, index): | |
|
68 | """ | |
|
69 | Apply a transformation on each cell, | |
|
70 | ||
|
71 | Parameters | |
|
72 | ---------- | |
|
73 | cell : NotebookNode cell | |
|
74 | Notebook cell being processed | |
|
75 | resources : dictionary | |
|
76 | Additional resources used in the conversion process. Allows | |
|
77 | transformers to pass variables into the Jinja engine. | |
|
78 | index : int | |
|
79 | Index of the cell being processed (see base.py) | |
|
80 | """ | |
|
81 | ||
|
82 | if resources.get(FIGURES_KEY, None) is None : | |
|
83 | resources[FIGURES_KEY] = {TEXT_KEY:{},BINARY_KEY:{}} | |
|
84 | ||
|
85 | for out in cell.get('outputs', []): | |
|
86 | for out_type in self.display_data_priority: | |
|
87 | ||
|
88 | if out.hasattr(out_type): | |
|
89 | figname, key, data, binary = self._new_figure(out[out_type], out_type) | |
|
90 | out['key_'+out_type] = figname | |
|
91 | ||
|
92 | if binary : | |
|
93 | resources[FIGURES_KEY][BINARY_KEY][key] = data | |
|
94 | else : | |
|
95 | resources[FIGURES_KEY][TEXT_KEY][key] = data | |
|
96 | ||
|
97 | index += 1 | |
|
98 | return cell, resources | |
|
99 | ||
|
100 | ||
|
101 | def _get_override_extension(self, extension): | |
|
102 | """Gets the overriden extension if it exists, else returns extension. | |
|
103 | ||
|
104 | Parameters | |
|
105 | ---------- | |
|
106 | extension : str | |
|
107 | File extension. | |
|
108 | """ | |
|
109 | ||
|
110 | if extension in self.extra_extension_map : | |
|
111 | return self.extra_extension_map[extension] | |
|
112 | ||
|
113 | return extension | |
|
114 | ||
|
115 | ||
|
116 | def _new_figure(self, data, format): | |
|
117 | """Create a new figure file in the given format. | |
|
118 | ||
|
119 | Parameters | |
|
120 | ---------- | |
|
121 | data : str | |
|
122 | Cell data (from Notebook node cell) | |
|
123 | format : str | |
|
124 | Figure format | |
|
125 | index : int | |
|
126 | Index of the figure being extracted | |
|
127 | """ | |
|
128 | ||
|
129 | figure_name_template = self.figure_name_format_map.get(format, self.default_key_template) | |
|
130 | key_template = self.key_format_map.get(format, self.default_key_template) | |
|
131 | ||
|
132 | #TODO: option to pass the hash as data? | |
|
133 | index = next(self.index_generator) | |
|
134 | figure_name = figure_name_template.format(index=index, ext=self._get_override_extension(format)) | |
|
135 | key = key_template.format(index=index, ext=self._get_override_extension(format)) | |
|
136 | ||
|
137 | #Binary files are base64-encoded, SVG is already XML | |
|
138 | binary = False | |
|
139 | if format in ('png', 'jpg', 'pdf'): | |
|
140 | data = data.decode('base64') | |
|
141 | binary = True | |
|
142 | ||
|
143 | return figure_name, key, data, binary |
@@ -0,0 +1,53 b'' | |||
|
1 | """Module that allows latex output notebooks to be conditioned before | |
|
2 | they are converted. | |
|
3 | """ | |
|
4 | #----------------------------------------------------------------------------- | |
|
5 | # Copyright (c) 2013, the IPython Development Team. | |
|
6 | # | |
|
7 | # Distributed under the terms of the Modified BSD License. | |
|
8 | # | |
|
9 | # The full license is in the file COPYING.txt, distributed with this software. | |
|
10 | #----------------------------------------------------------------------------- | |
|
11 | ||
|
12 | #----------------------------------------------------------------------------- | |
|
13 | # Imports | |
|
14 | #----------------------------------------------------------------------------- | |
|
15 | ||
|
16 | from __future__ import print_function, absolute_import | |
|
17 | ||
|
18 | # Our own imports | |
|
19 | # Needed to override transformer | |
|
20 | from .activatable import (ActivatableTransformer) | |
|
21 | from IPython.nbconvert import filters | |
|
22 | ||
|
23 | #----------------------------------------------------------------------------- | |
|
24 | # Classes | |
|
25 | #----------------------------------------------------------------------------- | |
|
26 | ||
|
27 | class LatexTransformer(ActivatableTransformer): | |
|
28 | """ | |
|
29 | Converter for latex destined documents. | |
|
30 | """ | |
|
31 | ||
|
32 | def cell_transform(self, cell, resources, index): | |
|
33 | """ | |
|
34 | Apply a transformation on each cell, | |
|
35 | ||
|
36 | Parameters | |
|
37 | ---------- | |
|
38 | cell : NotebookNode cell | |
|
39 | Notebook cell being processed | |
|
40 | resources : dictionary | |
|
41 | Additional resources used in the conversion process. Allows | |
|
42 | transformers to pass variables into the Jinja engine. | |
|
43 | index : int | |
|
44 | Modified index of the cell being processed (see base.py) | |
|
45 | """ | |
|
46 | ||
|
47 | #If the cell is a markdown cell, preprocess the ampersands used to | |
|
48 | #remove the space between them and their contents. Latex will complain | |
|
49 | #if spaces exist between the ampersands and the math content. | |
|
50 | #See filters.latex.rm_math_space for more information. | |
|
51 | if hasattr(cell, "source") and cell.cell_type == "markdown": | |
|
52 | cell.source = filters.rm_math_space(cell.source) | |
|
53 | return cell, resources |
@@ -0,0 +1,52 b'' | |||
|
1 | """Module that pre-processes the notebook for export via Reveal. | |
|
2 | """ | |
|
3 | #----------------------------------------------------------------------------- | |
|
4 | # Copyright (c) 2013, the IPython Development Team. | |
|
5 | # | |
|
6 | # Distributed under the terms of the Modified BSD License. | |
|
7 | # | |
|
8 | # The full license is in the file COPYING.txt, distributed with this software. | |
|
9 | #----------------------------------------------------------------------------- | |
|
10 | ||
|
11 | #----------------------------------------------------------------------------- | |
|
12 | # Imports | |
|
13 | #----------------------------------------------------------------------------- | |
|
14 | ||
|
15 | from .base import ConfigurableTransformer | |
|
16 | ||
|
17 | #----------------------------------------------------------------------------- | |
|
18 | # Classes and functions | |
|
19 | #----------------------------------------------------------------------------- | |
|
20 | ||
|
21 | class RevealHelpTransformer(ConfigurableTransformer): | |
|
22 | ||
|
23 | def call(self, nb, resources): | |
|
24 | """ | |
|
25 | Called once to 'transform' contents of the notebook. | |
|
26 | ||
|
27 | Parameters | |
|
28 | ---------- | |
|
29 | nb : NotebookNode | |
|
30 | Notebook being converted | |
|
31 | resources : dictionary | |
|
32 | Additional resources used in the conversion process. Allows | |
|
33 | transformers to pass variables into the Jinja engine. | |
|
34 | """ | |
|
35 | ||
|
36 | ||
|
37 | for worksheet in nb.worksheets : | |
|
38 | for i, cell in enumerate(worksheet.cells): | |
|
39 | ||
|
40 | #Make sure the cell has slideshow metadata. | |
|
41 | cell.metadata.align_type = cell.get('metadata', {}).get('slideshow', {}).get('align_type', 'Left') | |
|
42 | cell.metadata.slide_type = cell.get('metadata', {}).get('slideshow', {}).get('slide_type', '-') | |
|
43 | ||
|
44 | #Get the slide type. If type is start of subslide or slide, | |
|
45 | #end the last subslide/slide. | |
|
46 | if cell.metadata.slide_type in ['slide']: | |
|
47 | worksheet.cells[i - 1].metadata.slide_helper = 'slide_end' | |
|
48 | if cell.metadata.slide_type in ['subslide']: | |
|
49 | worksheet.cells[i - 1].metadata.slide_helper = 'subslide_end' | |
|
50 | ||
|
51 | return nb, resources | |
|
52 | No newline at end of file |
@@ -0,0 +1,261 b'' | |||
|
1 | """Module that allows custom Sphinx parameters to be set on the notebook and | |
|
2 | on the 'other' object passed into Jinja. Called prior to Jinja conversion | |
|
3 | process. | |
|
4 | """ | |
|
5 | #----------------------------------------------------------------------------- | |
|
6 | # Copyright (c) 2013, the IPython Development Team. | |
|
7 | # | |
|
8 | # Distributed under the terms of the Modified BSD License. | |
|
9 | # | |
|
10 | # The full license is in the file COPYING.txt, distributed with this software. | |
|
11 | #----------------------------------------------------------------------------- | |
|
12 | ||
|
13 | #----------------------------------------------------------------------------- | |
|
14 | # Imports | |
|
15 | #----------------------------------------------------------------------------- | |
|
16 | ||
|
17 | from __future__ import print_function, absolute_import | |
|
18 | ||
|
19 | # Stdlib imports | |
|
20 | # Used to find Sphinx package location | |
|
21 | import sphinx | |
|
22 | import os.path | |
|
23 | ||
|
24 | # Used to set the default date to today's date | |
|
25 | from datetime import date | |
|
26 | ||
|
27 | # Third-party imports | |
|
28 | # Needed for Pygments latex definitions. | |
|
29 | from pygments.formatters import LatexFormatter | |
|
30 | ||
|
31 | # Our own imports | |
|
32 | # Configurable traitlets | |
|
33 | from IPython.utils.traitlets import Unicode, Bool | |
|
34 | ||
|
35 | # Needed to override transformer | |
|
36 | from .activatable import (ActivatableTransformer) #TODO | |
|
37 | ||
|
38 | from IPython.nbconvert.utils import console | |
|
39 | ||
|
40 | #----------------------------------------------------------------------------- | |
|
41 | # Classes and functions | |
|
42 | #----------------------------------------------------------------------------- | |
|
43 | ||
|
44 | class SphinxTransformer(ActivatableTransformer): | |
|
45 | """ | |
|
46 | Sphinx utility transformer. | |
|
47 | ||
|
48 | This transformer is used to set variables needed by the latex to build | |
|
49 | Sphinx stylized templates. | |
|
50 | """ | |
|
51 | ||
|
52 | interactive = Bool(False, config=True, help=""" | |
|
53 | Allows you to define whether or not the Sphinx exporter will prompt | |
|
54 | you for input during the conversion process. If this is set to false, | |
|
55 | the author, version, release, date, and chapter_style traits should | |
|
56 | be set. | |
|
57 | """) | |
|
58 | ||
|
59 | author = Unicode("Unknown Author", config=True, help="Author name") | |
|
60 | ||
|
61 | version = Unicode("", config=True, help=""" | |
|
62 | Version number | |
|
63 | You can leave this blank if you do not want to render a version number. | |
|
64 | Example: "1.0.0" | |
|
65 | """) | |
|
66 | ||
|
67 | release = Unicode("", config=True, help=""" | |
|
68 | Release name | |
|
69 | You can leave this blank if you do not want to render a release name. | |
|
70 | Example: "Rough Draft" | |
|
71 | """) | |
|
72 | ||
|
73 | publish_date = Unicode("", config=True, help=""" | |
|
74 | Publish date | |
|
75 | This is the date to render on the document as the publish date. | |
|
76 | Leave this blank to default to todays date. | |
|
77 | Example: "June 12, 1990" | |
|
78 | """) | |
|
79 | ||
|
80 | chapter_style = Unicode("Bjarne", config=True, help=""" | |
|
81 | Sphinx chapter style | |
|
82 | This is the style to use for the chapter headers in the document. | |
|
83 | You may choose one of the following: | |
|
84 | "Bjarne" (default) | |
|
85 | "Lenny" | |
|
86 | "Glenn" | |
|
87 | "Conny" | |
|
88 | "Rejne" | |
|
89 | "Sonny" (used for international documents) | |
|
90 | """) | |
|
91 | ||
|
92 | output_style = Unicode("notebook", config=True, help=""" | |
|
93 | Nbconvert Ipython | |
|
94 | notebook input/output formatting style. | |
|
95 | You may choose one of the following: | |
|
96 | "simple (recommended for long code segments)" | |
|
97 | "notebook" (default) | |
|
98 | """) | |
|
99 | ||
|
100 | center_output = Bool(False, config=True, help=""" | |
|
101 | Optional attempt to center all output. If this is false, no additional | |
|
102 | formatting is applied. | |
|
103 | """) | |
|
104 | ||
|
105 | use_headers = Bool(True, config=True, help=""" | |
|
106 | Whether not a header should be added to the document. | |
|
107 | """) | |
|
108 | ||
|
109 | #Allow the user to override the title of the notebook (useful for | |
|
110 | #fancy document titles that the file system doesn't support.) | |
|
111 | overridetitle = Unicode("", config=True, help="") | |
|
112 | ||
|
113 | ||
|
114 | def call(self, nb, resources): | |
|
115 | """ | |
|
116 | Sphinx transformation to apply on each notebook. | |
|
117 | ||
|
118 | Parameters | |
|
119 | ---------- | |
|
120 | nb : NotebookNode | |
|
121 | Notebook being converted | |
|
122 | resources : dictionary | |
|
123 | Additional resources used in the conversion process. Allows | |
|
124 | transformers to pass variables into the Jinja engine. | |
|
125 | """ | |
|
126 | ||
|
127 | # TODO: Add versatile method of additional notebook metadata. Include | |
|
128 | # handling of multiple files. For now use a temporay namespace, | |
|
129 | # '_draft' to signify that this needs to change. | |
|
130 | if not "_draft" in nb.metadata: | |
|
131 | nb.metadata._draft = {} | |
|
132 | ||
|
133 | if not "sphinx" in resources: | |
|
134 | resources["sphinx"] = {} | |
|
135 | ||
|
136 | if self.interactive: | |
|
137 | ||
|
138 | # Prompt the user for additional meta data that doesn't exist currently | |
|
139 | # but would be usefull for Sphinx. | |
|
140 | nb.metadata._draft["author"] = self._prompt_author() | |
|
141 | nb.metadata._draft["version"] = self._prompt_version() | |
|
142 | nb.metadata._draft["release"] = self._prompt_release() | |
|
143 | nb.metadata._draft["date"] = self._prompt_date() | |
|
144 | ||
|
145 | # Prompt the user for the document style. | |
|
146 | resources["sphinx"]["chapterstyle"] = self._prompt_chapter_title_style() | |
|
147 | resources["sphinx"]["outputstyle"] = self._prompt_output_style() | |
|
148 | ||
|
149 | # Small options | |
|
150 | resources["sphinx"]["centeroutput"] = console.prompt_boolean("Do you want to center the output? (false)", False) | |
|
151 | resources["sphinx"]["header"] = console.prompt_boolean("Should a Sphinx document header be used? (true)", True) | |
|
152 | else: | |
|
153 | ||
|
154 | # Try to use the traitlets. | |
|
155 | nb.metadata._draft["author"] = self.author | |
|
156 | nb.metadata._draft["version"] = self.version | |
|
157 | nb.metadata._draft["release"] = self.release | |
|
158 | ||
|
159 | # Use todays date if none is provided. | |
|
160 | if len(self.publish_date.strip()) == 0: | |
|
161 | nb.metadata._draft["date"] = date.today().strftime("%B %-d, %Y") | |
|
162 | else: | |
|
163 | nb.metadata._draft["date"] = self.publish_date | |
|
164 | ||
|
165 | # Sphinx traitlets. | |
|
166 | resources["sphinx"]["chapterstyle"] = self.chapter_style | |
|
167 | resources["sphinx"]["outputstyle"] = self.output_style | |
|
168 | resources["sphinx"]["centeroutput"] = self.center_output | |
|
169 | resources["sphinx"]["header"] = self.use_headers | |
|
170 | ||
|
171 | # Find and pass in the path to the Sphinx dependencies. | |
|
172 | resources["sphinx"]["texinputs"] = os.path.abspath(sphinx.__file__ + "/../texinputs") | |
|
173 | ||
|
174 | # Generate Pygments definitions for Latex | |
|
175 | resources["sphinx"]["pygment_definitions"] = self._generate_pygments_latex_def() | |
|
176 | ||
|
177 | if not (self.overridetitle == None or len(self.overridetitle.strip()) == 0): | |
|
178 | nb.metadata.name = self.overridetitle | |
|
179 | ||
|
180 | # End | |
|
181 | return nb, resources | |
|
182 | ||
|
183 | ||
|
184 | def _generate_pygments_latex_def(self): | |
|
185 | """ | |
|
186 | Generate the pygments latex definitions that allows pygments | |
|
187 | to work in latex. | |
|
188 | """ | |
|
189 | ||
|
190 | return LatexFormatter().get_style_defs() | |
|
191 | ||
|
192 | ||
|
193 | def _prompt_author(self): | |
|
194 | """ | |
|
195 | Prompt the user to input an Author name | |
|
196 | """ | |
|
197 | return console.input("Author name: ") | |
|
198 | ||
|
199 | ||
|
200 | def _prompt_version(self): | |
|
201 | """ | |
|
202 | prompt the user to enter a version number | |
|
203 | """ | |
|
204 | return console.input("Version (ie ""1.0.0""): ") | |
|
205 | ||
|
206 | ||
|
207 | def _prompt_release(self): | |
|
208 | """ | |
|
209 | Prompt the user to input a release name | |
|
210 | """ | |
|
211 | ||
|
212 | return console.input("Release Name (ie ""Rough draft""): ") | |
|
213 | ||
|
214 | ||
|
215 | def _prompt_date(self): | |
|
216 | """ | |
|
217 | Prompt the user to enter a date | |
|
218 | """ | |
|
219 | ||
|
220 | default_date = date.today().strftime("%B %-d, %Y") | |
|
221 | user_date = console.input("Date (deafults to \"" + default_date + "\"): ") | |
|
222 | if len(user_date.strip()) == 0: | |
|
223 | user_date = default_date | |
|
224 | return user_date | |
|
225 | ||
|
226 | ||
|
227 | def _prompt_output_style(self): | |
|
228 | """ | |
|
229 | Prompts the user to pick an IPython output style. | |
|
230 | """ | |
|
231 | ||
|
232 | # Dictionary of available output styles | |
|
233 | styles = {1: "simple", | |
|
234 | 2: "notebook"} | |
|
235 | ||
|
236 | #Append comments to the menu when displaying it to the user. | |
|
237 | comments = {1: "(recommended for long code segments)", | |
|
238 | 2: "(default)"} | |
|
239 | ||
|
240 | return console.prompt_dictionary(styles, default_style=2, menu_comments=comments) | |
|
241 | ||
|
242 | ||
|
243 | def _prompt_chapter_title_style(self): | |
|
244 | """ | |
|
245 | Prompts the user to pick a Sphinx chapter style | |
|
246 | """ | |
|
247 | ||
|
248 | # Dictionary of available Sphinx styles | |
|
249 | styles = {1: "Bjarne", | |
|
250 | 2: "Lenny", | |
|
251 | 3: "Glenn", | |
|
252 | 4: "Conny", | |
|
253 | 5: "Rejne", | |
|
254 | 6: "Sonny"} | |
|
255 | ||
|
256 | #Append comments to the menu when displaying it to the user. | |
|
257 | comments = {1: "(default)", | |
|
258 | 6: "(for international documents)"} | |
|
259 | ||
|
260 | return console.prompt_dictionary(styles, menu_comments=comments) | |
|
261 |
|
1 | NO CONTENT: new file 100755 |
@@ -0,0 +1,37 b'' | |||
|
1 | """Global configuration class.""" | |
|
2 | #----------------------------------------------------------------------------- | |
|
3 | # Copyright (c) 2013, the IPython Development Team. | |
|
4 | # | |
|
5 | # Distributed under the terms of the Modified BSD License. | |
|
6 | # | |
|
7 | # The full license is in the file COPYING.txt, distributed with this software. | |
|
8 | #----------------------------------------------------------------------------- | |
|
9 | ||
|
10 | #----------------------------------------------------------------------------- | |
|
11 | # Imports | |
|
12 | #----------------------------------------------------------------------------- | |
|
13 | ||
|
14 | from IPython.utils.traitlets import List | |
|
15 | from IPython.config.configurable import Configurable | |
|
16 | ||
|
17 | #----------------------------------------------------------------------------- | |
|
18 | # Classes and functions | |
|
19 | #----------------------------------------------------------------------------- | |
|
20 | ||
|
21 | class GlobalConfigurable(Configurable): | |
|
22 | """Global configurable class for shared config | |
|
23 | ||
|
24 | Usefull for display data priority that might be use by many trasnformers | |
|
25 | """ | |
|
26 | ||
|
27 | display_data_priority = List(['html', 'pdf', 'svg', 'latex', 'png', 'jpg', 'jpeg' , 'text'], | |
|
28 | config=True, | |
|
29 | help= """ | |
|
30 | An ordered list of prefered output type, the first | |
|
31 | encounterd will usually be used when converting discarding | |
|
32 | the others. | |
|
33 | """ | |
|
34 | ) | |
|
35 | ||
|
36 | def __init__(self, config=None, **kw): | |
|
37 | super(GlobalConfigurable, self).__init__( config=config, **kw) |
@@ -0,0 +1,120 b'' | |||
|
1 | """Utility functions for interacting with the console""" | |
|
2 | #----------------------------------------------------------------------------- | |
|
3 | # Copyright (c) 2013, the IPython Development Team. | |
|
4 | # | |
|
5 | # Distributed under the terms of the Modified BSD License. | |
|
6 | # | |
|
7 | # The full license is in the file COPYING.txt, distributed with this software. | |
|
8 | #----------------------------------------------------------------------------- | |
|
9 | ||
|
10 | #----------------------------------------------------------------------------- | |
|
11 | # Imports | |
|
12 | #----------------------------------------------------------------------------- | |
|
13 | ||
|
14 | # Used to determine python version | |
|
15 | import sys | |
|
16 | ||
|
17 | #----------------------------------------------------------------------------- | |
|
18 | # Classes and functions | |
|
19 | #----------------------------------------------------------------------------- | |
|
20 | ||
|
21 | def input(prompt_text): | |
|
22 | """ | |
|
23 | Prompt the user for input. | |
|
24 | ||
|
25 | The input command will change depending on the version of python | |
|
26 | installed. To maintain support for 2 and earlier, we must use | |
|
27 | raw_input in that case. Else use input. | |
|
28 | ||
|
29 | Parameters | |
|
30 | ---------- | |
|
31 | prompt_text : str | |
|
32 | Prompt to display to the user. | |
|
33 | """ | |
|
34 | ||
|
35 | # Try to get the python version. This command is only available in | |
|
36 | # python 2 and later, so it's important that we catch the exception | |
|
37 | # if the command isn't found. | |
|
38 | try: | |
|
39 | majorversion = sys.version_info[0] | |
|
40 | except AttributeError: | |
|
41 | majorversion = 1 | |
|
42 | ||
|
43 | # Use the correct function to prompt the user for input depending on | |
|
44 | # what python version the code is running in. | |
|
45 | if majorversion >= 3: | |
|
46 | return input(prompt_text) | |
|
47 | else: | |
|
48 | return raw_input(prompt_text).decode(sys.stdin.encoding) | |
|
49 | ||
|
50 | ||
|
51 | def prompt_boolean(prompt, default=False): | |
|
52 | """ | |
|
53 | Prompt the user for a boolean response. | |
|
54 | ||
|
55 | Parameters | |
|
56 | ---------- | |
|
57 | prompt : str | |
|
58 | prompt to display to the user | |
|
59 | default : bool, optional | |
|
60 | response to return if none is given by the user | |
|
61 | """ | |
|
62 | ||
|
63 | response = input(prompt) | |
|
64 | response = response.strip().lower() | |
|
65 | ||
|
66 | #Catch 1, true, yes as True | |
|
67 | if len(response) > 0 and (response == "1" or response[0] == "t" or response[0] == "y"): | |
|
68 | return True | |
|
69 | ||
|
70 | #Catch 0, false, no as False | |
|
71 | elif len(response) > 0 and (response == "0" or response[0] == "f" or response[0] == "n"): | |
|
72 | return False | |
|
73 | ||
|
74 | else: | |
|
75 | return default | |
|
76 | ||
|
77 | ||
|
78 | def prompt_dictionary(choices, default_style=1, menu_comments={}): | |
|
79 | """ | |
|
80 | Prompt the user to chose one of many selections from a menu. | |
|
81 | ||
|
82 | Parameters | |
|
83 | ---------- | |
|
84 | choices : dictionary | |
|
85 | Keys - choice numbers (int) | |
|
86 | Values - choice value (str), this is what the function will return | |
|
87 | default_style : int, optional | |
|
88 | Choice to select if the user doesn't respond | |
|
89 | menu_comments : dictionary, optional | |
|
90 | Additional comments to append to the menu as it is displayed | |
|
91 | in the console. | |
|
92 | Keys - choice numbers (int) | |
|
93 | Values - comment (str), what will be appended to the | |
|
94 | corresponding choice | |
|
95 | """ | |
|
96 | ||
|
97 | # Build the menu that will be displayed to the user with | |
|
98 | # all of the options available. | |
|
99 | prompt = "" | |
|
100 | for key, value in choices.iteritems(): | |
|
101 | prompt += "%d %s " % (key, value) | |
|
102 | if key in menu_comments: | |
|
103 | prompt += menu_comments[key] | |
|
104 | prompt += "\n" | |
|
105 | ||
|
106 | # Continue to ask the user for a style until an appropriate | |
|
107 | # one is specified. | |
|
108 | response = -1 | |
|
109 | while (not response in choices): | |
|
110 | try: | |
|
111 | text_response = input(prompt) | |
|
112 | ||
|
113 | # Use default option if no input. | |
|
114 | if len(text_response.strip()) == 0: | |
|
115 | response = default_style | |
|
116 | else: | |
|
117 | response = int(text_response) | |
|
118 | except ValueError: | |
|
119 | print("Error: Value is not an available option. 0 selects the default.\n") | |
|
120 | return choices[response] |
@@ -0,0 +1,17 b'' | |||
|
1 | """NbConvert specific exceptions""" | |
|
2 | #----------------------------------------------------------------------------- | |
|
3 | # Copyright (c) 2013, the IPython Development Team. | |
|
4 | # | |
|
5 | # Distributed under the terms of the Modified BSD License. | |
|
6 | # | |
|
7 | # The full license is in the file COPYING.txt, distributed with this software. | |
|
8 | #----------------------------------------------------------------------------- | |
|
9 | ||
|
10 | #----------------------------------------------------------------------------- | |
|
11 | # Classes and functions | |
|
12 | #----------------------------------------------------------------------------- | |
|
13 | ||
|
14 | class ConversionException(Exception): | |
|
15 | """An exception raised by the conversion process.""" | |
|
16 | ||
|
17 | pass No newline at end of file |
@@ -0,0 +1,46 b'' | |||
|
1 | """A custom pygments lexer for IPython code cells. | |
|
2 | ||
|
3 | Informs The pygments highlighting library of the quirks of IPython's superset | |
|
4 | of Python -- magic commands, !shell commands, etc. | |
|
5 | """ | |
|
6 | #----------------------------------------------------------------------------- | |
|
7 | # Copyright (c) 2013, the IPython Development Team. | |
|
8 | # | |
|
9 | # Distributed under the terms of the Modified BSD License. | |
|
10 | # | |
|
11 | # The full license is in the file COPYING.txt, distributed with this software. | |
|
12 | #----------------------------------------------------------------------------- | |
|
13 | ||
|
14 | #----------------------------------------------------------------------------- | |
|
15 | # Imports | |
|
16 | #----------------------------------------------------------------------------- | |
|
17 | ||
|
18 | # Third-party imports | |
|
19 | from pygments.lexers import PythonLexer, BashLexer | |
|
20 | from pygments.lexer import bygroups, using | |
|
21 | from pygments.token import Keyword, Operator, Text | |
|
22 | ||
|
23 | #----------------------------------------------------------------------------- | |
|
24 | # Class declarations | |
|
25 | #----------------------------------------------------------------------------- | |
|
26 | ||
|
27 | class IPythonLexer(PythonLexer): | |
|
28 | """ | |
|
29 | Pygments Lexer for use with IPython code. Inherits from | |
|
30 | PythonLexer and adds information about IPython specific | |
|
31 | keywords (i.e. magic commands, shell commands, etc.) | |
|
32 | """ | |
|
33 | ||
|
34 | #Basic properties | |
|
35 | name = 'IPython' | |
|
36 | aliases = ['ip', 'ipython'] | |
|
37 | filenames = ['*.ipy'] | |
|
38 | ||
|
39 | #Highlighting information | |
|
40 | tokens = PythonLexer.tokens.copy() | |
|
41 | tokens['root'] = [ | |
|
42 | (r'(\%+)(\w+)\s+(\.*)(\n)', bygroups(Operator, Keyword, | |
|
43 | using(BashLexer), Text)), | |
|
44 | (r'(\%+)(\w+)\b', bygroups(Operator, Keyword)), | |
|
45 | (r'^(!)(.+)(\n)', bygroups(Operator, using(BashLexer), Text)), | |
|
46 | ] + tokens['root'] |
@@ -81,6 +81,8 b' ipython help profile # show the help for the profile subcmd' | |||
|
81 | 81 | |
|
82 | 82 | ipython locate # print the path to the IPython directory |
|
83 | 83 | ipython locate profile foo # print the path to the directory for profile `foo` |
|
84 | ||
|
85 | ipython nbconvert # convert notebooks to/from other formats | |
|
84 | 86 | """ |
|
85 | 87 | |
|
86 | 88 | #----------------------------------------------------------------------------- |
@@ -244,6 +246,9 b' class TerminalIPythonApp(BaseIPythonApplication, InteractiveShellApp):' | |||
|
244 | 246 | history=('IPython.core.historyapp.HistoryApp', |
|
245 | 247 | "Manage the IPython history database." |
|
246 | 248 | ), |
|
249 | nbconvert=('IPython.nbconvert.nbconvertapp.NbConvertApp', | |
|
250 | "Convert notebooks to/from other formats." | |
|
251 | ), | |
|
247 | 252 | )) |
|
248 | 253 | |
|
249 | 254 | # *do* autocreate requested profile, but don't create the config file. |
@@ -166,6 +166,8 b" have['jinja2'] = test_for('jinja2')" | |||
|
166 | 166 | have['wx'] = test_for('wx') |
|
167 | 167 | have['wx.aui'] = test_for('wx.aui') |
|
168 | 168 | have['azure'] = test_for('azure') |
|
169 | have['sphinx'] = test_for('sphinx') | |
|
170 | have['markdown'] = test_for('markdown') | |
|
169 | 171 | |
|
170 | 172 | min_zmq = (2,1,11) |
|
171 | 173 | |
@@ -305,6 +307,9 b' def make_exclude():' | |||
|
305 | 307 | if not have['azure']: |
|
306 | 308 | exclusions.append(ipjoin('html', 'services', 'notebooks', 'azurenbmanager')) |
|
307 | 309 | |
|
310 | if not all((have['pygments'], have['jinja2'], have['markdown'], have['sphinx'])): | |
|
311 | exclusions.append(ipjoin('nbconvert')) | |
|
312 | ||
|
308 | 313 | # This is needed for the reg-exp to match on win32 in the ipdoctest plugin. |
|
309 | 314 | if sys.platform == 'win32': |
|
310 | 315 | exclusions = [s.replace('\\','\\\\') for s in exclusions] |
@@ -440,7 +445,7 b' def make_runners(inc_slow=False):' | |||
|
440 | 445 | |
|
441 | 446 | # Packages to be tested via nose, that only depend on the stdlib |
|
442 | 447 | nose_pkg_names = ['config', 'core', 'extensions', 'lib', 'terminal', |
|
443 |
'testing', 'utils', 'nbformat' |
|
|
448 | 'testing', 'utils', 'nbformat'] | |
|
444 | 449 | |
|
445 | 450 | if have['qt']: |
|
446 | 451 | nose_pkg_names.append('qt') |
@@ -454,6 +459,9 b' def make_runners(inc_slow=False):' | |||
|
454 | 459 | if inc_slow: |
|
455 | 460 | nose_pkg_names.append('parallel') |
|
456 | 461 | |
|
462 | if all((have['pygments'], have['jinja2'], have['markdown'], have['sphinx'])): | |
|
463 | nose_pkg_names.append('nbconvert') | |
|
464 | ||
|
457 | 465 | # For debugging this code, only load quick stuff |
|
458 | 466 | #nose_pkg_names = ['core', 'extensions'] # dbg |
|
459 | 467 |
@@ -278,6 +278,7 b" if 'setuptools' in sys.modules:" | |||
|
278 | 278 | doc = 'Sphinx>=0.3', |
|
279 | 279 | test = 'nose>=0.10.1', |
|
280 | 280 | notebook = ['tornado>=2.0', 'pyzmq>=2.1.11', 'jinja2'], |
|
281 | nbconvert = ['pygments', 'markdown', 'jinja2', 'Sphinx>=0.3'] | |
|
281 | 282 | ) |
|
282 | 283 | requires = setup_args.setdefault('install_requires', []) |
|
283 | 284 | setupext.display_status = False |
@@ -151,6 +151,8 b' def find_package_data():' | |||
|
151 | 151 | 'IPython.testing.plugin' : ['*.txt'], |
|
152 | 152 | 'IPython.html' : ['templates/*'] + static_data, |
|
153 | 153 | 'IPython.qt.console' : ['resources/icon/*.svg'], |
|
154 | 'IPython.nbconvert.templates' : ['*.tpl', 'latex/*.tpl', | |
|
155 | 'latex/skeleton/*.tplx', 'skeleton/*'] | |
|
154 | 156 | } |
|
155 | 157 | return package_data |
|
156 | 158 | |
@@ -320,7 +322,7 b" def find_scripts(entry_points=False, suffix=''):" | |||
|
320 | 322 | 'iplogger%s = IPython.parallel.apps.iploggerapp:launch_new_instance', |
|
321 | 323 | 'ipcluster%s = IPython.parallel.apps.ipclusterapp:launch_new_instance', |
|
322 | 324 | 'iptest%s = IPython.testing.iptest:main', |
|
323 | 'irunner%s = IPython.lib.irunner:main' | |
|
325 | 'irunner%s = IPython.lib.irunner:main', | |
|
324 | 326 | ]] |
|
325 | 327 | gui_scripts = [] |
|
326 | 328 | scripts = dict(console_scripts=console_scripts, gui_scripts=gui_scripts) |
@@ -352,7 +354,8 b' def check_for_dependencies():' | |||
|
352 | 354 | print_line, print_raw, print_status, |
|
353 | 355 | check_for_sphinx, check_for_pygments, |
|
354 | 356 | check_for_nose, check_for_pexpect, |
|
355 | check_for_pyzmq, check_for_readline | |
|
357 | check_for_pyzmq, check_for_readline, | |
|
358 | check_for_jinja2, check_for_markdown | |
|
356 | 359 | ) |
|
357 | 360 | print_line() |
|
358 | 361 | print_raw("BUILDING IPYTHON") |
@@ -370,6 +373,8 b' def check_for_dependencies():' | |||
|
370 | 373 | check_for_pexpect() |
|
371 | 374 | check_for_pyzmq() |
|
372 | 375 | check_for_readline() |
|
376 | check_for_jinja2() | |
|
377 | check_for_markdown() | |
|
373 | 378 | |
|
374 | 379 | #--------------------------------------------------------------------------- |
|
375 | 380 | # VCS related |
@@ -67,7 +67,7 b' def check_for_sphinx():' | |||
|
67 | 67 | try: |
|
68 | 68 | import sphinx |
|
69 | 69 | except ImportError: |
|
70 |
print_status('sphinx', "Not found (required for |
|
|
70 | print_status('sphinx', "Not found (required for docs and nbconvert)") | |
|
71 | 71 | return False |
|
72 | 72 | else: |
|
73 | 73 | print_status('sphinx', sphinx.__version__) |
@@ -77,60 +77,50 b' def check_for_pygments():' | |||
|
77 | 77 | try: |
|
78 | 78 | import pygments |
|
79 | 79 | except ImportError: |
|
80 |
print_status('pygments', "Not found (required for |
|
|
80 | print_status('pygments', "Not found (required for docs and nbconvert)") | |
|
81 | 81 | return False |
|
82 | 82 | else: |
|
83 | 83 | print_status('pygments', pygments.__version__) |
|
84 | 84 | return True |
|
85 | 85 | |
|
86 |
def check_for_ |
|
|
87 | try: | |
|
88 | import nose | |
|
89 | except ImportError: | |
|
90 | print_status('nose', "Not found (required for running the test suite)") | |
|
91 | return False | |
|
92 | else: | |
|
93 | print_status('nose', nose.__version__) | |
|
94 | return True | |
|
95 | ||
|
96 | def check_for_pexpect(): | |
|
86 | def check_for_jinja2(): | |
|
97 | 87 | try: |
|
98 |
import |
|
|
88 | import jinja2 | |
|
99 | 89 | except ImportError: |
|
100 |
print_status( |
|
|
90 | print_status('jinja2', "Not found (required for notebook and nbconvert)") | |
|
101 | 91 | return False |
|
102 | 92 | else: |
|
103 |
print_status( |
|
|
93 | print_status('jinja2', jinja2.__version__) | |
|
104 | 94 | return True |
|
105 | 95 | |
|
106 |
def check_for_ |
|
|
96 | def check_for_markdown(): | |
|
107 | 97 | try: |
|
108 |
import |
|
|
98 | import markdown | |
|
109 | 99 | except ImportError: |
|
110 |
print_status( |
|
|
100 | print_status('pygments', "Not found (required for nbconvert)") | |
|
111 | 101 | return False |
|
112 | 102 | else: |
|
113 | print_status("httplib2","yes") | |
|
103 | print_status('markdown', markdown.version) | |
|
114 | 104 | return True |
|
115 | 105 | |
|
116 |
def check_for_s |
|
|
106 | def check_for_nose(): | |
|
117 | 107 | try: |
|
118 |
import s |
|
|
108 | import nose | |
|
119 | 109 | except ImportError: |
|
120 |
print_status( |
|
|
110 | print_status('nose', "Not found (required for running the test suite)") | |
|
121 | 111 | return False |
|
122 | 112 | else: |
|
123 |
print_status( |
|
|
113 | print_status('nose', nose.__version__) | |
|
124 | 114 | return True |
|
125 | 115 | |
|
126 |
def check_for_ |
|
|
116 | def check_for_pexpect(): | |
|
127 | 117 | try: |
|
128 |
import |
|
|
118 | import pexpect | |
|
129 | 119 | except ImportError: |
|
130 |
print_status(" |
|
|
120 | print_status("pexpect", "no (required for running standalone doctests)") | |
|
131 | 121 | return False |
|
132 | 122 | else: |
|
133 | print_status("simplejson","yes") | |
|
123 | print_status("pexpect", pexpect.__version__) | |
|
134 | 124 | return True |
|
135 | 125 | |
|
136 | 126 | def check_for_pyzmq(): |
General Comments 0
You need to be logged in to leave comments.
Login now