sergiocorreia/panflute

Avoid side-effects in walk

lewer opened this issue · 0 comments

lewer commented

Hi,
the documentation of pandoc says about action functions given to walk:

If they return None, the document will keep the same element as before (although it might have been modified).

but doesn't being able to modify the element encourage writing filters in a bad way?

For instance in this issue #96 (comment), someone was trying to modify the parent from the child. This is also what's done here:

# Append them to Emph's parent (after the emph)
for i, item in enumerate(elem.content, elem.index + 1):
elem.parent.content.insert(i, item)

This is probably bad practice to modify the tree while you're walking into it.

I ask because the first filters I wrote with panflute were heavily modifying the element. Now I have to rewrite these filters so that instead it returns a new element.

Should panflute not prevent the modification of the element to prevent side-effects? I don't know how; the only way I'm thinking about would be to use a copy of the element instead of the element when calling walk recursively, but it would make the code slower.