Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unicode errors when rendering to IPython notebook #68

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions redbaron.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,13 +354,13 @@ def __repr_html(self):
for num, item in enumerate(self):
yield u"<tr>"
yield u"<td>"
yield str(num)
yield u"%d" % num
yield u"</td>"
yield u"<td>"
yield item._repr_html_() if hasattr(item, "_repr_html_") else str(item)
yield u"</td>"
yield u"</tr>"
yield "</table>"
yield u"</table>"
return u''.join(__repr_html(self))


Expand Down Expand Up @@ -919,7 +919,7 @@ def __str__(self):

def _repr_html_(self):
return highlight(self.dumps(), PythonLexer(encode="Utf-8"),
HtmlFormatter(noclasses=True, encoding="UTf-8"))
HtmlFormatter(noclasses=True))

def copy(self):
# XXX not very optimised but at least very simple
Expand Down Expand Up @@ -1342,13 +1342,13 @@ def __repr_html(self):
for num, item in enumerate(self):
yield u"<tr>"
yield u"<td>"
yield str(num)
yield u"%d" % num
yield u"</td>"
yield u"<td>"
yield item._repr_html_()
yield u"</td>"
yield u"</tr>"
yield "</table>"
yield u"</table>"
return u''.join(__repr_html(self))

def __str__(self):
Expand Down Expand Up @@ -2031,7 +2031,7 @@ def __repr__(self):

def _repr_html_(self):
return highlight(self.__repr__(), PythonLexer(encode="Utf-8"),
HtmlFormatter(noclasses=True, encoding="UTf-8"))
HtmlFormatter(noclasses=True))


class ExceptNode(CodeBlockNode):
Expand Down
41 changes: 41 additions & 0 deletions tests/test_redbaron.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,20 @@

""" Main redbaron test module """

import re
import sys
from redbaron import RedBaron, truncate


python_version = sys.version_info[0]
if python_version == 3:
unicode_type = str
unicode_chr = chr
else:
unicode_type = unicode
unicode_chr = unichr


def test_other_name_assignment():
red = RedBaron("a = b")
assert red.assign is red[0]
Expand Down Expand Up @@ -36,3 +47,33 @@ def test_truncate():
assert "1...6" == truncate("123456", 5)
assert "123456...0" == truncate("12345678901234567890", 10)

def test_html_repr():
def strip_html_tags(s):
assert isinstance(s, unicode_type)
s = re.sub(r'<[^>]+>', '', s)
s = re.sub(r'\n+', '\n', s)
s = re.sub(r'&#([0-9]+);', lambda m: unicode_chr(int(m.group(1))), s)
return s
source = (
b"first = line # commentaire en fran\xC3\x87ais\n"
b'wait()\n'
b"if second_line:\n"
b" # l'unicode est support\xC3\xA9\n"
b" third(line)\n")
if python_version == 3:
source = source.decode('utf-8')
red = RedBaron(source)
assert strip_html_tags(red._repr_html_()) == (
u'Index' u'node'
u'0' u'first = line\n'
u'1' u' # commentaire en fran\xC7ais\n'
u'2' u'wait()\n'
u'3' u'if second_line:\n'
u" # l'unicode est support\xE9\n"
u' third(line)\n')
assert strip_html_tags(red.node_list[1:4]._repr_html_()) == (
u'Index' u'node'
u'0' u' # commentaire en fran\xC7ais\n'
u'1' u"'\\n'\n"
u'2' u'wait()\n')