Skip to content
This repository

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

Merged
merged 1 commit into from over 2 years ago

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 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 December 16, 2011
IPython/frontend/qt/rich_text.py
@@ -210,6 +216,20 @@ def default_image_tag(match, path = None, format = "png"):
210 216
     return ''
211 217
 
212 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
3
Fernando Perez Owner
fperez added a note December 16, 2011

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 December 16, 2011
Fernando Perez Owner
fperez added a note December 16, 2011

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 December 16, 2011
Fernando Perez fperez merged commit 376addf into from December 16, 2011
Fernando Perez fperez referenced this pull request from a commit January 10, 2012
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

Showing 1 unique commit by 1 author.

Dec 16, 2011
Min RK 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
This page is out of date. Refresh to see the latest.

Showing 1 changed file with 21 additions and 1 deletion. Show diff stats Hide diff stats

  1. 22  IPython/frontend/qt/rich_text.py
22  IPython/frontend/qt/rich_text.py
@@ -11,6 +11,9 @@
11 11
 # System library imports.
12 12
 from IPython.external.qt import QtGui
13 13
 
  14
+# IPython imports
  15
+from IPython.utils import py3compat
  16
+
14 17
 #-----------------------------------------------------------------------------
15 18
 # Constants
16 19
 #-----------------------------------------------------------------------------
@@ -92,7 +95,6 @@ def export(self):
92 95
                     box.setInformativeText(info)
93 96
                     box.addButton(ib, QtGui.QMessageBox.NoRole)
94 97
                     box.addButton(eb, QtGui.QMessageBox.YesRole)
95  
-                    box.setDefaultButton(ib)
96 98
                     layout.setSpacing(0)
97 99
                     layout.addWidget(box)
98 100
                     layout.addWidget(checkbox)
@@ -141,6 +143,8 @@ def export_html(html, filename, image_tag = None, inline = True):
141 143
     """
142 144
     if image_tag is None:
143 145
         image_tag = default_image_tag
  146
+    else:
  147
+        image_tag = ensure_utf8(image_tag)
144 148
 
145 149
     if inline:
146 150
         path = None
@@ -172,6 +176,8 @@ def export_xhtml(html, filename, image_tag=None):
172 176
     """
173 177
     if image_tag is None:
174 178
         image_tag = default_image_tag
  179
+    else:
  180
+        image_tag = ensure_utf8(image_tag)
175 181
 
176 182
     with open(filename, 'w') as f:
177 183
         # 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"):
210 216
     return ''
211 217
 
212 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
+
  232
+
213 233
 def fix_html(html):
214 234
     """ Transforms a Qt-generated HTML string into a standards-compliant one.
215 235
 
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.