##// END OF EJS Templates
use unicode for HTML export...
MinRK -
Show More
@@ -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