Skip to content

Commit 257c0fe

Browse files
committed
Better docs of treeprocessor API.
Fixes #375. Explains the difference between returning None and returning a modified root element. Also makes the docs more consistent with the doc strings in the code.
1 parent fad98e5 commit 257c0fe

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

docs/extensions/api.txt

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,16 +136,27 @@ the tree and runs the InlinePatterns on the text of each Element in the tree.
136136

137137
A Treeprocessor should inherit from ``markdown.treeprocessors.Treeprocessor``,
138138
over-ride the ``run`` method which takes one argument ``root`` (an Elementree
139-
object) and returns either that root element or a modified root element.
139+
object) and either modifies that root element and returns `None` or returns a
140+
new ElementTree object.
140141

141142
A pseudo example:
142143

143144
from markdown.treeprocessors import Treeprocessor
144145

145146
class MyTreeprocessor(Treeprocessor):
146147
def run(self, root):
147-
#do stuff
148-
return my_modified_root
148+
root.text = 'modified content'
149+
150+
Note that Python class methods return `None` by default when no `return`
151+
statement is defined. Additionly all Python variables refer to objects by
152+
reference. Therefore, the above `run` method modifies the `root` element
153+
in place and returns `None`. The changes made to the `root` element and its
154+
children are retained.
155+
156+
Some may be inclined to return the modified `root` element. While that would
157+
work, it would cause a copy of the entire ElemetTree to be generated each
158+
time the treeprocessor is run. Therefore, it is generally expected that
159+
the `run` method would only return `None` or a new ElementTree object.
149160

150161
For specifics on manipulating the ElementTree, see
151162
[Working with the ElementTree][] below.

0 commit comments

Comments
 (0)