##// END OF EJS Templates
Backport PR #4054: use unicode for HTML export...
MinRK -
Show More
@@ -255,15 +255,17 b' class RichIPythonWidget(IPythonWidget):'
255 255 except KeyError:
256 256 if not self._svg_warning_displayed:
257 257 QtGui.QMessageBox.warning(self, 'Error converting PNG to SVG.',
258 'Cannot convert a PNG to SVG. To fix this, add this '
258 'Cannot convert PNG images to SVG, export with PNG figures instead. '
259 'If you want to export matplotlib figures as SVG, add '
259 260 'to your ipython config:\n\n'
260 '\tc.InlineBackendConfig.figure_format = \'svg\'\n\n'
261 '\tc.InlineBackend.figure_format = \'svg\'\n\n'
261 262 'And regenerate the figures.',
262 263 QtGui.QMessageBox.Ok)
263 264 self._svg_warning_displayed = True
264 return ("<b>Cannot convert a PNG to SVG.</b> "
265 "To fix this, add this to your config: "
266 "<span>c.InlineBackendConfig.figure_format = 'svg'</span> "
265 return ("<b>Cannot convert PNG images to SVG.</b> "
266 "You must export this session with PNG images. "
267 "If you want to export matplotlib figures as SVG, add to your config "
268 "<span>c.InlineBackend.figure_format = 'svg'</span> "
267 269 "and regenerate the figures.")
268 270
269 271 # Not currently checking path, because it's tricky to find a
@@ -4,11 +4,12 b''
4 4 # Imports
5 5 #-----------------------------------------------------------------------------
6 6
7 # Standard library imports.
7 # Standard library imports
8 import io
8 9 import os
9 10 import re
10 11
11 # System library imports.
12 # System library imports
12 13 from IPython.external.qt import QtGui
13 14
14 15 # IPython imports
@@ -69,7 +70,7 b' class HtmlExporter(object):'
69 70 if dialog.exec_():
70 71 self.filename = dialog.selectedFiles()[0]
71 72 choice = dialog.selectedNameFilter()
72 html = self.control.document().toHtml().encode('utf-8')
73 html = py3compat.cast_unicode(self.control.document().toHtml())
73 74
74 75 # Configure the exporter.
75 76 if choice.startswith('XHTML'):
@@ -127,8 +128,8 b' def export_html(html, filename, image_tag = None, inline = True):'
127 128
128 129 Parameters:
129 130 -----------
130 html : str,
131 A utf-8 encoded Python string containing the Qt HTML to export.
131 html : unicode,
132 A Python unicode string containing the Qt HTML to export.
132 133
133 134 filename : str
134 135 The file to be saved.
@@ -143,8 +144,6 b' def export_html(html, filename, image_tag = None, inline = True):'
143 144 """
144 145 if image_tag is None:
145 146 image_tag = default_image_tag
146 else:
147 image_tag = ensure_utf8(image_tag)
148 147
149 148 if inline:
150 149 path = None
@@ -154,7 +153,7 b' def export_html(html, filename, image_tag = None, inline = True):'
154 153 if os.path.isfile(path):
155 154 raise OSError("%s exists, but is not a directory." % path)
156 155
157 with open(filename, 'w') as f:
156 with io.open(filename, 'w', encoding='utf-8') as f:
158 157 html = fix_html(html)
159 158 f.write(IMG_RE.sub(lambda x: image_tag(x, path = path, format = "png"),
160 159 html))
@@ -165,8 +164,8 b' def export_xhtml(html, filename, image_tag=None):'
165 164
166 165 Parameters:
167 166 -----------
168 html : str,
169 A utf-8 encoded Python string containing the Qt HTML to export.
167 html : unicode,
168 A Python unicode string containing the Qt HTML to export.
170 169
171 170 filename : str
172 171 The file to be saved.
@@ -176,15 +175,13 b' def export_xhtml(html, filename, image_tag=None):'
176 175 """
177 176 if image_tag is None:
178 177 image_tag = default_image_tag
179 else:
180 image_tag = ensure_utf8(image_tag)
181 178
182 with open(filename, 'w') as f:
179 with io.open(filename, 'w', encoding='utf-8') as f:
183 180 # Hack to make xhtml header -- note that we are not doing any check for
184 181 # valid XML.
185 182 offset = html.find("<html>")
186 183 assert offset > -1, 'Invalid HTML string: no <html> tag.'
187 html = ('<html xmlns="http://www.w3.org/1999/xhtml">\n'+
184 html = (u'<html xmlns="http://www.w3.org/1999/xhtml">\n'+
188 185 html[offset+6:])
189 186
190 187 html = fix_html(html)
@@ -213,21 +210,7 b' def default_image_tag(match, path = None, format = "png"):'
213 210 format : "png"|"svg", optional [default "png"]
214 211 Format for returned or referenced images.
215 212 """
216 return ''
217
218
219 def ensure_utf8(image_tag):
220 """wrapper for ensuring image_tag returns utf8-encoded str on Python 2"""
221 if py3compat.PY3:
222 # nothing to do on Python 3
223 return image_tag
224
225 def utf8_image_tag(*args, **kwargs):
226 s = image_tag(*args, **kwargs)
227 if isinstance(s, unicode):
228 s = s.encode('utf8')
229 return s
230 return utf8_image_tag
213 return u''
231 214
232 215
233 216 def fix_html(html):
@@ -235,8 +218,8 b' def fix_html(html):'
235 218
236 219 Parameters:
237 220 -----------
238 html : str,
239 A utf-8 encoded Python string containing the Qt HTML.
221 html : unicode,
222 A Python unicode string containing the Qt HTML.
240 223 """
241 224 # A UTF-8 declaration is needed for proper rendering of some characters
242 225 # (e.g., indented commands) when viewing exported HTML on a local system
General Comments 0
You need to be logged in to leave comments. Login now