##// END OF EJS Templates
start rst converter
Matthias BUSSONNIER -
Show More
@@ -0,0 +1,7 b''
1 c = get_config()
2
3 c.ConverterTemplate.extract_figures=False
4 c.ConverterTemplate.template_file='rst'
5 c.ConverterTemplate.tex_environement=False
6
7 c.NbconvertApp.fileext='rst'
@@ -0,0 +1,69 b''
1 {%- extends 'null.tpl' -%}
2
3 {% block in_prompt -%}
4 In[{{cell.prompt_number if cell.prompt_number else ' '}}]:
5
6 .. code:: python
7
8 {% endblock in_prompt %}
9
10 {% block output_prompt %}
11 Out[{{cell.prompt_number}}]:{% endblock output_prompt %}
12
13 {% block input %}{{ cell.input | indent}}
14 {% endblock input %}
15
16
17 {# Those Two are for error displaying
18 even if the first one seem to do nothing,
19 it introduces a new line
20
21 #}
22 {% block pyerr %}{{ super() }}
23 {% endblock pyerr %}
24
25 {% block traceback_line %}
26 {{ line |indent| rm_ansi }}{% endblock traceback_line %}
27 {# .... #}
28
29
30 {% block pyout %}
31 .. parsed-literal::
32
33 {{ output.text| indent }}
34 {% endblock pyout %}
35
36 {% block stream %}
37 .. parsed-literal::
38
39 {{ output.text| indent }}
40 {% endblock stream %}
41
42
43
44
45 {% block display_data scoped %}
46 # image file:
47 {% endblock display_data %}
48
49 {% block markdowncell scoped %}
50 {{ cell.source | markdown2rst }}
51 {% endblock markdowncell %}
52
53 {% block headingcell scoped %}
54 {%- set len = cell.source|length -%}
55 {{ cell.source}}
56 {% if cell.level == 1 %}
57 {{- '=' * len }}
58 {%- elif cell.level == 2 %}
59 {{- '-' * len }}
60 {% endif %}
61 {% endblock headingcell %}
62
63 {% block rawcell scoped %}
64 {{ cell.source }}
65 {% endblock rawcell %}
66
67 {% block unknowncell scoped %}
68 unknown type {{cell.type}}
69 {% endblock unknowncell %}
@@ -1,237 +1,240 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 import converters.transformers as trans
19 import converters.transformers as trans
20 from converters.jinja_filters import (python_comment, indent,
20 from converters.jinja_filters import (python_comment, indent,
21 rm_fake, remove_ansi, markdown, highlight,
21 rm_fake, remove_ansi, markdown, highlight,
22 ansi2html, markdown2latex, escape_tex)
22 ansi2html, markdown2latex, escape_tex)
23
23
24 from converters.utils import markdown2rst
25
24
26
25
27
26 # Stdlib imports
28 # Stdlib imports
27 import io
29 import io
28 import os
30 import os
29 from IPython.utils import path
31 from IPython.utils import path
30 from IPython.utils.traitlets import MetaHasTraits
32 from IPython.utils.traitlets import MetaHasTraits
31
33
32 from jinja2 import Environment, FileSystemLoader
34 from jinja2 import Environment, FileSystemLoader
33 env = Environment(
35 env = Environment(
34 loader=FileSystemLoader([
36 loader=FileSystemLoader([
35 './templates/',
37 './templates/',
36 './templates/skeleton/',
38 './templates/skeleton/',
37 ]),
39 ]),
38 extensions=['jinja2.ext.loopcontrols']
40 extensions=['jinja2.ext.loopcontrols']
39 )
41 )
40
42
41 texenv = Environment(
43 texenv = Environment(
42 loader=FileSystemLoader([
44 loader=FileSystemLoader([
43 './templates/tex/',
45 './templates/tex/',
44 './templates/skeleton/tex/',
46 './templates/skeleton/tex/',
45 ]),
47 ]),
46 extensions=['jinja2.ext.loopcontrols']
48 extensions=['jinja2.ext.loopcontrols']
47 )
49 )
48
50
49 # IPython imports
51 # IPython imports
50 from IPython.nbformat import current as nbformat
52 from IPython.nbformat import current as nbformat
51 from IPython.config.configurable import Configurable
53 from IPython.config.configurable import Configurable
52 from IPython.utils.traitlets import ( Unicode, Any, List, Bool)
54 from IPython.utils.traitlets import ( Unicode, Any, List, Bool)
53
55
54 #-----------------------------------------------------------------------------
56 #-----------------------------------------------------------------------------
55 # Class declarations
57 # Class declarations
56 #-----------------------------------------------------------------------------
58 #-----------------------------------------------------------------------------
57 class ConversionException(Exception):
59 class ConversionException(Exception):
58 pass
60 pass
59
61
60 #todo move this out
62 #todo move this out
61 def header_body():
63 def header_body():
62 """Return the body of the header as a list of strings."""
64 """Return the body of the header as a list of strings."""
63
65
64 from pygments.formatters import HtmlFormatter
66 from pygments.formatters import HtmlFormatter
65
67
66 header = []
68 header = []
67 static = os.path.join(path.get_ipython_package_dir(),
69 static = os.path.join(path.get_ipython_package_dir(),
68 'frontend', 'html', 'notebook', 'static',
70 'frontend', 'html', 'notebook', 'static',
69 )
71 )
70 here = os.path.split(os.path.realpath(__file__))[0]
72 here = os.path.split(os.path.realpath(__file__))[0]
71 css = os.path.join(static, 'css')
73 css = os.path.join(static, 'css')
72 for sheet in [
74 for sheet in [
73 # do we need jquery and prettify?
75 # do we need jquery and prettify?
74 # os.path.join(static, 'jquery', 'css', 'themes', 'base',
76 # os.path.join(static, 'jquery', 'css', 'themes', 'base',
75 # 'jquery-ui.min.css'),
77 # 'jquery-ui.min.css'),
76 # os.path.join(static, 'prettify', 'prettify.css'),
78 # os.path.join(static, 'prettify', 'prettify.css'),
77 os.path.join(css, 'boilerplate.css'),
79 os.path.join(css, 'boilerplate.css'),
78 os.path.join(css, 'fbm.css'),
80 os.path.join(css, 'fbm.css'),
79 os.path.join(css, 'notebook.css'),
81 os.path.join(css, 'notebook.css'),
80 os.path.join(css, 'renderedhtml.css'),
82 os.path.join(css, 'renderedhtml.css'),
81 # our overrides:
83 # our overrides:
82 os.path.join(here, '..', 'css', 'static_html.css'),
84 os.path.join(here, '..', 'css', 'static_html.css'),
83 ]:
85 ]:
84
86
85 with io.open(sheet, encoding='utf-8') as f:
87 with io.open(sheet, encoding='utf-8') as f:
86 s = f.read()
88 s = f.read()
87 header.append(s)
89 header.append(s)
88
90
89 pygments_css = HtmlFormatter().get_style_defs('.highlight')
91 pygments_css = HtmlFormatter().get_style_defs('.highlight')
90 header.append(pygments_css)
92 header.append(pygments_css)
91 return header
93 return header
92
94
93
95
94
96
95
97
96
98
97 inlining = {}
99 inlining = {}
98 inlining['css'] = header_body()
100 inlining['css'] = header_body()
99
101
100
102
101
103
102 texenv.block_start_string = '((*'
104 texenv.block_start_string = '((*'
103 texenv.block_end_string = '*))'
105 texenv.block_end_string = '*))'
104 texenv.variable_start_string = '((('
106 texenv.variable_start_string = '((('
105 texenv.variable_end_string = ')))'
107 texenv.variable_end_string = ')))'
106 texenv.comment_start_string = '((='
108 texenv.comment_start_string = '((='
107 texenv.comment_end_string = '=))'
109 texenv.comment_end_string = '=))'
108 texenv.filters['escape_tex'] = escape_tex
110 texenv.filters['escape_tex'] = escape_tex
109
111
110
112
111 class ConverterTemplate(Configurable):
113 class ConverterTemplate(Configurable):
112 """ A Jinja2 base converter templates
114 """ A Jinja2 base converter templates
113
115
114 Preprocess the ipynb files, feed it throug jinja templates,
116 Preprocess the ipynb files, feed it throug jinja templates,
115 and spit an converted files and a data object with other data
117 and spit an converted files and a data object with other data
116
118
117 shoudl be mostly configurable
119 shoudl be mostly configurable
118 """
120 """
119
121
120 display_data_priority = List(['html', 'pdf', 'svg', 'latex', 'png', 'jpg', 'jpeg' , 'text'],
122 display_data_priority = List(['html', 'pdf', 'svg', 'latex', 'png', 'jpg', 'jpeg' , 'text'],
121 config=True,
123 config=True,
122 help= """
124 help= """
123 An ordered list of prefered output type, the first
125 An ordered list of prefered output type, the first
124 encounterd will usually be used when converting discarding
126 encounterd will usually be used when converting discarding
125 the others.
127 the others.
126 """
128 """
127 )
129 )
128
130
129 pre_transformer_order = List(['haspyout_transformer'],
131 pre_transformer_order = List(['haspyout_transformer'],
130 config=True,
132 config=True,
131 help= """
133 help= """
132 An ordered list of pre transformer to apply to the ipynb
134 An ordered list of pre transformer to apply to the ipynb
133 file befor running through templates
135 file befor running through templates
134 """
136 """
135 )
137 )
136
138
137 extract_figures = Bool(False,
139 extract_figures = Bool(False,
138 config=True,
140 config=True,
139 help= """
141 help= """
140 wether to remove figure data from ipynb and store them in auxiliary
142 wether to remove figure data from ipynb and store them in auxiliary
141 dictionnary
143 dictionnary
142 """
144 """
143 )
145 )
144
146
145 tex_environement = Bool(False,
147 tex_environement = Bool(False,
146 config=True,
148 config=True,
147 help=""" is this a tex environment or not """)
149 help=""" is this a tex environment or not """)
148
150
149 template_file = Unicode('',
151 template_file = Unicode('',
150 config=True,
152 config=True,
151 help=""" Name of the template file to use """ )
153 help=""" Name of the template file to use """ )
152 #-------------------------------------------------------------------------
154 #-------------------------------------------------------------------------
153 # Instance-level attributes that are set in the constructor for this
155 # Instance-level attributes that are set in the constructor for this
154 # class.
156 # class.
155 #-------------------------------------------------------------------------
157 #-------------------------------------------------------------------------
156 infile = Any()
158 infile = Any()
157
159
158
160
159 infile_dir = Unicode()
161 infile_dir = Unicode()
160
162
161 #todo: move to filter
163 #todo: move to filter
162 def filter_data_type(self, output):
164 def filter_data_type(self, output):
163 """ return the first availlable format in priority """
165 """ return the first availlable format in priority """
164 for fmt in self.display_data_priority:
166 for fmt in self.display_data_priority:
165 if fmt in output:
167 if fmt in output:
166 return [fmt]
168 return [fmt]
167
169
168 preprocessors = []
170 preprocessors = []
169
171
170 def __init__(self, preprocessors={}, jinja_filters={}, config=None, **kw):
172 def __init__(self, preprocessors={}, jinja_filters={}, config=None, **kw):
171 """
173 """
172 config: the Configurable confg object to pass around
174 config: the Configurable confg object to pass around
173
175
174 preprocessors: dict of **availlable** key/value function to run on ipynb json data before conversion
176 preprocessors: dict of **availlable** key/value function to run on ipynb json data before conversion
175 to extract/inline file,
177 to extract/inline file,
176
178
177 """
179 """
178 super(ConverterTemplate, self).__init__(config=config, **kw)
180 super(ConverterTemplate, self).__init__(config=config, **kw)
179 self.env = texenv if self.tex_environement else env
181 self.env = texenv if self.tex_environement else env
180 self.ext = '.tplx' if self.tex_environement else '.tpl'
182 self.ext = '.tplx' if self.tex_environement else '.tpl'
181
183
182 for name in self.pre_transformer_order:
184 for name in self.pre_transformer_order:
183 transformer = getattr(preprocessors, name, getattr(trans, name, None))
185 transformer = getattr(preprocessors, name, getattr(trans, name, None))
184 if isinstance(transformer, MetaHasTraits):
186 if isinstance(transformer, MetaHasTraits):
185 transformer = transformer(config=config)
187 transformer = transformer(config=config)
186 self.preprocessors.append(transformer)
188 self.preprocessors.append(transformer)
187
189
188 ## for compat, remove later
190 ## for compat, remove later
189 if self.extract_figures:
191 if self.extract_figures:
190 self.preprocessors.append(trans.ExtractFigureTransformer(config=config))
192 self.preprocessors.append(trans.ExtractFigureTransformer(config=config))
191
193
192 ##
194 ##
193 self.env.filters['filter_data_type'] = self.filter_data_type
195 self.env.filters['filter_data_type'] = self.filter_data_type
194 self.env.filters['pycomment'] = python_comment
196 self.env.filters['pycomment'] = python_comment
195 self.env.filters['indent'] = indent
197 self.env.filters['indent'] = indent
196 self.env.filters['rm_fake'] = rm_fake
198 self.env.filters['rm_fake'] = rm_fake
197 self.env.filters['rm_ansi'] = remove_ansi
199 self.env.filters['rm_ansi'] = remove_ansi
198 self.env.filters['markdown'] = markdown
200 self.env.filters['markdown'] = markdown
199 self.env.filters['highlight'] = highlight
201 self.env.filters['highlight'] = highlight
200 self.env.filters['ansi2html'] = ansi2html
202 self.env.filters['ansi2html'] = ansi2html
201 self.env.filters['markdown2latex'] = markdown2latex
203 self.env.filters['markdown2latex'] = markdown2latex
204 self.env.filters['markdown2rst'] = markdown2rst
202 for k, v in jinja_filters.iteritems():
205 for k, v in jinja_filters.iteritems():
203 self.env.filters[k] = v
206 self.env.filters[k] = v
204
207
205 self.template = self.env.get_template(self.template_file+self.ext)
208 self.template = self.env.get_template(self.template_file+self.ext)
206
209
207
210
208 def process(self, nb):
211 def process(self, nb):
209 """
212 """
210 preprocess the notebook json for easier use with the templates.
213 preprocess the notebook json for easier use with the templates.
211 will call all the `preprocessor`s in order before returning it.
214 will call all the `preprocessor`s in order before returning it.
212 """
215 """
213
216
214 # dict of 'resources' that could be made by the preprocessors
217 # dict of 'resources' that could be made by the preprocessors
215 # like key/value data to extract files from ipynb like in latex conversion
218 # like key/value data to extract files from ipynb like in latex conversion
216 resources = {}
219 resources = {}
217
220
218 for preprocessor in self.preprocessors:
221 for preprocessor in self.preprocessors:
219 nb, resources = preprocessor(nb, resources)
222 nb, resources = preprocessor(nb, resources)
220
223
221 return nb, resources
224 return nb, resources
222
225
223 def convert(self, nb):
226 def convert(self, nb):
224 """ convert the ipynb file
227 """ convert the ipynb file
225
228
226 return both the converted ipynb file and a dict containing potential
229 return both the converted ipynb file and a dict containing potential
227 other resources
230 other resources
228 """
231 """
229 nb, resources = self.process(nb)
232 nb, resources = self.process(nb)
230 return self.template.render(nb=nb, inlining=inlining), resources
233 return self.template.render(nb=nb, inlining=inlining), resources
231
234
232
235
233 def from_filename(self, filename):
236 def from_filename(self, filename):
234 "read and parse notebook into NotebookNode called self.nb"
237 "read and parse notebook into NotebookNode called self.nb"
235 with io.open(filename) as f:
238 with io.open(filename) as f:
236 return self.convert(nbformat.read(f, 'json'))
239 return self.convert(nbformat.read(f, 'json'))
237
240
General Comments 0
You need to be logged in to leave comments. Login now