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