##// 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 from __future__ import print_function, absolute_import
18 from __future__ import print_function, absolute_import
19 from .transformers import extract_figure_transformer
19 from .transformers import extract_figure_transformer
20 import converters.transformers as trans
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 # Stdlib imports
26 # Stdlib imports
24 import io
27 import io
25 import os
28 import os
26 import re
27 from IPython.utils import path
29 from IPython.utils import path
28
30
29 from jinja2 import Environment, FileSystemLoader
31 from jinja2 import Environment, FileSystemLoader
@@ -48,27 +50,13 b' from IPython.nbformat import current as nbformat'
48 from IPython.config.configurable import Configurable
50 from IPython.config.configurable import Configurable
49 from IPython.utils.traitlets import ( Unicode, Any, List, Bool)
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 # Class declarations
54 # Class declarations
59 #-----------------------------------------------------------------------------
55 #-----------------------------------------------------------------------------
60 def rm_fake(strng):
61 return strng.replace('/files/', '')
62
63 class ConversionException(Exception):
56 class ConversionException(Exception):
64 pass
57 pass
65
58
66
59
67 def python_comment(string):
68 return '# '+'\n# '.join(string.split('\n'))
69
70
71
72 def header_body():
60 def header_body():
73 """Return the body of the header as a list of strings."""
61 """Return the body of the header as a list of strings."""
74
62
@@ -101,19 +89,6 b' def header_body():'
101 header.append(pygments_css)
89 header.append(pygments_css)
102 return header
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 inlining = {}
96 inlining = {}
122 inlining['css'] = header_body()
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
99
133 def escape_tex(value):
134 newval = value
135 for pattern, replacement in LATEX_SUBS:
136 newval = pattern.sub(replacement, newval)
137 return newval
138
100
139 texenv.block_start_string = '((*'
101 texenv.block_start_string = '((*'
140 texenv.block_end_string = '*))'
102 texenv.block_end_string = '*))'
@@ -144,23 +106,6 b" texenv.comment_start_string = '((='"
144 texenv.comment_end_string = '=))'
106 texenv.comment_end_string = '=))'
145 texenv.filters['escape_tex'] = escape_tex
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 class ConverterTemplate(Configurable):
110 class ConverterTemplate(Configurable):
166 """ A Jinja2 base converter templates"""
111 """ A Jinja2 base converter templates"""
@@ -224,7 +169,7 b' class ConverterTemplate(Configurable):'
224 self.nb = None
169 self.nb = None
225 self.preprocessors = preprocessors
170 self.preprocessors = preprocessors
226 for name in self.pre_transformer_order:
171 for name in self.pre_transformer_order:
227 self.preprocessors.append(getattr(trans,'haspyout_transformer'))
172 self.preprocessors.append(getattr(trans, name))
228 if self.extract_figures:
173 if self.extract_figures:
229 self.preprocessors.append(extract_figure_transformer)
174 self.preprocessors.append(extract_figure_transformer)
230
175
@@ -36,6 +36,20 b' def haspyout_transformer(cell, other, count):'
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 @cell_preprocessor
53 @cell_preprocessor
40 def extract_figure_transformer(cell,other,count):
54 def extract_figure_transformer(cell, other, count):
41 for i,out in enumerate(cell.get('outputs', [])):
55 for i, out in enumerate(cell.get('outputs', [])):
General Comments 0
You need to be logged in to leave comments. Login now