Skip to content

Commit

Permalink
Merge pull request ipython#1165 from minrk/save
Browse files Browse the repository at this point in the history
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.

closes ipython#1164
  • Loading branch information
fperez committed Dec 16, 2011
2 parents 3a25d02 + 5a66a5a commit 69cb5e8
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion IPython/frontend/qt/rich_text.py
Expand Up @@ -11,6 +11,9 @@
# System library imports.
from IPython.external.qt import QtGui

# IPython imports
from IPython.utils import py3compat

#-----------------------------------------------------------------------------
# Constants
#-----------------------------------------------------------------------------
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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


def fix_html(html):
""" Transforms a Qt-generated HTML string into a standards-compliant one.
Expand Down

0 comments on commit 69cb5e8

Please sign in to comment.