Login

nofollow filter

Author:
svetlyak
Posted:
July 13, 2007
Language:
Python
Version:
.96
Tags:
filter
Score:
7 (after 9 ratings)

This filter add extra attribute rel="nofollow" to any "<a ..." element in the value, which does not contain it already. I use this to filter comments text in my blog.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
from django.template import Library
import re

register = Library()

r_nofollow = re.compile('<a (?![^>]*nofollow)')
s_nofollow = '<a rel="nofollow" '

def nofollow(value):
    return r_nofollow.sub(s_nofollow, value)

register.filter(nofollow)

More like this

  1. hide emails with PIL - template filter by dekomote 6 years, 10 months ago
  2. Convert numbers in USA notation to brazilian notation by eOliva 7 years, 10 months ago
  3. Frequently used tags/filters for Jinja2 by mathwizard 7 years, 11 months ago
  4. A simple rest template filter by marinho 8 years, 9 months ago
  5. Compact list_filter with counter by fab10m 5 years, 6 months ago

Comments

polarbear (on July 24, 2007):
<p>You can use also jQuery like:</p> <p>$(".comment a").attr({rel: "nofollow"});</p> <p>to add rel="nofollow" attribute to all a within class .comment</p>

#

luckystarr (on September 7, 2007):
<p>@polarbear</p> <p>This would not work. I doubt that Google interprets JavaScript to check if it may or may not contribute the current sites pagerank to a link.</p>

#

muffinresearch (on July 3, 2008):
<p>One small issue with this is that the regex doesn't match when you just add the text "nofollow" somewhere in one of the links being tested e.g: an href with a fragment-identifier like so: "blah.com#nofollow"</p> <p>The following regex prevents bypassing the filter in this way</p> <pre>re.compile('<a (?![^>]*rel=["\']nofollow[\'"])') </pre>

#

muhuk (on April 9, 2009):
<p>building on muffinresearch's regex:</p> <pre><a (?![^>]*rel=["\']nofollow[\'"])(?=[^>]*href=["\']http) </pre> <p>This matches only internal links. (hopefully)</p>

#

Please login first before commenting.