From ad1c8d8a09e4bea8492a1e25ab543def24819b13 Mon Sep 17 00:00:00 2001 From: Remi Rampin Date: Thu, 28 May 2015 15:43:18 -0400 Subject: [PATCH 1/3] [enh] return unicode from _repr_html_ Do no make Pygments encode highlighted source code, keep unicode. This is already done for NodeList and ProxyList, fix Node and EndlNode. --- redbaron.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/redbaron.py b/redbaron.py index 7db6b687..48b69d20 100644 --- a/redbaron.py +++ b/redbaron.py @@ -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 @@ -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): From a48d367fce06a8fc859e3b5597f5ef45afd00a52 Mon Sep 17 00:00:00 2001 From: Remi Rampin Date: Thu, 28 May 2015 15:46:22 -0400 Subject: [PATCH 2/3] [fix] don't join bytes object in _repr_html_() NodeList and ProxyList do a unicode join() to construct the final unicode object; ths makes every element unicode to begin with, to avoid automatic decoding. --- redbaron.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/redbaron.py b/redbaron.py index 48b69d20..a3083a3b 100644 --- a/redbaron.py +++ b/redbaron.py @@ -354,13 +354,13 @@ def __repr_html(self): for num, item in enumerate(self): yield u"" yield u"" - yield str(num) + yield u"%d" % num yield u"" yield u"" yield item._repr_html_() if hasattr(item, "_repr_html_") else str(item) yield u"" yield u"" - yield "" + yield u"" return u''.join(__repr_html(self)) @@ -1342,13 +1342,13 @@ def __repr_html(self): for num, item in enumerate(self): yield u"" yield u"" - yield str(num) + yield u"%d" % num yield u"" yield u"" yield item._repr_html_() yield u"" yield u"" - yield "" + yield u"" return u''.join(__repr_html(self)) def __str__(self): From 368cfc1d3c77c2ed1ccc359e100e22434f4ef86c Mon Sep 17 00:00:00 2001 From: Remi Rampin Date: Fri, 29 May 2015 14:56:36 -0400 Subject: [PATCH 3/3] [enh] adds test for _repr_html_() and unicode --- tests/test_redbaron.py | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/tests/test_redbaron.py b/tests/test_redbaron.py index 90c51654..54bf5d39 100644 --- a/tests/test_redbaron.py +++ b/tests/test_redbaron.py @@ -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] @@ -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') +