Show More
@@ -0,0 +1,23 b'' | |||||
|
1 | """Latex transformer. | |||
|
2 | ||||
|
3 | Module that allows latex output notebooks to be conditioned before | |||
|
4 | they are converted. | |||
|
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 | # Functions | |||
|
16 | #----------------------------------------------------------------------------- | |||
|
17 | def export_sphinx_report(nb, config=None): | |||
|
18 | pass | |||
|
19 | def export_sphinx_report(nb, fileName, config=None): | |||
|
20 | pass | |||
|
21 | ||||
|
22 | #TODO: Add basic export/import utility functions. | |||
|
23 | __author__ = 'root' |
@@ -0,0 +1,82 b'' | |||||
|
1 | # ANSI color functions: | |||
|
2 | import re | |||
|
3 | def remove_ansi(src): | |||
|
4 | """Strip all ANSI color escape sequences from input string. | |||
|
5 | ||||
|
6 | Parameters | |||
|
7 | ---------- | |||
|
8 | src : string | |||
|
9 | ||||
|
10 | Returns | |||
|
11 | ------- | |||
|
12 | string | |||
|
13 | """ | |||
|
14 | return re.sub(r'\033\[(0|\d;\d\d)m', '', src) | |||
|
15 | ||||
|
16 | ||||
|
17 | def ansi2html(txt): | |||
|
18 | """Render ANSI colors as HTML colors | |||
|
19 | ||||
|
20 | This is equivalent to util.fixConsole in utils.js | |||
|
21 | ||||
|
22 | Parameters | |||
|
23 | ---------- | |||
|
24 | txt : string | |||
|
25 | ||||
|
26 | Returns | |||
|
27 | ------- | |||
|
28 | string | |||
|
29 | """ | |||
|
30 | ||||
|
31 | ansi_colormap = { | |||
|
32 | '30': 'ansiblack', | |||
|
33 | '31': 'ansired', | |||
|
34 | '32': 'ansigreen', | |||
|
35 | '33': 'ansiyellow', | |||
|
36 | '34': 'ansiblue', | |||
|
37 | '35': 'ansipurple', | |||
|
38 | '36': 'ansicyan', | |||
|
39 | '37': 'ansigrey', | |||
|
40 | '01': 'ansibold', | |||
|
41 | } | |||
|
42 | ||||
|
43 | # do ampersand first | |||
|
44 | txt = txt.replace('&', '&') | |||
|
45 | html_escapes = { | |||
|
46 | '<': '<', | |||
|
47 | '>': '>', | |||
|
48 | "'": ''', | |||
|
49 | '"': '"', | |||
|
50 | '`': '`', | |||
|
51 | } | |||
|
52 | for c, escape in html_escapes.iteritems(): | |||
|
53 | txt = txt.replace(c, escape) | |||
|
54 | ||||
|
55 | ansi_re = re.compile('\x1b' + r'\[([\dA-Fa-f;]*?)m') | |||
|
56 | m = ansi_re.search(txt) | |||
|
57 | opened = False | |||
|
58 | cmds = [] | |||
|
59 | opener = '' | |||
|
60 | closer = '' | |||
|
61 | while m: | |||
|
62 | cmds = m.groups()[0].split(';') | |||
|
63 | closer = '</span>' if opened else '' | |||
|
64 | # True if there is there more than one element in cmds, *or* | |||
|
65 | # if there is only one but it is not equal to a string of zeroes. | |||
|
66 | opened = len(cmds) > 1 or cmds[0] != '0' * len(cmds[0]) | |||
|
67 | classes = [] | |||
|
68 | for cmd in cmds: | |||
|
69 | if cmd in ansi_colormap: | |||
|
70 | classes.append(ansi_colormap.get(cmd)) | |||
|
71 | ||||
|
72 | if classes: | |||
|
73 | opener = '<span class="%s">' % (' '.join(classes)) | |||
|
74 | else: | |||
|
75 | opener = '' | |||
|
76 | txt = re.sub(ansi_re, closer + opener, txt, 1) | |||
|
77 | ||||
|
78 | m = ansi_re.search(txt) | |||
|
79 | ||||
|
80 | if opened: | |||
|
81 | txt += '</span>' | |||
|
82 | return txt |
@@ -0,0 +1,39 b'' | |||||
|
1 | # Our own imports | |||
|
2 | from utils.lexers import IPythonLexer | |||
|
3 | ||||
|
4 | #----------------------------------------------------------------------------- | |||
|
5 | # Globals and constants | |||
|
6 | #----------------------------------------------------------------------------- | |||
|
7 | _multiline_outputs = ['text', 'html', 'svg', 'latex', 'javascript', 'json'] | |||
|
8 | ||||
|
9 | ||||
|
10 | #----------------------------------------------------------------------------- | |||
|
11 | # Utility functions | |||
|
12 | #----------------------------------------------------------------------------- | |||
|
13 | def highlight(src, lang='ipython'): | |||
|
14 | """ | |||
|
15 | Return a syntax-highlighted version of the input source as html output. | |||
|
16 | """ | |||
|
17 | from pygments.formatters import HtmlFormatter | |||
|
18 | return pygment_highlight(src, HtmlFormatter(), lang) | |||
|
19 | ||||
|
20 | def highlight2latex(src, lang='ipython'): | |||
|
21 | """ | |||
|
22 | Return a syntax-highlighted version of the input source as latex output. | |||
|
23 | """ | |||
|
24 | from pygments.formatters import LatexFormatter | |||
|
25 | return pygment_highlight(src, LatexFormatter(), lang) | |||
|
26 | ||||
|
27 | def pygment_highlight(src, output_formatter, lang='ipython'): | |||
|
28 | """ | |||
|
29 | Return a syntax-highlighted version of the input source | |||
|
30 | """ | |||
|
31 | from pygments import highlight | |||
|
32 | from pygments.lexers import get_lexer_by_name | |||
|
33 | ||||
|
34 | if lang == 'ipython': | |||
|
35 | lexer = IPythonLexer() | |||
|
36 | else: | |||
|
37 | lexer = get_lexer_by_name(lang, stripall=True) | |||
|
38 | ||||
|
39 | return highlight(src, lexer, output_formatter) |
@@ -0,0 +1,43 b'' | |||||
|
1 | ||||
|
2 | def cell_preprocessor(function): | |||
|
3 | """ wrap a function to be executed on all cells of a notebook | |||
|
4 | ||||
|
5 | wrapped function parameters : | |||
|
6 | cell : the cell | |||
|
7 | other : external resources | |||
|
8 | index : index of the cell | |||
|
9 | """ | |||
|
10 | def wrappedfunc(nb, other): | |||
|
11 | for worksheet in nb.worksheets : | |||
|
12 | for index, cell in enumerate(worksheet.cells): | |||
|
13 | worksheet.cells[index], other = function(cell, other, index) | |||
|
14 | return nb, other | |||
|
15 | return wrappedfunc | |||
|
16 | ||||
|
17 | ||||
|
18 | @cell_preprocessor | |||
|
19 | def coalesce_streams(cell, other, count): | |||
|
20 | """merge consecutive sequences of stream output into single stream | |||
|
21 | ||||
|
22 | to prevent extra newlines inserted at flush calls | |||
|
23 | ||||
|
24 | TODO: handle \r deletion | |||
|
25 | """ | |||
|
26 | outputs = cell.get('outputs', []) | |||
|
27 | if not outputs: | |||
|
28 | return cell, other | |||
|
29 | new_outputs = [] | |||
|
30 | last = outputs[0] | |||
|
31 | new_outputs = [last] | |||
|
32 | for output in outputs[1:]: | |||
|
33 | if (output.output_type == 'stream' and | |||
|
34 | last.output_type == 'stream' and | |||
|
35 | last.stream == output.stream | |||
|
36 | ): | |||
|
37 | last.text += output.text | |||
|
38 | else: | |||
|
39 | new_outputs.append(output) | |||
|
40 | ||||
|
41 | cell.outputs = new_outputs | |||
|
42 | return cell, other | |||
|
43 |
1 | NO CONTENT: modified file chmod 100644 => 100755 |
|
NO CONTENT: modified file chmod 100644 => 100755 |
@@ -23,7 +23,6 b' from __future__ import print_function, absolute_import' | |||||
23 | # Stdlib imports |
|
23 | # Stdlib imports | |
24 | import io |
|
24 | import io | |
25 | import os |
|
25 | import os | |
26 | import re |
|
|||
27 |
|
26 | |||
28 | # IPython imports |
|
27 | # IPython imports | |
29 | from IPython.config.configurable import Configurable |
|
28 | from IPython.config.configurable import Configurable | |
@@ -35,16 +34,19 b' from IPython.utils.text import indent' | |||||
35 | from jinja2 import Environment, FileSystemLoader |
|
34 | from jinja2 import Environment, FileSystemLoader | |
36 | from markdown import markdown |
|
35 | from markdown import markdown | |
37 |
|
36 | |||
38 | # local import (pre-transformers) |
|
37 | # local import | |
39 | from exceptions import ConversionException |
|
38 | import filters.strings | |
40 | from . import transformers as trans #TODO |
|
39 | import filters.markdown | |
41 | from .utils import get_lines #TODO |
|
40 | import filters.latex | |
42 | from .utils import remove_ansi #TODO |
|
41 | import filters.datatypefilter | |
43 | from .utils import highlight, ansi2html #TODO |
|
42 | import filters.pygments | |
|
43 | import filters.ansi | |||
|
44 | ||||
|
45 | import transformers.extractfigure | |||
|
46 | import transformers.csshtmlheader | |||
|
47 | import transformers.revealhelp | |||
|
48 | import transformers.coalescestreams | |||
44 |
|
49 | |||
45 | import .utils.strings as strings |
|
|||
46 | import .utils.markdown as markdown_utils |
|
|||
47 | import .utils.datatypefilter.DataTypeFilter as DataTypeFilter |
|
|||
48 |
|
50 | |||
49 | #----------------------------------------------------------------------------- |
|
51 | #----------------------------------------------------------------------------- | |
50 | # Globals and constants |
|
52 | # Globals and constants | |
@@ -64,7 +66,7 b" JINJA_EXTENSIONS = ['jinja2.ext.loopcontrols']" | |||||
64 | class Exporter(Configurable): |
|
66 | class Exporter(Configurable): | |
65 | """ A Jinja2 base converter templates |
|
67 | """ A Jinja2 base converter templates | |
66 |
|
68 | |||
67 |
Preprocess the |
|
69 | Pre-process the IPYNB files, feed it through Jinja2 templates, | |
68 | and spit an converted files and a data object with other data |
|
70 | and spit an converted files and a data object with other data | |
69 | should be mostly configurable |
|
71 | should be mostly configurable | |
70 | """ |
|
72 | """ | |
@@ -72,17 +74,11 b' class Exporter(Configurable):' | |||||
72 | pre_transformer_order = List(['haspyout_transformer'], |
|
74 | pre_transformer_order = List(['haspyout_transformer'], | |
73 | config=True, |
|
75 | config=True, | |
74 | help= """ |
|
76 | help= """ | |
75 |
An ordered list of pre |
|
77 | An ordered list of pre-transformer to apply to the IPYNB | |
76 | file before running through templates |
|
78 | file before running through templates | |
77 | """ |
|
79 | """ | |
78 | ) |
|
80 | ) | |
79 |
|
81 | |||
80 | #TODO: Flagged for removal. |
|
|||
81 | tex_environement = Bool( |
|
|||
82 | False, |
|
|||
83 | config=True, |
|
|||
84 | help=" Whether or not the user is exporting to latex.") |
|
|||
85 |
|
||||
86 | template_file = Unicode( |
|
82 | template_file = Unicode( | |
87 | '', config=True, |
|
83 | '', config=True, | |
88 | help="Name of the template file to use") |
|
84 | help="Name of the template file to use") | |
@@ -94,7 +90,7 b' class Exporter(Configurable):' | |||||
94 |
|
90 | |||
95 | stdout = Bool( |
|
91 | stdout = Bool( | |
96 | True, config=True, |
|
92 | True, config=True, | |
97 |
help="""Whether to print the converted |
|
93 | help="""Whether to print the converted IPYNB file to stdout | |
98 | "use full do diff files without actually writing a new file""" |
|
94 | "use full do diff files without actually writing a new file""" | |
99 | ) |
|
95 | ) | |
100 |
|
96 | |||
@@ -130,67 +126,46 b' class Exporter(Configurable):' | |||||
130 | user defined filter will overwrite the one availlable by default. |
|
126 | user defined filter will overwrite the one availlable by default. | |
131 | """ |
|
127 | """ | |
132 |
|
128 | |||
133 | #Merge default config options with user specific override options. |
|
129 | #Set the default options for the exporter. | |
134 |
default_config = self. |
|
130 | default_config = self.config | |
|
131 | ||||
|
132 | #Set properties that must be set in the config class in order to | |||
|
133 | #propagate to other classes. | |||
|
134 | default_config.GlobalConfigurable.display_data_priority =['svg', 'png', 'latex', 'jpg', 'jpeg','text'] | |||
|
135 | default_config.ExtractFigureTransformer.display_data_priority=['svg', 'png', 'latex', 'jpg', 'jpeg','text'] | |||
|
136 | ||||
|
137 | #Set default properties of the exporter. | |||
|
138 | #For most (or all cases), the template file name matches the format name. | |||
|
139 | self.display_data_priority= ['svg', 'png', 'latex', 'jpg', 'jpeg','text'] | |||
|
140 | self.template_file = export_format | |||
|
141 | ||||
135 | if not config == None: |
|
142 | if not config == None: | |
136 | default_config._merge(config) |
|
143 | default_config._merge(config) | |
137 | config = default_config |
|
144 | config = default_config | |
138 |
|
145 | |||
139 | #Call the base class constructor |
|
146 | #Call the base class constructor | |
140 | super(Exporter, self).__init__(config=config, **kw) |
|
147 | super(Exporter, self).__init__(config=config, **kw) | |
141 |
|
148 | |||
142 | #Standard environment |
|
149 | #Standard environment | |
143 | self.ext = TEMPLATE_EXTENSION |
|
150 | self.ext = TEMPLATE_EXTENSION | |
144 |
self. |
|
151 | self._init_environment() | |
145 | loader=FileSystemLoader([ |
|
|||
146 | os.path.dirname(os.path.realpath(__file__)) + TEMPLATE_PATH, |
|
|||
147 | os.path.dirname(os.path.realpath(__file__)) + TEMPLATE_SKELETON_PATH, |
|
|||
148 | ]), |
|
|||
149 | extensions=JINJA_EXTENSIONS |
|
|||
150 | ) |
|
|||
151 |
|
152 | |||
152 | for name in self.pre_transformer_order: |
|
153 | #TODO: Implement reflection style methods to get user transformers. | |
153 | # get the user-defined transformer first |
|
154 | #for name in self.pre_transformer_order: | |
154 | transformer = preprocessors.get(name, getattr(trans, name, None)) |
|
155 | # # get the user-defined transformer first | |
155 | if isinstance(transformer, MetaHasTraits): |
|
156 | # transformer = preprocessors.get(name, getattr(trans, name, None)) | |
156 | transformer = transformer(config=config) |
|
157 | # if isinstance(transformer, MetaHasTraits): | |
157 | self.preprocessors.append(transformer) |
|
158 | # transformer = transformer(config=config) | |
|
159 | # self.preprocessors.append(transformer) | |||
158 |
|
160 | |||
159 | #For compatibility, TODO: remove later. |
|
161 | #For compatibility, TODO: remove later. | |
160 | self.preprocessors.append(trans.coalesce_streams) |
|
162 | self.preprocessors.append(transformers.coalescestreams.coalesce_streams) | |
161 | self.preprocessors.append(trans.ExtractFigureTransformer(config=config)) |
|
163 | self.preprocessors.append(transformers.extractfigure.ExtractFigureTransformer(config=config)) | |
162 | self.preprocessors.append(trans.RevealHelpTransformer(config=config)) |
|
164 | self.preprocessors.append(transformers.revealhelp.RevealHelpTransformer(config=config)) | |
163 | self.preprocessors.append(trans.CSSHtmlHeaderTransformer(config=config)) |
|
165 | self.preprocessors.append(transformers.csshtmlheader.CSSHtmlHeaderTransformer(config=config)) | |
164 | self.preprocessors.append(LatexTransformer(config=config)) |
|
|||
165 |
|
||||
166 | #Only load the sphinx transformer if the file reference worked |
|
|||
167 | #(Sphinx dependencies exist on the user's machine.) |
|
|||
168 | if SphinxTransformer: |
|
|||
169 | self.preprocessors.append(SphinxTransformer(config=config)) |
|
|||
170 |
|
166 | |||
171 | #Add filters to the Jinja2 environment |
|
167 | #Add filters to the Jinja2 environment | |
172 | self.env.filters['filter_data_type'] = DataTypeFilter(config=config) |
|
168 | self._register_filters(config) | |
173 | self.env.filters['pycomment'] = _python_comment |
|
|||
174 | self.env.filters['indent'] = indent |
|
|||
175 | self.env.filters['rm_fake'] = _rm_fake |
|
|||
176 | self.env.filters['rm_ansi'] = remove_ansi |
|
|||
177 | self.env.filters['markdown'] = markdown |
|
|||
178 | self.env.filters['ansi2html'] = ansi2html |
|
|||
179 | self.env.filters['markdown2latex'] = markdown_utils.markdown2latex |
|
|||
180 | self.env.filters['markdown2rst'] = markdown_utils.markdown2rst |
|
|||
181 | self.env.filters['get_lines'] = get_lines |
|
|||
182 | self.env.filters['wrap'] = strings.wrap |
|
|||
183 | self.env.filters['rm_dollars'] = strings.strip_dollars |
|
|||
184 | self.env.filters['rm_math_space'] = rm_math_space |
|
|||
185 | self.env.filters['highlight2html'] = highlight |
|
|||
186 | self.env.filters['highlight2latex'] = highlight2latex |
|
|||
187 |
|
||||
188 | #Latex specific filters |
|
|||
189 | if self.tex_environement: |
|
|||
190 | self.env.filters['escape_tex'] = _escape_tex |
|
|||
191 | self.env.filters['highlight'] = highlight2latex |
|
|||
192 | else: |
|
|||
193 | self.env.filters['highlight'] = highlight |
|
|||
194 |
|
169 | |||
195 | #Load user filters. Overwrite existing filters if need be. |
|
170 | #Load user filters. Overwrite existing filters if need be. | |
196 | for key, user_filter in jinja_filters.iteritems(): |
|
171 | for key, user_filter in jinja_filters.iteritems(): | |
@@ -243,6 +218,36 b' class Exporter(Configurable):' | |||||
243 | return self.export(nbformat.read(file_stream, 'json')) |
|
218 | return self.export(nbformat.read(file_stream, 'json')) | |
244 |
|
219 | |||
245 |
|
220 | |||
|
221 | def _init_environment(self): | |||
|
222 | self.env = Environment( | |||
|
223 | loader=FileSystemLoader([ | |||
|
224 | os.path.dirname(os.path.realpath(__file__)) + TEMPLATE_PATH, | |||
|
225 | os.path.dirname(os.path.realpath(__file__)) + TEMPLATE_SKELETON_PATH, | |||
|
226 | ]), | |||
|
227 | extensions=JINJA_EXTENSIONS | |||
|
228 | ) | |||
|
229 | ||||
|
230 | ||||
|
231 | def _register_filters(self, config): | |||
|
232 | self.env.filters['indent'] = indent | |||
|
233 | self.env.filters['markdown'] = markdown | |||
|
234 | ||||
|
235 | self.env.filters['ansi2html'] = filters.ansi.ansi2html | |||
|
236 | self.env.filters['filter_data_type'] = filters.datatypefilter.DataTypeFilter(config=config) | |||
|
237 | self.env.filters['get_lines'] = filters.strings.get_lines | |||
|
238 | self.env.filters['highlight'] = filters.pygments.highlight | |||
|
239 | self.env.filters['highlight2html'] = filters.pygments.highlight | |||
|
240 | self.env.filters['highlight2latex'] = filters.pygments.highlight2latex | |||
|
241 | self.env.filters['markdown2latex'] = filters.markdown.markdown2latex | |||
|
242 | self.env.filters['markdown2rst'] = filters.markdown.markdown2rst | |||
|
243 | self.env.filters['pycomment'] = filters.strings.python_comment | |||
|
244 | self.env.filters['rm_ansi'] = filters.ansi.remove_ansi | |||
|
245 | self.env.filters['rm_dollars'] = filters.strings.strip_dollars | |||
|
246 | self.env.filters['rm_fake'] = filters.strings.rm_fake | |||
|
247 | self.env.filters['rm_math_space'] = filters.latex.rm_math_space | |||
|
248 | self.env.filters['wrap'] = filters.strings.wrap | |||
|
249 | ||||
|
250 | ||||
246 | def _preprocess(self, nb): |
|
251 | def _preprocess(self, nb): | |
247 | """ Preprocess the notebook using the transformers specific |
|
252 | """ Preprocess the notebook using the transformers specific | |
248 | for the current export format. |
|
253 | for the current export format. | |
@@ -259,75 +264,3 b' class Exporter(Configurable):' | |||||
259 | nb, resources = transformer(nb, resources) |
|
264 | nb, resources = transformer(nb, resources) | |
260 | return nb, resources |
|
265 | return nb, resources | |
261 |
|
266 | |||
262 |
|
||||
263 | def _get_default_options(self, export_format): |
|
|||
264 | """ Load the default options for built in formats. |
|
|||
265 |
|
||||
266 | export_format: Format being exported to. |
|
|||
267 | """ |
|
|||
268 |
|
||||
269 | c = get_config() |
|
|||
270 |
|
||||
271 | #Set default data extraction priorities. |
|
|||
272 | c.GlobalConfigurable.display_data_priority =['svg', 'png', 'latex', 'jpg', 'jpeg','text'] |
|
|||
273 | c.ExtractFigureTransformer.display_data_priority=['svg', 'png', 'latex', 'jpg', 'jpeg','text'] |
|
|||
274 | c.ConverterTemplate.display_data_priority= ['svg', 'png', 'latex', 'jpg', 'jpeg','text'] |
|
|||
275 |
|
||||
276 | #For most (or all cases), the template file name matches the format name. |
|
|||
277 | c.ConverterTemplate.template_file = export_format |
|
|||
278 |
|
||||
279 | if export_format == "basichtml" or "fullhtml" or "reveal": |
|
|||
280 | c.CSSHtmlHeaderTransformer.enabled=True |
|
|||
281 | if export_format == 'reveal' |
|
|||
282 | c.NbconvertApp.fileext='reveal.html' |
|
|||
283 | else: |
|
|||
284 | c.NbconvertApp.fileext='html' |
|
|||
285 |
|
||||
286 | elif export_format == "latex_sphinx_howto" or export_format == "latex_sphinx_manual": |
|
|||
287 |
|
||||
288 | #Turn on latex environment |
|
|||
289 | c.ConverterTemplate.tex_environement=True |
|
|||
290 |
|
||||
291 | #Standard latex extension |
|
|||
292 | c.NbconvertApp.fileext='tex' |
|
|||
293 |
|
||||
294 | #Prioritize latex extraction for latex exports. |
|
|||
295 | c.GlobalConfigurable.display_data_priority =['latex', 'svg', 'png', 'jpg', 'jpeg' , 'text'] |
|
|||
296 | c.ExtractFigureTransformer.display_data_priority=['latex', 'svg', 'png', 'jpg', 'jpeg'] |
|
|||
297 | c.ExtractFigureTransformer.extra_ext_map={'svg':'pdf'} |
|
|||
298 | c.ExtractFigureTransformer.enabled=True |
|
|||
299 |
|
||||
300 | # Enable latex transformers (make markdown2latex work with math $.) |
|
|||
301 | c.LatexTransformer.enabled=True |
|
|||
302 | c.SphinxTransformer.enabled = True |
|
|||
303 |
|
||||
304 | elif export_format == 'markdown': |
|
|||
305 | c.NbconvertApp.fileext='md' |
|
|||
306 | c.ExtractFigureTransformer.enabled=True |
|
|||
307 |
|
||||
308 | elif export_format == 'python': |
|
|||
309 | c.NbconvertApp.fileext='py' |
|
|||
310 |
|
||||
311 |
|
||||
312 | elif export_format == 'rst': |
|
|||
313 | c.NbconvertApp.fileext='rst' |
|
|||
314 | c.ExtractFigureTransformer.enabled=True |
|
|||
315 | return c |
|
|||
316 |
|
||||
317 |
|
||||
318 | #TODO: Comment me. |
|
|||
319 | def _rm_fake(strng): |
|
|||
320 | return strng.replace('/files/', '') |
|
|||
321 |
|
||||
322 |
|
||||
323 | #TODO: Comment me. |
|
|||
324 | def _python_comment(string): |
|
|||
325 | return '# '+'\n# '.join(string.split('\n')) |
|
|||
326 |
|
||||
327 |
|
||||
328 | #TODO: Comment me. |
|
|||
329 | def _escape_tex(value): |
|
|||
330 | newval = value |
|
|||
331 | for pattern, replacement in LATEX_SUBS: |
|
|||
332 | newval = pattern.sub(replacement, newval) |
|
|||
333 | return newval No newline at end of file |
|
@@ -1,5 +1,4 b'' | |||||
1 |
|
1 | """Latex exporter for the notebook conversion pipeline. | ||
2 | """Latex exporter for the notebook conversion pipeline. |
|
|||
3 |
|
2 | |||
4 | This module defines Exporter, a highly configurable converter |
|
3 | This module defines Exporter, a highly configurable converter | |
5 | that uses Jinja2 to export notebook files into different format. |
|
4 | that uses Jinja2 to export notebook files into different format. | |
@@ -19,9 +18,7 b' befor conversion and jinja filter that would then be availlable in the templates' | |||||
19 | #----------------------------------------------------------------------------- |
|
18 | #----------------------------------------------------------------------------- | |
20 | # Imports |
|
19 | # Imports | |
21 | #----------------------------------------------------------------------------- |
|
20 | #----------------------------------------------------------------------------- | |
22 | from .utils import highlight2latex #TODO |
|
21 | import base.Exporter as Exporter | |
23 |
|
||||
24 | from .transformers.latex import LatexTransformer, rm_math_space #TODO: rm_math_space from filters |
|
|||
25 |
|
22 | |||
26 | #Try to import the Sphinx exporter. If the user doesn't have Sphinx isntalled |
|
23 | #Try to import the Sphinx exporter. If the user doesn't have Sphinx isntalled | |
27 | #on his/her machine, fail silently. |
|
24 | #on his/her machine, fail silently. | |
@@ -44,20 +41,10 b' LATEX_JINJA_COMMENT_BLOCK = ["((=", "=))"]' | |||||
44 | LATEX_JINJA_VARIABLE_BLOCK = ["(((", ")))"] |
|
41 | LATEX_JINJA_VARIABLE_BLOCK = ["(((", ")))"] | |
45 | LATEX_JINJA_LOGIC_BLOCK = ["((*", "*))"] |
|
42 | LATEX_JINJA_LOGIC_BLOCK = ["((*", "*))"] | |
46 |
|
43 | |||
47 | #Latex substitutions for escaping latex. |
|
|||
48 | LATEX_SUBS = ( |
|
|||
49 | (re.compile(r'\\'), r'\\textbackslash'), |
|
|||
50 | (re.compile(r'([{}_#%&$])'), r'\\\1'), |
|
|||
51 | (re.compile(r'~'), r'\~{}'), |
|
|||
52 | (re.compile(r'\^'), r'\^{}'), |
|
|||
53 | (re.compile(r'"'), r"''"), |
|
|||
54 | (re.compile(r'\.\.\.+'), r'\\ldots'), |
|
|||
55 | ) |
|
|||
56 |
|
||||
57 | #----------------------------------------------------------------------------- |
|
44 | #----------------------------------------------------------------------------- | |
58 | # Classes and functions |
|
45 | # Classes and functions | |
59 | #----------------------------------------------------------------------------- |
|
46 | #----------------------------------------------------------------------------- | |
60 |
class LatexExporter( |
|
47 | class LatexExporter(Exporter): | |
61 | """ A Jinja2 base converter templates |
|
48 | """ A Jinja2 base converter templates | |
62 |
|
49 | |||
63 | Preprocess the ipynb files, feed it throug jinja templates, |
|
50 | Preprocess the ipynb files, feed it throug jinja templates, | |
@@ -149,29 +136,9 b' class LatexExporter(Configurable):' | |||||
149 | self.preprocessors.append(SphinxTransformer(config=config)) |
|
136 | self.preprocessors.append(SphinxTransformer(config=config)) | |
150 |
|
137 | |||
151 | #Add filters to the Jinja2 environment |
|
138 | #Add filters to the Jinja2 environment | |
152 | self.env.filters['filter_data_type'] = DataTypeFilter(config=config) |
|
139 | self.env.filters['escape_tex'] = filters.latex.escape_tex | |
153 | self.env.filters['pycomment'] = _python_comment |
|
140 | self.env.filters['highlight'] = filters.pygments.highlight2latex | |
154 | self.env.filters['indent'] = indent |
|
141 | ||
155 | self.env.filters['rm_fake'] = _rm_fake |
|
|||
156 | self.env.filters['rm_ansi'] = remove_ansi |
|
|||
157 | self.env.filters['markdown'] = markdown |
|
|||
158 | self.env.filters['ansi2html'] = ansi2html |
|
|||
159 | self.env.filters['markdown2latex'] = markdown_utils.markdown2latex |
|
|||
160 | self.env.filters['markdown2rst'] = markdown_utils.markdown2rst |
|
|||
161 | self.env.filters['get_lines'] = get_lines |
|
|||
162 | self.env.filters['wrap'] = strings.wrap |
|
|||
163 | self.env.filters['rm_dollars'] = strings.strip_dollars |
|
|||
164 | self.env.filters['rm_math_space'] = rm_math_space |
|
|||
165 | self.env.filters['highlight2html'] = highlight |
|
|||
166 | self.env.filters['highlight2latex'] = highlight2latex |
|
|||
167 |
|
||||
168 | #Latex specific filters |
|
|||
169 | if self.tex_environement: |
|
|||
170 | self.env.filters['escape_tex'] = _escape_tex |
|
|||
171 | self.env.filters['highlight'] = highlight2latex |
|
|||
172 | else: |
|
|||
173 | self.env.filters['highlight'] = highlight |
|
|||
174 |
|
||||
175 | #Load user filters. Overwrite existing filters if need be. |
|
142 | #Load user filters. Overwrite existing filters if need be. | |
176 | for key, user_filter in jinja_filters.iteritems(): |
|
143 | for key, user_filter in jinja_filters.iteritems(): | |
177 | if isinstance(user_filter, MetaHasTraits): |
|
144 | if isinstance(user_filter, MetaHasTraits): | |
@@ -181,115 +148,3 b' class LatexExporter(Configurable):' | |||||
181 |
|
148 | |||
182 | #Load the template file. |
|
149 | #Load the template file. | |
183 | self.template = self.env.get_template(self.template_file+self.ext) |
|
150 | self.template = self.env.get_template(self.template_file+self.ext) | |
184 |
|
||||
185 |
|
||||
186 | def export(self, nb): |
|
|||
187 | """Export notebook object |
|
|||
188 |
|
||||
189 | nb: Notebook object to export. |
|
|||
190 |
|
||||
191 | Returns both the converted ipynb file and a dict containing the |
|
|||
192 | resources created along the way via the transformers and Jinja2 |
|
|||
193 | processing. |
|
|||
194 | """ |
|
|||
195 |
|
||||
196 | nb, resources = self._preprocess(nb) |
|
|||
197 | return self.template.render(nb=nb, resources=resources), resources |
|
|||
198 |
|
||||
199 |
|
||||
200 | def from_filename(self, filename): |
|
|||
201 | """Read and export a notebook from a filename |
|
|||
202 |
|
||||
203 | filename: Filename of the notebook file to export. |
|
|||
204 |
|
||||
205 | Returns both the converted ipynb file and a dict containing the |
|
|||
206 | resources created along the way via the transformers and Jinja2 |
|
|||
207 | processing. |
|
|||
208 | """ |
|
|||
209 | with io.open(filename) as f: |
|
|||
210 | return self.export(nbformat.read(f, 'json')) |
|
|||
211 |
|
||||
212 |
|
||||
213 | def from_file(self, file_stream): |
|
|||
214 | """Read and export a notebook from a filename |
|
|||
215 |
|
||||
216 | file_stream: File handle of file that contains notebook data. |
|
|||
217 |
|
||||
218 | Returns both the converted ipynb file and a dict containing the |
|
|||
219 | resources created along the way via the transformers and Jinja2 |
|
|||
220 | processing. |
|
|||
221 | """ |
|
|||
222 |
|
||||
223 | return self.export(nbformat.read(file_stream, 'json')) |
|
|||
224 |
|
||||
225 |
|
||||
226 | def _preprocess(self, nb): |
|
|||
227 | """ Preprocess the notebook using the transformers specific |
|
|||
228 | for the current export format. |
|
|||
229 |
|
||||
230 | nb: Notebook to preprocess |
|
|||
231 | """ |
|
|||
232 |
|
||||
233 | #Dict of 'resources' that can be filled by the preprocessors. |
|
|||
234 | resources = {} |
|
|||
235 |
|
||||
236 | #Run each transformer on the notebook. Carry the output along |
|
|||
237 | #to each transformer |
|
|||
238 | for transformer in self.preprocessors: |
|
|||
239 | nb, resources = transformer(nb, resources) |
|
|||
240 | return nb, resources |
|
|||
241 |
|
||||
242 |
|
||||
243 | def _get_default_options(self, export_format): |
|
|||
244 | """ Load the default options for built in formats. |
|
|||
245 |
|
||||
246 | export_format: Format being exported to. |
|
|||
247 | """ |
|
|||
248 |
|
||||
249 | c = get_config() |
|
|||
250 |
|
||||
251 | #Set default data extraction priorities. |
|
|||
252 | c.GlobalConfigurable.display_data_priority =['svg', 'png', 'latex', 'jpg', 'jpeg','text'] |
|
|||
253 | c.ExtractFigureTransformer.display_data_priority=['svg', 'png', 'latex', 'jpg', 'jpeg','text'] |
|
|||
254 | c.ConverterTemplate.display_data_priority= ['svg', 'png', 'latex', 'jpg', 'jpeg','text'] |
|
|||
255 |
|
||||
256 | #For most (or all cases), the template file name matches the format name. |
|
|||
257 | c.ConverterTemplate.template_file = export_format |
|
|||
258 |
|
||||
259 | if export_format == "basichtml" or "fullhtml" or "reveal": |
|
|||
260 | c.CSSHtmlHeaderTransformer.enabled=True |
|
|||
261 | if export_format == 'reveal' |
|
|||
262 | c.NbconvertApp.fileext='reveal.html' |
|
|||
263 | else: |
|
|||
264 | c.NbconvertApp.fileext='html' |
|
|||
265 |
|
||||
266 | elif export_format == "latex_sphinx_howto" or export_format == "latex_sphinx_manual": |
|
|||
267 |
|
||||
268 | #Turn on latex environment |
|
|||
269 | c.ConverterTemplate.tex_environement=True |
|
|||
270 |
|
||||
271 | #Standard latex extension |
|
|||
272 | c.NbconvertApp.fileext='tex' |
|
|||
273 |
|
||||
274 | #Prioritize latex extraction for latex exports. |
|
|||
275 | c.GlobalConfigurable.display_data_priority =['latex', 'svg', 'png', 'jpg', 'jpeg' , 'text'] |
|
|||
276 | c.ExtractFigureTransformer.display_data_priority=['latex', 'svg', 'png', 'jpg', 'jpeg'] |
|
|||
277 | c.ExtractFigureTransformer.extra_ext_map={'svg':'pdf'} |
|
|||
278 | c.ExtractFigureTransformer.enabled=True |
|
|||
279 |
|
||||
280 | # Enable latex transformers (make markdown2latex work with math $.) |
|
|||
281 | c.LatexTransformer.enabled=True |
|
|||
282 | c.SphinxTransformer.enabled = True |
|
|||
283 |
|
||||
284 | elif export_format == 'markdown': |
|
|||
285 | c.NbconvertApp.fileext='md' |
|
|||
286 | c.ExtractFigureTransformer.enabled=True |
|
|||
287 |
|
||||
288 | elif export_format == 'python': |
|
|||
289 | c.NbconvertApp.fileext='py' |
|
|||
290 |
|
||||
291 |
|
||||
292 | elif export_format == 'rst': |
|
|||
293 | c.NbconvertApp.fileext='rst' |
|
|||
294 | c.ExtractFigureTransformer.enabled=True |
|
|||
295 | return c No newline at end of file |
|
@@ -20,7 +20,7 b' class DataTypeFilter(GlobalConfigurable):' | |||||
20 | """ Returns the prefered display format """ |
|
20 | """ Returns the prefered display format """ | |
21 |
|
21 | |||
22 | def __init__(self, config=None, **kw): |
|
22 | def __init__(self, config=None, **kw): | |
23 |
super( |
|
23 | super(DataTypeFilter, self).__init__(config=config, **kw) | |
24 |
|
24 | |||
25 | def __call__(self, output): |
|
25 | def __call__(self, output): | |
26 | """ Return the first available format in the priority """ |
|
26 | """ Return the first available format in the priority """ |
@@ -14,11 +14,34 b' they are converted.' | |||||
14 | #----------------------------------------------------------------------------- |
|
14 | #----------------------------------------------------------------------------- | |
15 | # Imports |
|
15 | # Imports | |
16 | #----------------------------------------------------------------------------- |
|
16 | #----------------------------------------------------------------------------- | |
17 | from __future__ import print_function |
|
17 | import re | |
|
18 | ||||
|
19 | #----------------------------------------------------------------------------- | |||
|
20 | # Globals and constants | |||
|
21 | #----------------------------------------------------------------------------- | |||
|
22 | ||||
|
23 | #Latex substitutions for escaping latex. | |||
|
24 | LATEX_SUBS = ( | |||
|
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 | ) | |||
18 |
|
32 | |||
19 | #----------------------------------------------------------------------------- |
|
33 | #----------------------------------------------------------------------------- | |
20 | # Functions |
|
34 | # Functions | |
21 | #----------------------------------------------------------------------------- |
|
35 | #----------------------------------------------------------------------------- | |
|
36 | ||||
|
37 | #TODO: Comment me. | |||
|
38 | def escape_tex(value): | |||
|
39 | newval = value | |||
|
40 | for pattern, replacement in LATEX_SUBS: | |||
|
41 | newval = pattern.sub(replacement, newval) | |||
|
42 | return newval | |||
|
43 | ||||
|
44 | ||||
22 | def rm_math_space(text): |
|
45 | def rm_math_space(text): | |
23 | """ |
|
46 | """ | |
24 | Remove the space between latex math commands and enclosing $ symbols. |
|
47 | Remove the space between latex math commands and enclosing $ symbols. |
@@ -17,11 +17,13 b' markdown.' | |||||
17 | from __future__ import print_function |
|
17 | from __future__ import print_function | |
18 |
|
18 | |||
19 | # Stdlib imports |
|
19 | # Stdlib imports | |
|
20 | import sys | |||
20 | import subprocess |
|
21 | import subprocess | |
21 |
|
22 | |||
22 | #----------------------------------------------------------------------------- |
|
23 | #----------------------------------------------------------------------------- | |
23 | # Functions |
|
24 | # Functions | |
24 | #----------------------------------------------------------------------------- |
|
25 | #----------------------------------------------------------------------------- | |
|
26 | ||||
25 | # Pandoc-dependent code |
|
27 | # Pandoc-dependent code | |
26 | def markdown2latex(src): |
|
28 | def markdown2latex(src): | |
27 | """Convert a markdown string to LaTeX via pandoc. |
|
29 | """Convert a markdown string to LaTeX via pandoc. |
@@ -1,4 +1,4 b'' | |||||
1 |
|
|
1 | """String utilities. | |
2 |
|
2 | |||
3 | Contains a collection of usefull string manipulations functions. |
|
3 | Contains a collection of usefull string manipulations functions. | |
4 | """ |
|
4 | """ | |
@@ -32,4 +32,26 b' def wrap(text, width=100):' | |||||
32 | def strip_dollars(text): |
|
32 | def strip_dollars(text): | |
33 | """Remove all dollar symbols from text""" |
|
33 | """Remove all dollar symbols from text""" | |
34 |
|
34 | |||
35 | return text.strip('$') No newline at end of file |
|
35 | return text.strip('$') | |
|
36 | ||||
|
37 | ||||
|
38 | #TODO: Comment me. | |||
|
39 | def rm_fake(strng): | |||
|
40 | return strng.replace('/files/', '') | |||
|
41 | ||||
|
42 | ||||
|
43 | #TODO: Comment me. | |||
|
44 | def python_comment(string): | |||
|
45 | return '# '+'\n# '.join(string.split('\n')) | |||
|
46 | ||||
|
47 | def get_lines(src, start=None,end=None): | |||
|
48 | """ | |||
|
49 | Split the input text into separate lines and then return the | |||
|
50 | lines that the caller is interested in. | |||
|
51 | """ | |||
|
52 | ||||
|
53 | # Split the input into lines. | |||
|
54 | lines = src.split("\n") | |||
|
55 | ||||
|
56 | # Return the right lines. | |||
|
57 | return "\n".join(lines[start:end]) #re-join |
@@ -1,4 +1,6 b'' | |||||
1 |
|
1 | from transformers.base import ConfigurableTransformers | ||
|
2 | ||||
|
3 | ||||
2 | class ActivatableTransformer(ConfigurableTransformers): |
|
4 | class ActivatableTransformer(ConfigurableTransformers): | |
3 | """A simple ConfigurableTransformers that have an enabled flag |
|
5 | """A simple ConfigurableTransformers that have an enabled flag | |
4 |
|
6 | |||
@@ -15,4 +17,3 b' class ActivatableTransformer(ConfigurableTransformers):' | |||||
15 | return nb, other |
|
17 | return nb, other | |
16 | else : |
|
18 | else : | |
17 | return super(ActivatableTransformer, self).__call__(nb, other) |
|
19 | return super(ActivatableTransformer, self).__call__(nb, other) | |
18 |
|
@@ -57,6 +57,7 b' class ConfigurableTransformers(GlobalConfigurable):' | |||||
57 |
|
57 | |||
58 | You should return modified cell and resource dict. |
|
58 | You should return modified cell and resource dict. | |
59 | """ |
|
59 | """ | |
|
60 | ||||
60 | raise NotImplementedError('should be implemented by subclass') |
|
61 | raise NotImplementedError('should be implemented by subclass') | |
61 | return cell, other |
|
62 | return cell, other | |
62 |
|
63 |
1 | NO CONTENT: modified file chmod 100644 => 100755 |
|
NO CONTENT: modified file chmod 100644 => 100755 |
1 | NO CONTENT: modified file chmod 100644 => 100755 |
|
NO CONTENT: modified file chmod 100644 => 100755 |
1 | NO CONTENT: modified file chmod 100644 => 100755 |
|
NO CONTENT: modified file chmod 100644 => 100755 |
1 | NO CONTENT: modified file chmod 100644 => 100755, file renamed from nbconvert/transformers/reavealhelp.py to nbconvert/transformers/revealhelp.py |
|
NO CONTENT: modified file chmod 100644 => 100755, file renamed from nbconvert/transformers/reavealhelp.py to nbconvert/transformers/revealhelp.py |
1 | NO CONTENT: modified file chmod 100644 => 100755 |
|
NO CONTENT: modified file chmod 100644 => 100755 |
1 | NO CONTENT: file renamed from nbconvert1/converters/lexers.py to nbconvert/utils/lexers.py |
|
NO CONTENT: file renamed from nbconvert1/converters/lexers.py to nbconvert/utils/lexers.py |
General Comments 0
You need to be logged in to leave comments.
Login now