##// END OF EJS Templates
Fixed empty lines not showing up in ConsoleWidget HTML export.
epatters -
Show More
@@ -17,6 +17,9 from IPython.external.qt import QtGui
17 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 23 # A regular expression for matching images in rich text HTML.
21 24 # Note that this is overly restrictive, but Qt's output is predictable...
22 25 IMG_RE = re.compile(r'<img src="(?P<name>[\d]+)" />')
@@ -151,7 +154,7 def export_html(html, filename, image_tag = None, inline = True):
151 154 raise OSError("%s exists, but is not a directory." % path)
152 155
153 156 with open(filename, 'w') as f:
154 html = fix_html_encoding(html)
157 html = fix_html(html)
155 158 f.write(IMG_RE.sub(lambda x: image_tag(x, path = path, format = "png"),
156 159 html))
157 160
@@ -181,7 +184,7 def export_xhtml(html, filename, image_tag=None):
181 184 html = ('<html xmlns="http://www.w3.org/1999/xhtml">\n'+
182 185 html[offset+6:])
183 186
184 html = fix_html_encoding(html)
187 html = fix_html(html)
185 188 f.write(IMG_RE.sub(lambda x: image_tag(x, path = None, format = "svg"),
186 189 html))
187 190
@@ -210,24 +213,26 def default_image_tag(match, path = None, format = "png"):
210 213 return ''
211 214
212 215
213 def fix_html_encoding(html):
214 """ Return html string, with a UTF-8 declaration added to <HEAD>.
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).
216 def fix_html(html):
217 """ Transforms a Qt-generated HTML string into a standards-compliant one.
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 228 offset = html.find('<head>')
228 229 if offset > -1:
229 230 html = (html[:offset+6]+
230 231 '\n<meta http-equiv="Content-Type" '+
231 232 'content="text/html; charset=utf-8" />\n'+
232 233 html[offset+6:])
234
235 # Replace empty paragraphs tags with line breaks.
236 html = re.sub(EMPTY_P_RE, '<br/>', html)
237
233 238 return html
General Comments 0
You need to be logged in to leave comments. Login now