diff options
author | Waylan Limberg <waylan@gmail.com> | 2010-10-29 21:37:59 -0400 |
---|---|---|
committer | Waylan Limberg <waylan@gmail.com> | 2010-10-29 21:37:59 -0400 |
commit | d4294e1250fac05645ffabb998245c5870a4af8c (patch) | |
tree | 82d8e9f860eb3e289e1f4e6209700f38071cd327 | |
parent | 3740aefd95f6a35d357ef78d3e4966ffd1baf388 (diff) | |
download | markdown-d4294e1250fac05645ffabb998245c5870a4af8c.tar.gz markdown-d4294e1250fac05645ffabb998245c5870a4af8c.tar.bz2 markdown-d4294e1250fac05645ffabb998245c5870a4af8c.zip |
Fixed Ticket 80. Added support for ElementTree Comments to be included by third party extensions when using cElementTree.
-rw-r--r-- | markdown/etree_loader.py | 16 | ||||
-rw-r--r-- | markdown/util.py | 2 | ||||
-rw-r--r-- | tests/test_apis.py | 36 |
3 files changed, 48 insertions, 6 deletions
diff --git a/markdown/etree_loader.py b/markdown/etree_loader.py index 0ea38f6..578fa28 100644 --- a/markdown/etree_loader.py +++ b/markdown/etree_loader.py @@ -8,26 +8,30 @@ def importETree(): etree_in_c = None try: # Is it Python 2.5+ with C implemenation of ElementTree installed? import xml.etree.cElementTree as etree_in_c + from xml.etree.ElementTree import Comment except ImportError: try: # Is it Python 2.5+ with Python implementation of ElementTree? import xml.etree.ElementTree as etree except ImportError: try: # An earlier version of Python with cElementTree installed? import cElementTree as etree_in_c + from elementtree.ElementTree import Comment except ImportError: try: # An earlier version of Python with Python ElementTree? import elementtree.ElementTree as etree except ImportError: message(CRITICAL, "Failed to import ElementTree") sys.exit(1) - if etree_in_c and etree_in_c.VERSION < "1.0": - message(CRITICAL, "For cElementTree version 1.0 or higher is required.") - sys.exit(1) - elif etree_in_c : + if etree_in_c: + if etree_in_c.VERSION < "1.0": + message(CRITICAL, "cElementTree version 1.0 or higher is required.") + sys.exit(1) + # Third party serializers (including ours) require non-c Comment + etree_in_c.Comment = Comment return etree_in_c elif etree.VERSION < "1.1": - message(CRITICAL, "For ElementTree version 1.1 or higher is required") + message(CRITICAL, "ElementTree version 1.1 or higher is required") sys.exit(1) - else : + else: return etree diff --git a/markdown/util.py b/markdown/util.py index f41469b..fa50e83 100644 --- a/markdown/util.py +++ b/markdown/util.py @@ -51,6 +51,8 @@ AUXILIARY GLOBAL FUNCTIONS def isBlockLevel(tag): """Check if the tag is a block level HTML tag.""" + if tag is etree.Comment: + return True return BLOCK_LEVEL_ELEMENTS.match(tag) """ diff --git a/tests/test_apis.py b/tests/test_apis.py index 51f4e73..114b09d 100644 --- a/tests/test_apis.py +++ b/tests/test_apis.py @@ -285,3 +285,39 @@ def _create_fake_extension(name, has_factory_func=True, is_wrong_type=False): # this needs to be specificly overriden or a new python session needs to # be started to get rid of this. This should be ok in a testing context. sys.modules[mod_name] = ext_mod + + +class testETreeComments(unittest.TestCase): + """ + Test that ElementTree Comments work. + + These tests should only be a concern when using cElementTree with third + party serializers (including markdown's html4 serializer). While markdown + doesn't use ElementTree.Comment itself, we should certainly support any + third party extensions which may. Therefore, these tests are included to + ensure such support is maintained. + """ + + def setUp(self): + # Create comment node + self.comment = markdown.util.etree.Comment('foo') + + def testCommentIsComment(self): + """ Test that an ElementTree Comment passes the `is Comment` test. """ + self.assert_(self.comment.tag is markdown.util.etree.Comment) + + def testCommentIsBlockLevel(self): + """ Test that an ElementTree Comment is recognized as BlockLevel. """ + self.assert_(markdown.util.isBlockLevel(self.comment.tag)) + + def testCommentSerialization(self): + """ Test that an ElementTree Comment serializes properly. """ + self.assertEqual(markdown.html4.to_html_string(self.comment), + '<!--foo-->') + + def testCommentPrettify(self): + """ Test that an ElementTree Comment is prettified properly. """ + pretty = markdown.treeprocessors.PrettifyTreeprocessor() + pretty.run(self.comment) + self.assertEqual(markdown.html4.to_html_string(self.comment), + '<!--foo-->\n') |