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