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
- Resolve URLs to view name and args/kwargs by fahhem 1 year, 6 months ago
- better paginator template tag by amitu 3 years, 7 months ago
- A few jinja2 filters like django ones by brondsem 3 years, 2 months ago
- Url filter middleware by limodou 5 years, 2 months ago
- SuperChoices by willhardy 3 years, 6 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.
#