##// END OF EJS Templates
mofe filter in separate file
Matthias BUSSONNIER -
Show More
@@ -0,0 +1,42 b''
1 #-----------------------------------------------------------------------------
2 # Copyright (c) 2012, the IPython Development Team.
3 #
4 # Distributed under the terms of the Modified BSD License.
5 #
6 # The full license is in the file COPYING.txt, distributed with this software.
7 #-----------------------------------------------------------------------------
8
9 from __future__ import absolute_import
10
11 # Stdlib imports
12 import re
13
14 from IPython.utils.text import indent
15 from markdown import markdown
16 from .utils import remove_ansi
17 from .utils import highlight, ansi2html
18 from .utils import markdown2latex
19 #-----------------------------------------------------------------------------
20 # Class declarations
21 #-----------------------------------------------------------------------------
22 def rm_fake(strng):
23 return strng.replace('/files/', '')
24
25 def python_comment(string):
26 return '# '+'\n# '.join(string.split('\n'))
27
28 LATEX_SUBS = (
29 (re.compile(r'\\'), r'\\textbackslash'),
30 (re.compile(r'([{}_#%&$])'), r'\\\1'),
31 (re.compile(r'~'), r'\~{}'),
32 (re.compile(r'\^'), r'\^{}'),
33 (re.compile(r'"'), r"''"),
34 (re.compile(r'\.\.\.+'), r'\\ldots'),
35 )
36
37 def escape_tex(value):
38 newval = value
39 for pattern, replacement in LATEX_SUBS:
40 newval = pattern.sub(replacement, newval)
41 return newval
42
@@ -18,12 +18,14 b' a conversion of IPython notebooks to some other format should inherit.'
18 18 from __future__ import print_function, absolute_import
19 19 from .transformers import extract_figure_transformer
20 20 import converters.transformers as trans
21 from converters.jinja_filters import (python_comment, indent,
22 rm_fake, remove_ansi, markdown, highlight,
23 ansi2html, markdown2latex, escape_tex)
21 24
22 25
23 26 # Stdlib imports
24 27 import io
25 28 import os
26 import re
27 29 from IPython.utils import path
28 30
29 31 from jinja2 import Environment, FileSystemLoader
@@ -48,27 +50,13 b' from IPython.nbformat import current as nbformat'
48 50 from IPython.config.configurable import Configurable
49 51 from IPython.utils.traitlets import ( Unicode, Any, List, Bool)
50 52
51 # Our own imports
52 from IPython.utils.text import indent
53 from .utils import remove_ansi
54 from markdown import markdown
55 from .utils import highlight, ansi2html
56 from .utils import markdown2latex
57 53 #-----------------------------------------------------------------------------
58 54 # Class declarations
59 55 #-----------------------------------------------------------------------------
60 def rm_fake(strng):
61 return strng.replace('/files/', '')
62
63 56 class ConversionException(Exception):
64 57 pass
65 58
66 59
67 def python_comment(string):
68 return '# '+'\n# '.join(string.split('\n'))
69
70
71
72 60 def header_body():
73 61 """Return the body of the header as a list of strings."""
74 62
@@ -101,19 +89,6 b' def header_body():'
101 89 header.append(pygments_css)
102 90 return header
103 91
104 # todo, make the key part configurable.
105 def _new_figure(data, fmt, count):
106 """Create a new figure file in the given format.
107
108 Returns a path relative to the input file.
109 """
110 figname = '_fig_%02i.%s' % (count, fmt)
111
112 # Binary files are base64-encoded, SVG is already XML
113 if fmt in ('png', 'jpg', 'pdf'):
114 data = data.decode('base64')
115
116 return figname,data
117 92
118 93
119 94
@@ -121,20 +96,7 b' def _new_figure(data, fmt, count):'
121 96 inlining = {}
122 97 inlining['css'] = header_body()
123 98
124 LATEX_SUBS = (
125 (re.compile(r'\\'), r'\\textbackslash'),
126 (re.compile(r'([{}_#%&$])'), r'\\\1'),
127 (re.compile(r'~'), r'\~{}'),
128 (re.compile(r'\^'), r'\^{}'),
129 (re.compile(r'"'), r"''"),
130 (re.compile(r'\.\.\.+'), r'\\ldots'),
131 )
132
133 def escape_tex(value):
134 newval = value
135 for pattern, replacement in LATEX_SUBS:
136 newval = pattern.sub(replacement, newval)
137 return newval
99
138 100
139 101 texenv.block_start_string = '((*'
140 102 texenv.block_end_string = '*))'
@@ -144,23 +106,6 b" texenv.comment_start_string = '((='"
144 106 texenv.comment_end_string = '=))'
145 107 texenv.filters['escape_tex'] = escape_tex
146 108
147 def cell_preprocessor(function):
148 """ wrap a function to be executed on all cells of a notebook
149
150 wrapped function parameters :
151 cell : the cell
152 other : external resources
153 index : index of the cell
154 """
155 def wrappedfunc(nb,other):
156 for worksheet in nb.worksheets :
157 for index, cell in enumerate(worksheet.cells):
158 worksheet.cells[index],other= function(cell,other,index)
159 return nb,other
160 return wrappedfunc
161
162
163
164 109
165 110 class ConverterTemplate(Configurable):
166 111 """ A Jinja2 base converter templates"""
@@ -203,7 +148,7 b' class ConverterTemplate(Configurable):'
203 148
204 149 infile_dir = Unicode()
205 150
206 def filter_data_type(self,output):
151 def filter_data_type(self, output):
207 152 for fmt in self.display_data_priority:
208 153 if fmt in output:
209 154 return [fmt]
@@ -224,7 +169,7 b' class ConverterTemplate(Configurable):'
224 169 self.nb = None
225 170 self.preprocessors = preprocessors
226 171 for name in self.pre_transformer_order:
227 self.preprocessors.append(getattr(trans,'haspyout_transformer'))
172 self.preprocessors.append(getattr(trans, name))
228 173 if self.extract_figures:
229 174 self.preprocessors.append(extract_figure_transformer)
230 175
@@ -253,7 +198,7 b' class ConverterTemplate(Configurable):'
253 198 resources = {}
254 199
255 200 for preprocessor in self.preprocessors:
256 nb,resources = preprocessor(nb,resources)
201 nb, resources = preprocessor(nb, resources)
257 202
258 203 return nb, resources
259 204
@@ -263,7 +208,7 b' class ConverterTemplate(Configurable):'
263 208 return both the converted ipynb file and a dict containing potential
264 209 other resources
265 210 """
266 nb,resources = self.process()
211 nb, resources = self.process()
267 212 return self.template.render(nb=nb, inlining=inlining), resources
268 213
269 214
@@ -10,11 +10,11 b' def cell_preprocessor(function):'
10 10 other : external resources
11 11 index : index of the cell
12 12 """
13 def wrappedfunc(nb,other):
13 def wrappedfunc(nb, other):
14 14 for worksheet in nb.worksheets :
15 15 for index, cell in enumerate(worksheet.cells):
16 worksheet.cells[index],other= function(cell,other,index)
17 return nb,other
16 worksheet.cells[index], other = function(cell, other, index)
17 return nb, other
18 18 return wrappedfunc
19 19
20 20
@@ -33,18 +33,32 b' def haspyout_transformer(cell, other, count):'
33 33 if out.output_type == 'pyout':
34 34 cell.haspyout = True
35 35 break
36 return cell,other
36 return cell, other
37 37
38 38
39 # todo, make the key part configurable.
40 def _new_figure(data, fmt, count):
41 """Create a new figure file in the given format.
42
43 Returns a path relative to the input file.
44 """
45 figname = '_fig_%02i.%s' % (count, fmt)
46
47 # Binary files are base64-encoded, SVG is already XML
48 if fmt in ('png', 'jpg', 'pdf'):
49 data = data.decode('base64')
50
51 return figname, data
52
39 53 @cell_preprocessor
40 def extract_figure_transformer(cell,other,count):
41 for i,out in enumerate(cell.get('outputs', [])):
54 def extract_figure_transformer(cell, other, count):
55 for i, out in enumerate(cell.get('outputs', [])):
42 56 for type in ['html', 'pdf', 'svg', 'latex', 'png', 'jpg', 'jpeg']:
43 57 if out.hasattr(type):
44 figname,data = _new_figure(out[type], type, count)
58 figname, data = _new_figure(out[type], type, count)
45 59 cell.outputs[i][type] = figname
46 60 out['key_'+type] = figname
47 61 other[figname] = data
48 62 count = count+1
49 return cell,other
63 return cell, other
50 64
General Comments 0
You need to be logged in to leave comments. Login now