File tree Expand file tree Collapse file tree 1 file changed +14
-3
lines changed
Expand file tree Collapse file tree 1 file changed +14
-3
lines changed Original file line number Diff line number Diff line change @@ -136,16 +136,27 @@ the tree and runs the InlinePatterns on the text of each Element in the tree.
136136
137137A Treeprocessor should inherit from ``markdown.treeprocessors.Treeprocessor``,
138138over-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
141142A 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
150161For specifics on manipulating the ElementTree, see
151162[Working with the ElementTree][] below.
You can’t perform that action at this time.
0 commit comments