Based on svetlyak's nofollow filter. This one processes only external URL's. Links with internal URL's will be returned unmodified.
There's one gotcha; I preferred to have false positives instead of false negatives. So, it will nofollow href="some/relative/path/"
for example. If you must do relative paths do it like this:
<a href="./some/relative/path">link text</a>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | import re
from django import template
from django.utils.safestring import mark_safe
NOFOLLOW_RE = re.compile(u'<a (?![^>]*rel=["\']nofollow[\'"])' \
u'(?![^>]*href=["\']\.{0,2}/[^/])',
re.UNICODE|re.IGNORECASE)
register = template.Library()
def nofollow(content):
return mark_safe(re.sub(NOFOLLOW_RE, u'<a rel="nofollow" ', content))
register.filter(nofollow)
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 3 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
- Help text hyperlinks by sa2812 1 year, 7 months ago
Comments
fwiw that's completely broken, the scheme name (
http:
) is optional, it's perfectly legal for a URL to start with//
which means "go to this (potentially external) URL using the current scheme".Example: this goes to wikipedia, but there's no http in the url (you might have to check the source to see it though)
#
@masklinn: Thanks for the input. I have updated the regex, check the description. I'd like to know what you think about this variation.
#
Please login first before commenting.