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

Top down iterator with mutable state #17

Open
boisgera opened this issue May 19, 2020 · 2 comments
Open

Top down iterator with mutable state #17

boisgera opened this issue May 19, 2020 · 2 comments
Assignees

Comments

@boisgera
Copy link
Owner

State being the path handled by iter(..., path=True) ? The aim would be to be able to iterate the tree, change things and then adjust what should be iterated after the changes.

@boisgera boisgera self-assigned this May 19, 2020
@TakodaS
Copy link

TakodaS commented Mar 19, 2021

Just wondering if this is related to a problem I am having. I want to delete all citations in a latex document and use

for elt, path in pandoc.iter(doc, path=True):
        if isinstance(elt, Cite): 
              parent, index = path[-1]
              del parent[index]

Is this the correct way to delete elements by type? The script doesn't delete all the citations in a document, as it always seems to miss a few. I think deleting elements while passing through a list isn't a good idea. Maybe it would be better to get a collection of elements first then delete them all at the same time?

@boisgera
Copy link
Owner Author

Hi @TakodaS!

I think deleting elements while passing through a list isn't a good idea.

Exactly. That's the same issue for Python lists and Pandoc documents.

Maybe it would be better to get a collection of elements first then delete them all at the same time?

Yes, you can collect the location of every citation in the document

cites = [
    path[-1]
    for elt, path in pandoc.iter(doc, path=True)
    if isinstance(elt, Cite)
]

then delete them in a second pass in reverse order (otherwise you may invalidate the indices in the remaining locations !)

for parent, index in reversed(cites):
    del parent[index]

Does it work for you?

Cheers,

SB

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

2 participants