diff options
author | facelessuser <faceless.shop@gmail.com> | 2017-11-17 01:38:23 -0700 |
---|---|---|
committer | facelessuser <faceless.shop@gmail.com> | 2017-11-17 01:38:23 -0700 |
commit | 82f7feea7ae88a4f5e2d4e9d99006c4cdd3325a7 (patch) | |
tree | 9e887dec0f13fa161927286aedb38940a88b63b8 | |
parent | 33c53ffe73c1c8d1b771804514c8a3a157434c76 (diff) | |
download | python-markdown-82f7feea7ae88a4f5e2d4e9d99006c4cdd3325a7.tar.gz |
Better ancestry tracking
1. If initalizing the inline processor with an element, allow passing in a list of that elements parents.
2. Update the parent map along the way so that children added to the stack will have their ancestry built proper from the parent_map.
3. Move some poorly placed pushing and popping of the ancestry stack to more appropriate places.
-rw-r--r-- | markdown/treeprocessors.py | 25 |
1 files changed, 14 insertions, 11 deletions
diff --git a/markdown/treeprocessors.py b/markdown/treeprocessors.py index ae411e2..31adbce 100644 --- a/markdown/treeprocessors.py +++ b/markdown/treeprocessors.py @@ -231,8 +231,6 @@ class InlineProcessor(Treeprocessor): Returns: String with placeholders instead of ElementTree elements. """ - # print(self.ancestors) - # print(data[startIndex:]) for exclude in pattern.getExcludes(): if exclude.lower() in self.ancestors: return data, False, 0 @@ -252,8 +250,8 @@ 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): + self.ancestors.append(child.tag.lower()) if child.text: child.text = self.__handleInline( child.text, patternIndex + 1 @@ -263,8 +261,6 @@ class InlineProcessor(Treeprocessor): child.tail = self.__handleInline( child.tail, patternIndex ) - else: - self.ancestors.pop() placeholder = self.__stashNode(node, pattern.type()) @@ -274,18 +270,15 @@ class InlineProcessor(Treeprocessor): def __build_ancestors(self, parent, parents): """Build the ancestor list.""" - ancestors = [] - parent_map = dict((c, p) for p in parent.getiterator() for c in p) - ancestors.append(parent.tag.lower()) while parent: - parent = parent_map.get(parent) if parent: ancestors.append(parent.tag.lower()) + parent = self.parent_map.get(parent) ancestors.reverse() parents.extend(ancestors) - def run(self, tree): + def run(self, tree, ancestors=None): """Apply inline patterns to a parsed Markdown tree. Iterate over ElementTree, find elements with inline tag, apply inline @@ -298,13 +291,19 @@ class InlineProcessor(Treeprocessor): Arguments: * tree: ElementTree object, representing Markdown tree. + * ancestors: List of parent tag names that preceed the tree node (if needed). Returns: ElementTree object with applied inline patterns. """ self.stashed_nodes = {} - stack = [(tree, [])] + # Ensure a valid parent list, but copy passed in lists + # to ensure we don't have the user accidentally change it on us. + tree_parents = [] if ancestors is None else ancestors[:] + + self.parent_map = dict((c, p) for p in tree.getiterator() for c in p) + stack = [(tree, tree_parents)] while stack: currElement, parents = stack.pop() @@ -323,6 +322,8 @@ class InlineProcessor(Treeprocessor): lst = self.__processPlaceholders( self.__handleInline(text), child ) + for l in lst: + self.parent_map[l[0]] = child stack += lst insertQueue.append((child, lst)) self.ancestors.pop() @@ -336,8 +337,10 @@ class InlineProcessor(Treeprocessor): pos = list(currElement).index(child) + 1 tailResult.reverse() for newChild in tailResult: + self.parent_map[newChild[0]] = currElement currElement.insert(pos, newChild[0]) if len(child): + self.parent_map[child] = currElement stack.append((child, self.ancestors[:])) for element, lst in insertQueue: |