diff options
| author | wiemann <wiemann@929543f6-e4f2-0310-98a6-ba3bd3dd1d04> | 2005-03-05 15:53:45 +0000 |
|---|---|---|
| committer | wiemann <wiemann@929543f6-e4f2-0310-98a6-ba3bd3dd1d04> | 2005-03-05 15:53:45 +0000 |
| commit | cb57e80d4b9d16aed07b08408f9e7ac527833bd1 (patch) | |
| tree | ccfa7cb0889c2fe05a2e5fe2fea1db1859ceba99 /docutils/nodes.py | |
| parent | fb848ff48742947b27a9fc5f57058f9f9e28cba6 (diff) | |
| download | docutils-cb57e80d4b9d16aed07b08408f9e7ac527833bd1.tar.gz | |
added Node.has_children() and Node.next_node()
git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@3007 929543f6-e4f2-0310-98a6-ba3bd3dd1d04
Diffstat (limited to 'docutils/nodes.py')
| -rw-r--r-- | docutils/nodes.py | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/docutils/nodes.py b/docutils/nodes.py index 2cb04300c..51bcb583d 100644 --- a/docutils/nodes.py +++ b/docutils/nodes.py @@ -161,6 +161,40 @@ class Node: category='nodes.Node.walkabout') visitor.dispatch_departure(self) + def has_children(self): + """Return true if this node has children.""" + return 0 + + def next_node(self, descend=1, ascend=1, cond=lambda e: 1): + """ + Return the next node in tree traversal order for which the + condition cond(node) is true. + + If descend is true, traverse children as well. If ascend is + true, go up in the tree if there is no direct next sibling. + Return None if there is no next node. + """ + node = self + while 1: + if descend and node.has_children(): + r = node[0] + elif node.parent is not None: + # Index of the next sibling. + index = node.parent.index(node) + 1 + if index < len(node.parent): + r = node.parent[index] + elif ascend: + r = node.parent.next_node(descend=0, ascend=1) + else: + return None + else: + return None + if cond(r): + return r + else: + # Get r.next_node(...), avoiding recursion. + node = r + class Text(Node, UserString): @@ -505,6 +539,10 @@ class Element(Node): """Return this element's children.""" return self.children + def has_children(self): + """Return true if this node has children.""" + return len(self.children) > 0 + def copy(self): return self.__class__(**self.attributes) |
