import random from django import template """ Randomized string encoding Inspired by John Gruber's Markdown: http://daringfireball.net/projects/markdown/syntax#autolink """ register = template.Library() #@template.stringfilter def encode_string(value): """ Encode a string into it's equivalent html entity. The tag will randomly choose to represent the character as a hex digit or decimal digit. Use {{ obj.name|encode_string }} {{ "person"|encode_string }} Becomes something like: person """ e_string = "" for a in value: type = random.randint(0,1) if type: en = "&#x%x;" % ord(a) else: en = "&#%d;" % ord(a) e_string += en return e_string register.filter("encode_string", encode_string) def encode_mailto(value, arg): """ Encode an e-mail address and its corresponding link name to its equivalent html entities. Use {{ obj.email|encode_mailto:obj.name }} {{ "j@j.com"|encode_mailto:"j" }} Becomes something like: j """ address = 'mailto:%s' % value address = encode_string(address) name = encode_string(arg) tag = "%s" % (address, name) return tag register.filter("encode_mailto", encode_mailto)