From f7f20b5294b4a8ffd78a117c3a92d905d0b524ed 2013-12-15 01:32:07 From: MinRK Date: 2013-12-15 01:32:07 Subject: [PATCH] don't do anything if add_anchor fails --- diff --git a/IPython/nbconvert/filters/strings.py b/IPython/nbconvert/filters/strings.py index 97ef3a3..1931722 100755 --- a/IPython/nbconvert/filters/strings.py +++ b/IPython/nbconvert/filters/strings.py @@ -72,7 +72,11 @@ def html2text(element): Analog of jQuery's $(element).text() """ if isinstance(element, py3compat.string_types): - element = ElementTree.fromstring(element) + try: + element = ElementTree.fromstring(element) + except Exception: + # failed to parse, just return it unmodified + return element text = element.text or "" for child in element: @@ -86,7 +90,11 @@ def add_anchor(html): For use in heading cells """ - h = ElementTree.fromstring(py3compat.cast_bytes_py2(html, encoding='utf-8')) + try: + h = ElementTree.fromstring(py3compat.cast_bytes_py2(html, encoding='utf-8')) + except Exception: + # failed to parse, just return it unmodified + return html link = html2text(h).replace(' ', '-') h.set('id', link) a = ElementTree.Element("a", {"class" : "anchor-link", "href" : "#" + link}) diff --git a/IPython/nbconvert/filters/tests/test_strings.py b/IPython/nbconvert/filters/tests/test_strings.py index 9fc6d63..7311a28 100644 --- a/IPython/nbconvert/filters/tests/test_strings.py +++ b/IPython/nbconvert/filters/tests/test_strings.py @@ -60,7 +60,12 @@ class TestStrings(TestsBase): assert '' in results - + def test_add_anchor_fail(self): + """add_anchor does nothing when it fails""" + html = '

Hello
World!

' + results = add_anchor(html) + self.assertEqual(html, results) + def test_strip_dollars(self): """strip_dollars test""" tests = [