From a5fb7dcbf6bc7dd67ef41e41653e8b34093a714b Mon Sep 17 00:00:00 2001 From: facelessuser Date: Sat, 18 Nov 2017 09:11:45 -0700 Subject: Change excludes from method to class attribute Remove `getExcludes` and replace with a class attribute `ANCESTOR_EXCLUDES`. --- docs/extensions/api.txt | 10 ++++------ markdown/inlinepatterns.py | 6 ++---- markdown/treeprocessors.py | 3 ++- tests/test_apis.py | 4 +--- 4 files changed, 9 insertions(+), 14 deletions(-) diff --git a/docs/extensions/api.txt b/docs/extensions/api.txt index 29bf83f..246bb27 100644 --- a/docs/extensions/api.txt +++ b/docs/extensions/api.txt @@ -53,7 +53,7 @@ A pseudo example: Inline Patterns {: #inlinepatterns } ------------------------------------ -Inline Patterns implement the inline HTML element syntax for Markdown such as +Inline Patterns implement the inline HTML element syntax for Markdown such as `*emphasis*` or `[links](http://example.com)`. Pattern objects should be instances of classes that inherit from `markdown.inlinepatterns.Pattern` or one of its children. Each pattern object uses a single regular expression and @@ -68,11 +68,9 @@ must have the following methods: Accepts a match object and returns an ElementTree element of a plain Unicode string. -* **`getExcludes()`**: - - Returns an array of tag names that are undesirable ancestors. The pattern - should not match if it would cause the content to be a descendant of one - of the tag names in the list. +Also, Inline Patterns can define the property `ANCESTOR_EXCLUDES` with either +a list or tuple of undesirable ancestors. The pattern should not match if it +would cause the content to be a descendant of one of the defined tag names. Note that any regular expression returned by `getCompiledRegExp` must capture the whole block. Therefore, they should all start with `r'^(.*?)'` and end diff --git a/markdown/inlinepatterns.py b/markdown/inlinepatterns.py index f483f99..2f00b3d 100644 --- a/markdown/inlinepatterns.py +++ b/markdown/inlinepatterns.py @@ -189,6 +189,8 @@ The pattern classes class Pattern(object): """Base class that inline patterns subclass. """ + ANCESTOR_EXCLUDES = tuple() + def __init__(self, pattern, markdown_instance=None): """ Create an instant of an inline pattern. @@ -207,10 +209,6 @@ class Pattern(object): if markdown_instance: self.markdown = markdown_instance - def getExcludes(self): - """Get tag to exclude.""" - return [] - def getCompiledRegExp(self): """ Return a compiled regular expression. """ return self.compiled_re diff --git a/markdown/treeprocessors.py b/markdown/treeprocessors.py index ab31dd6..f159a8a 100644 --- a/markdown/treeprocessors.py +++ b/markdown/treeprocessors.py @@ -231,7 +231,8 @@ class InlineProcessor(Treeprocessor): Returns: String with placeholders instead of ElementTree elements. """ - for exclude in pattern.getExcludes(): + + for exclude in pattern.ANCESTOR_EXCLUDES: if exclude.lower() in self.ancestors: return data, False, 0 diff --git a/tests/test_apis.py b/tests/test_apis.py index cbcb989..48e79e8 100644 --- a/tests/test_apis.py +++ b/tests/test_apis.py @@ -778,9 +778,7 @@ class TestAncestorExclusion(unittest.TestCase): class AncestorExample(markdown.inlinepatterns.SimpleTagPattern): """ Ancestor Test. """ - def getExcludes(self): - """ Tags to exclude. """ - return ['a'] + ANCESTOR_EXCLUDES = ('a',) def handleMatch(self, m): """ Handle match. """ -- cgit v1.2.1