diff options
author | facelessuser <faceless.shop@gmail.com> | 2017-11-16 18:47:41 -0700 |
---|---|---|
committer | facelessuser <faceless.shop@gmail.com> | 2017-11-16 18:47:41 -0700 |
commit | 56c5a63a15209ad82bd2104a620478be42457131 (patch) | |
tree | 29dafacb032b62c078582cd5f3b862331bbaeeeb | |
parent | f018b0ede5a49c8d711351d8e9ff437c97cf3acc (diff) | |
download | python-markdown-56c5a63a15209ad82bd2104a620478be42457131.tar.gz |
Ancestory experiment
Use ancestory to avoid processing code blocks. For every node that is handled via the inline pattern, we will build up a parent list. As we decend into recursion, we append and pop children as needed. Though it is not practical, for this experiment, we purposely leave code tag text unprotected and default all inline patterns to avoid 'code'.
-rw-r--r-- | markdown/inlinepatterns.py | 6 | ||||
-rw-r--r-- | markdown/treeprocessors.py | 26 |
2 files changed, 31 insertions, 1 deletions
diff --git a/markdown/inlinepatterns.py b/markdown/inlinepatterns.py index 3658ebd..8b3c74d 100644 --- a/markdown/inlinepatterns.py +++ b/markdown/inlinepatterns.py @@ -207,6 +207,10 @@ class Pattern(object): if markdown_instance: self.markdown = markdown_instance + def getExcludes(self): + """Get tag to exclude.""" + return ['code'] + def getCompiledRegExp(self): """ Return a compiled regular expression. """ return self.compiled_re @@ -308,7 +312,7 @@ class BacktickPattern(Pattern): def handleMatch(self, m): if m.group(4): el = util.etree.Element(self.tag) - el.text = util.AtomicString(m.group(4).strip()) + el.text = m.group(4).strip() return el else: return m.group(2).replace('\\\\', self.ESCAPED_BSLASH) diff --git a/markdown/treeprocessors.py b/markdown/treeprocessors.py index bb76572..3bc4013 100644 --- a/markdown/treeprocessors.py +++ b/markdown/treeprocessors.py @@ -54,6 +54,7 @@ class InlineProcessor(Treeprocessor): self.__placeholder_re = util.INLINE_PLACEHOLDER_RE self.markdown = md self.inlinePatterns = md.inlinePatterns + self.ancestors = [] def __makePlaceholder(self, type): """ Generate a placeholder """ @@ -236,6 +237,10 @@ class InlineProcessor(Treeprocessor): if not match: return data, False, 0 + for exclude in pattern.getExcludes(): + if exclude.lower() in self.ancestors: + return data, False, 0 + node = pattern.handleMatch(match) if node is None: @@ -245,15 +250,19 @@ class InlineProcessor(Treeprocessor): if not isinstance(node.text, util.AtomicString): # We need to process current node too for child in [node] + list(node): + self.ancestors.append(child.tag.lower()) if not isString(node): if child.text: child.text = self.__handleInline( child.text, patternIndex + 1 ) + self.ancestors.pop() if child.tail: child.tail = self.__handleInline( child.tail, patternIndex ) + else: + self.ancestors.pop() placeholder = self.__stashNode(node, pattern.type()) @@ -261,6 +270,18 @@ class InlineProcessor(Treeprocessor): match.group(1), placeholder, match.groups()[-1]), True, 0 + def __build_ancestors(self, parent): + """Build the ancestor list.""" + + ancestors = [] + parent_map = dict((c, p) for p in parent.getiterator() for c in p) + ancestors.append(parent.tag) + while parent: + parent = parent_map.get(parent) + if parent: + ancestors.append(parent.tag) + return list(reversed(ancestors)) + def run(self, tree): """Apply inline patterns to a parsed Markdown tree. @@ -284,8 +305,12 @@ class InlineProcessor(Treeprocessor): while stack: currElement = stack.pop() + + self.ancestors = self.__build_ancestors(currElement) + insertQueue = [] for child in currElement: + self.ancestors.append(child.tag.lower()) if child.text and not isinstance( child.text, util.AtomicString ): @@ -296,6 +321,7 @@ class InlineProcessor(Treeprocessor): ) stack += lst insertQueue.append((child, lst)) + self.ancestors.pop() if child.tail: tail = self.__handleInline(child.tail) dumby = util.etree.Element('d') |