import re from django.utils.safestring import mark_safe from django.utils.html import conditional_escape from django.template import Library register = Library() @register.filter() def obfuscate(email, linktext=None, autoescape=None): """ Given a string representing an email address, returns a mailto link with rot13 JavaScript obfuscation. Accepts an optional argument to use as the link text; otherwise uses the email address itself. """ if autoescape: esc = conditional_escape else: esc = lambda x: x email = re.sub('@','\\\\100', re.sub('\.', '\\\\056', \ esc(email))).encode('rot13') if linktext: linktext = esc(linktext).encode('rot13') else: linktext = email rotten_link = """""" % (email, linktext) return mark_safe(rotten_link) obfuscate.needs_autoescape = True