Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

encode image_tag as utf8 in [x]html export #1165

Merged
merged 1 commit into from

2 participants

Min RK Fernando Perez
Min RK
Owner

Should only affect Python 2

Also removes redundant 'setDefaultButton' in Inline/External dialog, which caused strange display of the two buttons.

closes #1164

Min RK minrk encode image_tag as utf8 in [x]html export
Should only affect Python 2

also removes redundant 'setDefaultButton' in Inline/External dialog,
which caused strange display of the two buttons.
971ca49
Fernando Perez fperez commented on the diff
IPython/frontend/qt/rich_text.py
@@ -210,6 +216,20 @@ def default_image_tag(match, path = None, format = "png"):
return ''
+def ensure_utf8(image_tag):
+ """wrapper for ensuring image_tag returns utf8-encoded str on Python 2"""
+ if py3compat.PY3:
+ # nothing to do on Python 3
+ return image_tag
+
+ def utf8_image_tag(*args, **kwargs):
+ s = image_tag(*args, **kwargs)
+ if isinstance(s, unicode):
+ s = s.encode('utf8')
+ return s
+ return utf8_image_tag
Fernando Perez Owner
fperez added a note

Why are you returning the function here? I think it should instead read

return utf8_image_tag(image_tag)
Min RK Owner
minrk added a note
Fernando Perez Owner
fperez added a note

Ah, got it. I didn't realize it was being used as a decorator, I mistakenly thought image_tag was a string.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Fernando Perez
Owner

OK, code looks otherwise good. I checked previously failing situations now work correctly. Thanks!

Merging now.

Fernando Perez fperez closed this
Fernando Perez fperez merged commit 376addf into from
Fernando Perez fperez referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 16, 2011
  1. Min RK

    encode image_tag as utf8 in [x]html export

    minrk authored
    Should only affect Python 2
    
    also removes redundant 'setDefaultButton' in Inline/External dialog,
    which caused strange display of the two buttons.
This page is out of date. Refresh to see the latest.
Showing with 21 additions and 1 deletion.
  1. +21 −1 IPython/frontend/qt/rich_text.py
22 IPython/frontend/qt/rich_text.py
View
@@ -11,6 +11,9 @@
# System library imports.
from IPython.external.qt import QtGui
+# IPython imports
+from IPython.utils import py3compat
+
#-----------------------------------------------------------------------------
# Constants
#-----------------------------------------------------------------------------
@@ -92,7 +95,6 @@ def export(self):
box.setInformativeText(info)
box.addButton(ib, QtGui.QMessageBox.NoRole)
box.addButton(eb, QtGui.QMessageBox.YesRole)
- box.setDefaultButton(ib)
layout.setSpacing(0)
layout.addWidget(box)
layout.addWidget(checkbox)
@@ -141,6 +143,8 @@ def export_html(html, filename, image_tag = None, inline = True):
"""
if image_tag is None:
image_tag = default_image_tag
+ else:
+ image_tag = ensure_utf8(image_tag)
if inline:
path = None
@@ -172,6 +176,8 @@ def export_xhtml(html, filename, image_tag=None):
"""
if image_tag is None:
image_tag = default_image_tag
+ else:
+ image_tag = ensure_utf8(image_tag)
with open(filename, 'w') as f:
# Hack to make xhtml header -- note that we are not doing any check for
@@ -210,6 +216,20 @@ def default_image_tag(match, path = None, format = "png"):
return ''
+def ensure_utf8(image_tag):
+ """wrapper for ensuring image_tag returns utf8-encoded str on Python 2"""
+ if py3compat.PY3:
+ # nothing to do on Python 3
+ return image_tag
+
+ def utf8_image_tag(*args, **kwargs):
+ s = image_tag(*args, **kwargs)
+ if isinstance(s, unicode):
+ s = s.encode('utf8')
+ return s
+ return utf8_image_tag
Fernando Perez Owner
fperez added a note

Why are you returning the function here? I think it should instead read

return utf8_image_tag(image_tag)
Min RK Owner
minrk added a note
Fernando Perez Owner
fperez added a note

Ah, got it. I didn't realize it was being used as a decorator, I mistakenly thought image_tag was a string.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+
+
def fix_html(html):
""" Transforms a Qt-generated HTML string into a standards-compliant one.
Something went wrong with that request. Please try again.