In my models I often use text fields that are intended to contain large pieces of textile-formatted input. I'd like to automatically obfuscate any email addresses that are entered into these text fields, so that when they're printed in a template they're not visible to spiders.
Is there a smart way to do this?
Update:
Based on lazerscience's answer below, this was the code i ended up using. I named the file encode_mailto.py, and put it in a templatetags directory, inside a 'utilities' type app that i install into most of my django projects.
import re
import random
from django.utils.safestring import mark_safe
from django import template
register = template.Library()
email_link_pat = re.compile(r'<a\s+href=("|\')?mailto:[^>]+>[^<]*</a>')
email_pat = re.compile(r'\b[-.\w]+@[-.\w]+\.[a-z]{2,4}\b')
def get_script(m):
code_list = []
for c in m.group(0):
d = ord(c)
x = random.randint(0, d)
code_list.append("%d+%d" % (x, d-x))
return '<script type="text/javascript">document.write(String.fromCharCode(%s))</script>' % \
",".join(code_list)
def encode_mailto(text):
text = email_link_pat.sub(get_script, text)
text = email_pat.sub(get_script, text)
return mark_safe(text)
register.filter('encode_mailto', encode_mailto)</pre>
Then use it in templates as follows:
{% load encode_mailto %}
{{"A bunch of text with an email address [email protected]"|encode_mailto }}