I’m trying to add anchors to all h2’s in my html, using python. This code will add those anchors, but I need to fill the name of the anchors too.
Any idea if the name can be the number of the match in the loop or a slugified version of the text between the h2 tags?
Here’s the code so far:
regex = '(?P<name><h2>.*?</h2>)'
text = re.sub(regex, "<a name=''/>"+r"\g<name>", text)
You can take advantage of the fact that the second argument to
re.subcan be a function to do pretty much anything you’d like. Here’s an example that will slugify the text inside the<h2>element:(That
slugifyfunction could obviously use some work.)You could also implement a counter with a version of
anchorizethat used a global counter or, better yet, a class that kept track of its own counter and implemented the special__call__method.