##// END OF EJS Templates
multiple env
Matthias BUSSONNIER -
Show More
@@ -0,0 +1,56 b''
1 ((*- extends 'null.tplx' -*))
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 *))
@@ -1,176 +1,218 b''
1 """Base classes for the notebook conversion pipeline.
1 """Base classes for the notebook conversion pipeline.
2
2
3 This module defines Converter, from which all objects designed to implement
3 This module defines Converter, from which all objects designed to implement
4 a conversion of IPython notebooks to some other format should inherit.
4 a conversion of IPython notebooks to some other format should inherit.
5 """
5 """
6 #-----------------------------------------------------------------------------
6 #-----------------------------------------------------------------------------
7 # Copyright (c) 2012, the IPython Development Team.
7 # Copyright (c) 2012, the IPython Development Team.
8 #
8 #
9 # Distributed under the terms of the Modified BSD License.
9 # Distributed under the terms of the Modified BSD License.
10 #
10 #
11 # The full license is in the file COPYING.txt, distributed with this software.
11 # The full license is in the file COPYING.txt, distributed with this software.
12 #-----------------------------------------------------------------------------
12 #-----------------------------------------------------------------------------
13
13
14 #-----------------------------------------------------------------------------
14 #-----------------------------------------------------------------------------
15 # Imports
15 # Imports
16 #-----------------------------------------------------------------------------
16 #-----------------------------------------------------------------------------
17
17
18 from __future__ import print_function, absolute_import
18 from __future__ import print_function, absolute_import
19
19
20 # Stdlib imports
20 # Stdlib imports
21 import io
21 import io
22 import os
22 import os
23 import re
23 from IPython.utils import path
24 from IPython.utils import path
24
25
25 from jinja2 import Environment, FileSystemLoader
26 from jinja2 import Environment, FileSystemLoader
26 env = Environment(
27 env = Environment(
27 loader=FileSystemLoader('./templates/'),
28 loader=FileSystemLoader('./templates/'),
28 extensions=['jinja2.ext.loopcontrols']
29 extensions=['jinja2.ext.loopcontrols']
29 )
30 )
30
31
32 texenv = Environment(
33 loader=FileSystemLoader('./templates/tex/'),
34 extensions=['jinja2.ext.loopcontrols']
35 )
36
31 # IPython imports
37 # IPython imports
32 from IPython.nbformat import current as nbformat
38 from IPython.nbformat import current as nbformat
33 from IPython.config.configurable import Configurable
39 from IPython.config.configurable import Configurable
34 from IPython.utils.traitlets import ( Unicode, Any)
40 from IPython.utils.traitlets import ( Unicode, Any)
35
41
36 # Our own imports
42 # Our own imports
37 from IPython.utils.text import indent
43 from IPython.utils.text import indent
38 from .utils import remove_ansi
44 from .utils import remove_ansi
39 from markdown import markdown
45 from markdown import markdown
40 from .utils import highlight, ansi2html
46 from .utils import highlight, ansi2html
41 #-----------------------------------------------------------------------------
47 #-----------------------------------------------------------------------------
42 # Class declarations
48 # Class declarations
43 #-----------------------------------------------------------------------------
49 #-----------------------------------------------------------------------------
44 def rm_fake(strng):
50 def rm_fake(strng):
45 return strng.replace('/files/', '')
51 return strng.replace('/files/', '')
46
52
47 class ConversionException(Exception):
53 class ConversionException(Exception):
48 pass
54 pass
49
55
50
56
51 def python_comment(string):
57 def python_comment(string):
52 return '# '+'\n# '.join(string.split('\n'))
58 return '# '+'\n# '.join(string.split('\n'))
53
59
54
60
55
61
56 def header_body():
62 def header_body():
57 """Return the body of the header as a list of strings."""
63 """Return the body of the header as a list of strings."""
58
64
59 from pygments.formatters import HtmlFormatter
65 from pygments.formatters import HtmlFormatter
60
66
61 header = []
67 header = []
62 static = os.path.join(path.get_ipython_package_dir(),
68 static = os.path.join(path.get_ipython_package_dir(),
63 'frontend', 'html', 'notebook', 'static',
69 'frontend', 'html', 'notebook', 'static',
64 )
70 )
65 here = os.path.split(os.path.realpath(__file__))[0]
71 here = os.path.split(os.path.realpath(__file__))[0]
66 css = os.path.join(static, 'css')
72 css = os.path.join(static, 'css')
67 for sheet in [
73 for sheet in [
68 # do we need jquery and prettify?
74 # do we need jquery and prettify?
69 # os.path.join(static, 'jquery', 'css', 'themes', 'base',
75 # os.path.join(static, 'jquery', 'css', 'themes', 'base',
70 # 'jquery-ui.min.css'),
76 # 'jquery-ui.min.css'),
71 # os.path.join(static, 'prettify', 'prettify.css'),
77 # os.path.join(static, 'prettify', 'prettify.css'),
72 os.path.join(css, 'boilerplate.css'),
78 os.path.join(css, 'boilerplate.css'),
73 os.path.join(css, 'fbm.css'),
79 os.path.join(css, 'fbm.css'),
74 os.path.join(css, 'notebook.css'),
80 os.path.join(css, 'notebook.css'),
75 os.path.join(css, 'renderedhtml.css'),
81 os.path.join(css, 'renderedhtml.css'),
76 # our overrides:
82 # our overrides:
77 os.path.join(here, '..', 'css', 'static_html.css'),
83 os.path.join(here, '..', 'css', 'static_html.css'),
78 ]:
84 ]:
79
85
80 with io.open(sheet, encoding='utf-8') as f:
86 with io.open(sheet, encoding='utf-8') as f:
81 s = f.read()
87 s = f.read()
82 header.append(s)
88 header.append(s)
83
89
84 pygments_css = HtmlFormatter().get_style_defs('.highlight')
90 pygments_css = HtmlFormatter().get_style_defs('.highlight')
85 header.append(pygments_css)
91 header.append(pygments_css)
86 return header
92 return header
87
93
88 inlining = {}
94 inlining = {}
89 inlining['css'] = header_body()
95 inlining['css'] = header_body()
90
96
91
97
92 def filter_data_type(output):
98 def filter_data_type(output):
93 for fmt in ['html', 'pdf', 'svg', 'latex', 'png', 'jpg', 'jpeg' , 'text']:
99 for fmt in ['html', 'pdf', 'svg', 'latex', 'png', 'jpg', 'jpeg' , 'text']:
94 if fmt in output:
100 if fmt in output:
95 return [fmt]
101 return [fmt]
96
102
97
103
98 env.filters['filter_data_type'] = filter_data_type
104 env.filters['filter_data_type'] = filter_data_type
99 env.filters['pycomment'] = python_comment
105 env.filters['pycomment'] = python_comment
100 env.filters['indent'] = indent
106 env.filters['indent'] = indent
101 env.filters['rm_fake'] = rm_fake
107 env.filters['rm_fake'] = rm_fake
102 env.filters['rm_ansi'] = remove_ansi
108 env.filters['rm_ansi'] = remove_ansi
103 env.filters['markdown'] = markdown
109 env.filters['markdown'] = markdown
104 env.filters['highlight'] = highlight
110 env.filters['highlight'] = highlight
105 env.filters['ansi2html'] = ansi2html
111 env.filters['ansi2html'] = ansi2html
106
112
107
113
114
115 LATEX_SUBS = (
116 (re.compile(r'\\'), r'\\textbackslash'),
117 (re.compile(r'([{}_#%&$])'), r'\\\1'),
118 (re.compile(r'~'), r'\~{}'),
119 (re.compile(r'\^'), r'\^{}'),
120 (re.compile(r'"'), r"''"),
121 (re.compile(r'\.\.\.+'), r'\\ldots'),
122 )
123
124 def escape_tex(value):
125 newval = value
126 for pattern, replacement in LATEX_SUBS:
127 newval = pattern.sub(replacement, newval)
128 return newval
129
130 texenv.block_start_string = '((*'
131 texenv.block_end_string = '*))'
132 texenv.variable_start_string = '((('
133 texenv.variable_end_string = ')))'
134 texenv.comment_start_string = '((='
135 texenv.comment_end_string = '=))'
136 texenv.filters['escape_tex'] = escape_tex
137
138 texenv.filters['filter_data_type'] = filter_data_type
139 texenv.filters['pycomment'] = python_comment
140 texenv.filters['indent'] = indent
141 texenv.filters['rm_fake'] = rm_fake
142 texenv.filters['rm_ansi'] = remove_ansi
143 texenv.filters['markdown'] = markdown
144 texenv.filters['highlight'] = highlight
145 texenv.filters['ansi2html'] = ansi2html
146
147
108 def haspyout_transformer(nb,_):
148 def haspyout_transformer(nb,_):
109 print('calling...')
149 print('calling...')
110 for worksheet in nb.worksheets:
150 for worksheet in nb.worksheets:
111 print('worksheet')
151 print('worksheet')
112 for cell in worksheet.cells:
152 for cell in worksheet.cells:
113 cell.type = cell.cell_type
153 cell.type = cell.cell_type
114 cell.haspyout = False
154 cell.haspyout = False
115 for out in cell.get('outputs', []):
155 for out in cell.get('outputs', []):
116 if out.output_type == 'pyout':
156 if out.output_type == 'pyout':
117 cell.haspyout = True
157 cell.haspyout = True
118 break
158 break
119 return nb
159 return nb
120
160
121 class ConverterTemplate(Configurable):
161 class ConverterTemplate(Configurable):
122 """ A Jinja2 base converter templates"""
162 """ A Jinja2 base converter templates"""
123
163
124 display_data_priority = ['pdf', 'svg', 'png', 'jpg', 'text']
164 display_data_priority = ['pdf', 'svg', 'png', 'jpg', 'text']
125 #-------------------------------------------------------------------------
165 #-------------------------------------------------------------------------
126 # Instance-level attributes that are set in the constructor for this
166 # Instance-level attributes that are set in the constructor for this
127 # class.
167 # class.
128 #-------------------------------------------------------------------------
168 #-------------------------------------------------------------------------
129 infile = Any()
169 infile = Any()
130
170
131
171
132 infile_dir = Unicode()
172 infile_dir = Unicode()
133
173
134 def __init__(self, tplfile='fullhtml', preprocessors=[], config=None, **kw):
174 def __init__(self, tplfile='fullhtml', preprocessors=[], config=None,tex_environement=False, **kw):
135 """
175 """
136 tplfile : jinja template file to process.
176 tplfile : jinja template file to process.
137
177
138 config: the Configurable confg object to pass around
178 config: the Configurable confg object to pass around
139
179
140 preprocessors: list of function to run on ipynb json data before conversion
180 preprocessors: list of function to run on ipynb json data before conversion
141 to extract/inline file,
181 to extract/inline file,
142
182
143 """
183 """
144 self.template = env.get_template(tplfile+'.tpl')
184 self.env = texenv if tex_environement else env
185 self.ext = '.tplx' if tex_environement else '.tpl'
186 self.template = self.env.get_template(tplfile+self.ext)
145 self.nb = None
187 self.nb = None
146 self.preprocessors = preprocessors
188 self.preprocessors = preprocessors
147 self.preprocessors.append(haspyout_transformer)
189 self.preprocessors.append(haspyout_transformer)
148 super(ConverterTemplate, self).__init__(config=config, **kw)
190 super(ConverterTemplate, self).__init__(config=config, **kw)
149
191
150
192
151 def process(self):
193 def process(self):
152 """
194 """
153 preprocess the notebook json for easier use with the templates.
195 preprocess the notebook json for easier use with the templates.
154 will call all the `preprocessor`s in order before returning it.
196 will call all the `preprocessor`s in order before returning it.
155 """
197 """
156 nb = self.nb
198 nb = self.nb
157
199
158 for preprocessor in self.preprocessors:
200 for preprocessor in self.preprocessors:
159 nb = preprocessor(nb,{})
201 nb = preprocessor(nb,{})
160
202
161 return nb
203 return nb
162
204
163 def convert(self):
205 def convert(self):
164 """ convert the ipynb file
206 """ convert the ipynb file
165
207
166 return both the converted ipynb file and a dict containing potential
208 return both the converted ipynb file and a dict containing potential
167 other resources
209 other resources
168 """
210 """
169 return self.template.render(nb=self.process(), inlining=inlining), {}
211 return self.template.render(nb=self.process(), inlining=inlining), {}
170
212
171
213
172 def read(self, filename):
214 def read(self, filename):
173 "read and parse notebook into NotebookNode called self.nb"
215 "read and parse notebook into NotebookNode called self.nb"
174 with io.open(filename) as f:
216 with io.open(filename) as f:
175 self.nb = nbformat.read(f, 'json')
217 self.nb = nbformat.read(f, 'json')
176
218
@@ -1,13 +1,21 b''
1 #!/usr/bin/env python
1 #!/usr/bin/env python
2 # coding: utf-8
2 # coding: utf-8
3
3
4 from __future__ import print_function
4 from __future__ import print_function
5 import sys
5 import sys
6 import io
6 import io
7 from converters.template import *
7 from converters.template import *
8 C = ConverterTemplate(tplfile=sys.argv[1])
8
9 template_file = sys.argv[1]
10
11 if template_file.startswith('latex'):
12 tex_environement=True
13 else:
14 tex_environement=False
15
16 C = ConverterTemplate(tplfile=sys.argv[1], tex_environement=tex_environement)
9 C.read(sys.argv[2])
17 C.read(sys.argv[2])
10
18
11 output,rest = C.convert()
19 output,rest = C.convert()
12
20
13 print(output.encode('utf-8'))
21 print(output.encode('utf-8'))
General Comments 0
You need to be logged in to leave comments. Login now