Created
October 16, 2008 08:13
-
-
Save svetlyak40wt/17094 to your computer and use it in GitHub Desktop.
Django middleware to remove links to the current page
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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