diff options
author | Isaac Muse <faceless.shop@gmail.com> | 2017-11-23 07:56:38 -0700 |
---|---|---|
committer | Waylan Limberg <waylan.limberg@icloud.com> | 2017-11-23 09:56:38 -0500 |
commit | de5c696f94e8dde242c29d4be50b7bbf3c17fedb (patch) | |
tree | 31c6c11b698be4b284c9b93b05a4d0ca3bd58e6a /tests/test_apis.py | |
parent | 007bd2aa4c184b28f710d041a0abe78bffc0ec2e (diff) | |
download | markdown-de5c696f94e8dde242c29d4be50b7bbf3c17fedb.tar.gz markdown-de5c696f94e8dde242c29d4be50b7bbf3c17fedb.tar.bz2 markdown-de5c696f94e8dde242c29d4be50b7bbf3c17fedb.zip |
Feature ancestry (#598)
Ancestry exclusion for inline patterns.
Adds the ability for an inline pattern to define a list of ancestor tag names that should be avoided. If a pattern would create a descendant of one of the listed tag names, the pattern will not match. Fixes #596.
Diffstat (limited to 'tests/test_apis.py')
-rw-r--r-- | tests/test_apis.py | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/tests/test_apis.py b/tests/test_apis.py index 7b1214f..48e79e8 100644 --- a/tests/test_apis.py +++ b/tests/test_apis.py @@ -770,3 +770,55 @@ class TestEscapeAppend(unittest.TestCase): self.assertEqual('|' in md.ESCAPED_CHARS, True) md2 = markdown.Markdown() self.assertEqual('|' not in md2.ESCAPED_CHARS, True) + + +class TestAncestorExclusion(unittest.TestCase): + """ Tests exclusion of tags in ancestor list. """ + + class AncestorExample(markdown.inlinepatterns.SimpleTagPattern): + """ Ancestor Test. """ + + ANCESTOR_EXCLUDES = ('a',) + + def handleMatch(self, m): + """ Handle match. """ + el = markdown.util.etree.Element(self.tag) + el.text = m.group(3) + return el + + class AncestorExtension(markdown.Extension): + + def __init__(self, *args, **kwargs): + """Initialize.""" + + self.config = {} + + def extendMarkdown(self, md, md_globals): + """Modify inline patterns.""" + + pattern = r'(\+)([^\+]+)\2' + md.inlinePatterns["ancestor-test"] = TestAncestorExclusion.AncestorExample(pattern, 'strong') + + def setUp(self): + """Setup markdown object.""" + self.md = markdown.Markdown(extensions=[TestAncestorExclusion.AncestorExtension()]) + + def test_ancestors(self): + """ Test that an extension can exclude parent tags. """ + test = """ +Some +test+ and a [+link+](http://test.com) +""" + result = """<p>Some <strong>test</strong> and a <a href="http://test.com">+link+</a></p>""" + + self.md.reset() + self.assertEqual(self.md.convert(test), result) + + def test_ancestors_tail(self): + """ Test that an extension can exclude parent tags when dealing with a tail. """ + test = """ +[***+em+*+strong+**](http://test.com) +""" + result = """<p><a href="http://test.com"><strong><em>+em+</em>+strong+</strong></a></p>""" + + self.md.reset() + self.assertEqual(self.md.convert(test), result) |