diff --git a/redbaron.py b/redbaron.py index 7db6b687..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)) @@ -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 @@ -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): @@ -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): 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') +