Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid side-effects in walk #228

Open
lewer opened this issue Feb 1, 2023 · 0 comments
Open

Avoid side-effects in walk #228

lewer opened this issue Feb 1, 2023 · 0 comments

Comments

@lewer
Copy link
Contributor

lewer commented Feb 1, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant