Show More
@@ -17,6 +17,9 b' from IPython.external.qt import QtGui' | |||||
17 | # Constants |
|
17 | # Constants | |
18 | #----------------------------------------------------------------------------- |
|
18 | #----------------------------------------------------------------------------- | |
19 |
|
19 | |||
|
20 | # A regular expression for an HTML paragraph with no content. | |||
|
21 | EMPTY_P_RE = re.compile(r'<p[^/>]*>\s*</p>') | |||
|
22 | ||||
20 | # A regular expression for matching images in rich text HTML. |
|
23 | # A regular expression for matching images in rich text HTML. | |
21 | # Note that this is overly restrictive, but Qt's output is predictable... |
|
24 | # Note that this is overly restrictive, but Qt's output is predictable... | |
22 | IMG_RE = re.compile(r'<img src="(?P<name>[\d]+)" />') |
|
25 | IMG_RE = re.compile(r'<img src="(?P<name>[\d]+)" />') | |
@@ -151,7 +154,7 b' def export_html(html, filename, image_tag = None, inline = True):' | |||||
151 | raise OSError("%s exists, but is not a directory." % path) |
|
154 | raise OSError("%s exists, but is not a directory." % path) | |
152 |
|
155 | |||
153 | with open(filename, 'w') as f: |
|
156 | with open(filename, 'w') as f: | |
154 |
html = fix_html |
|
157 | html = fix_html(html) | |
155 | 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"), | |
156 | html)) |
|
159 | html)) | |
157 |
|
160 | |||
@@ -181,7 +184,7 b' def export_xhtml(html, filename, image_tag=None):' | |||||
181 | html = ('<html xmlns="http://www.w3.org/1999/xhtml">\n'+ |
|
184 | html = ('<html xmlns="http://www.w3.org/1999/xhtml">\n'+ | |
182 | html[offset+6:]) |
|
185 | html[offset+6:]) | |
183 |
|
186 | |||
184 |
html = fix_html |
|
187 | html = fix_html(html) | |
185 | f.write(IMG_RE.sub(lambda x: image_tag(x, path = None, format = "svg"), |
|
188 | f.write(IMG_RE.sub(lambda x: image_tag(x, path = None, format = "svg"), | |
186 | html)) |
|
189 | html)) | |
187 |
|
190 | |||
@@ -210,24 +213,26 b' def default_image_tag(match, path = None, format = "png"):' | |||||
210 | return '' |
|
213 | return '' | |
211 |
|
214 | |||
212 |
|
215 | |||
213 |
def fix_html |
|
216 | def fix_html(html): | |
214 | """ Return html string, with a UTF-8 declaration added to <HEAD>. |
|
217 | """ Transforms a Qt-generated HTML string into a standards-compliant one. | |
215 |
|
||||
216 | Assumes that html is Qt generated and has already been UTF-8 encoded |
|
|||
217 | and coerced to a python string. If the expected head element is |
|
|||
218 | not found, the given object is returned unmodified. |
|
|||
219 |
|
||||
220 | This patching is needed for proper rendering of some characters |
|
|||
221 | (e.g., indented commands) when viewing exported HTML on a local |
|
|||
222 | system (i.e., without seeing an encoding declaration in an HTTP |
|
|||
223 | header). |
|
|||
224 |
|
218 | |||
225 | C.f. http://www.w3.org/International/O-charset for details. |
|
219 | Parameters: | |
|
220 | ----------- | |||
|
221 | html : str, | |||
|
222 | A utf-8 encoded Python string containing the Qt HTML. | |||
226 | """ |
|
223 | """ | |
|
224 | # A UTF-8 declaration is needed for proper rendering of some characters | |||
|
225 | # (e.g., indented commands) when viewing exported HTML on a local system | |||
|
226 | # (i.e., without seeing an encoding declaration in an HTTP header). | |||
|
227 | # C.f. http://www.w3.org/International/O-charset for details. | |||
227 | offset = html.find('<head>') |
|
228 | offset = html.find('<head>') | |
228 | if offset > -1: |
|
229 | if offset > -1: | |
229 | html = (html[:offset+6]+ |
|
230 | html = (html[:offset+6]+ | |
230 | '\n<meta http-equiv="Content-Type" '+ |
|
231 | '\n<meta http-equiv="Content-Type" '+ | |
231 | 'content="text/html; charset=utf-8" />\n'+ |
|
232 | 'content="text/html; charset=utf-8" />\n'+ | |
232 | html[offset+6:]) |
|
233 | html[offset+6:]) | |
|
234 | ||||
|
235 | # Replace empty paragraphs tags with line breaks. | |||
|
236 | html = re.sub(EMPTY_P_RE, '<br/>', html) | |||
|
237 | ||||
233 | return html |
|
238 | return html |
General Comments 0
You need to be logged in to leave comments.
Login now