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

Possible way to delete a node? #268

Closed
RheingoldRiver opened this issue Apr 24, 2021 · 5 comments
Closed

Possible way to delete a node? #268

RheingoldRiver opened this issue Apr 24, 2021 · 5 comments

Comments

@RheingoldRiver
Copy link

Hi, one thing that comes up a lot is wanting to delete a template or a section, etc, completely from a page. What I would like is a method called .delete() that replaces the entire contents of the node with an empty string basically. Does some functionality for this already exist that I'm missing? Or would this be possible to add (either to the base node class, or to Template) ?

Right now what I do is remove every param, then change the name to @@@DELETE@@@ or something, then when I am done I replace all occurrences of {{@@@DELETE@@@}} in the entire page with an empty string, but this is pretty cumbersome.

@earwig
Copy link
Owner

earwig commented Apr 24, 2021

code.remove(node)

E.g.:

>>> code = mwparserfromhell.parse("foo {{bar}} baz")
>>> node = code.filter_templates()[0]
>>> code.remove(node)
>>> code
'foo  baz'

@earwig
Copy link
Owner

earwig commented Apr 24, 2021

BTW: Nodes don't know what they're contained in, so adding the method to the node itself isn't currently possible. This will change at some point in the future, then we could add a method to the node itself.

@RheingoldRiver
Copy link
Author

ahhh perfect, thank you!

@RheingoldRiver
Copy link
Author

Actually I tested it and it seems like code.remove(node) doesn't take with it, its own line if it was the only thing on the line. Would that be possible to do? (I can make a separate issue for that since that's a different feature request, if you think it's reasonable)

@earwig
Copy link
Owner

earwig commented Apr 25, 2021

It's related to #55, #265, #266... there's a general problem here that the parser always treats whitespace as part of a text node and will never strip it for you. I think it's a reasonable feature request.

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