Django middleware to remove links to the current page
import re | |
class RemoveSelfLinks: | |
''' | |
Simple middleware, to remove links to the current page. | |
This middleware search all these links using regular expression | |
and replaces them with <span> elments. <span element must have | |
same attributes as <a> element except of 'href'. | |
''' | |
def process_response(self, request, response): | |
if response.status_code == 200: | |
link = request.META['PATH_INFO'] | |
response.content = re.sub(r'<a([^>]+)href="%s"([^>]*)>(.*?(?!</a>)[^<]*)</a>' % link, | |
r'<span \1 \2>\3</span>', | |
response.content) | |
return response |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment