I've got some code which is essentially:
wikicode = mwp.parse(self.page.get()) for node in wikicode.filter_templates(recursive=False, matches=title): wikicode.remove(node) self.page.text = str(wikicode) self.page.save()
which works, but it leaves an extra blank line behind where the template used to be. This is intended to be run on [[:en:Template talk:Did you know/Approved]], i.e. one template per line.
What's the best way to get rid of the blank lines? I'm trying to avoid just running a regex replacement on the raw text because that's fragile, but maybe theres really no good alternative here?
One could use a plain filter() to iterate over all the nodes, and test each, to see if it is the template that you want to delete. Having detected the template you want to delete, set a flag, and delete following text nodes, until the next template node is detected.
On Tue, 21 Mar 2023, 18:28 Roy Smith, roy@panix.com wrote:
I've got some code which is essentially:
wikicode = mwp.parse(self.page.get()) for node in wikicode.filter_templates(recursive=False,
matches=title):
wikicode.remove(node) self.page.text = str(wikicode) self.page.save()
which works, but it leaves an extra blank line behind where the template used to be. This is intended to be run on [[:en:Template talk:Did you know/Approved]], i.e. one template per line.
What's the best way to get rid of the blank lines? I'm trying to avoid just running a regex replacement on the raw text because that's fragile, but maybe theres really no good alternative here? _______________________________________________ pywikibot mailing list -- pywikibot@lists.wikimedia.org Public archives at https://lists.wikimedia.org/hyperkitty/list/pywikibot@lists.wikimedia.org/me... To unsubscribe send an email to pywikibot-leave@lists.wikimedia.org
Today was a beautiful early spring day here in New York (my apologies to those of you in the "winter is approaching" hemisphere) so I decided to move away from the keyboard and went out for a bike ride. Along the way, I pretty much came to the same conclusion, and now that I'm back, it's nice to see confirmation that I was thinking in the right direction. Thanks!
On Mar 21, 2023, at 5:20 PM, William Avery willm.avery@gmail.com wrote:
One could use a plain filter() to iterate over all the nodes, and test each, to see if it is the template that you want to delete. Having detected the template you want to delete, set a flag, and delete following text nodes, until the next template node is detected.
On Tue, 21 Mar 2023, 18:28 Roy Smith, <roy@panix.com mailto:roy@panix.com> wrote: I've got some code which is essentially:
wikicode = mwp.parse(self.page.get()) for node in wikicode.filter_templates(recursive=False, matches=title): wikicode.remove(node) self.page.text = str(wikicode) self.page.save()
which works, but it leaves an extra blank line behind where the template used to be. This is intended to be run on [[:en:Template talk:Did you know/Approved]], i.e. one template per line.
What's the best way to get rid of the blank lines? I'm trying to avoid just running a regex replacement on the raw text because that's fragile, but maybe theres really no good alternative here? _______________________________________________ pywikibot mailing list -- pywikibot@lists.wikimedia.org mailto:pywikibot@lists.wikimedia.org Public archives at https://lists.wikimedia.org/hyperkitty/list/pywikibot@lists.wikimedia.org/me... https://lists.wikimedia.org/hyperkitty/list/pywikibot@lists.wikimedia.org/message/SRJVFLJSSFFMAWE6HMNLUH6A2HO23SDX/ To unsubscribe send an email to pywikibot-leave@lists.wikimedia.org mailto:pywikibot-leave@lists.wikimedia.org _______________________________________________ pywikibot mailing list -- pywikibot@lists.wikimedia.org Public archives at https://lists.wikimedia.org/hyperkitty/list/pywikibot@lists.wikimedia.org/me... To unsubscribe send an email to pywikibot-leave@lists.wikimedia.org
Biking is great and sometimes hard to begin when the other option is coding. :-)
My approach would be to import template.py as a module which is able to do the work. Now I see, it has everything in __init__, so not very flexible, and needs a page generator.