From: Matthijs Kooijman Date: Sun, 10 Feb 2008 15:43:55 +0000 (+0100) Subject: * Add an encode_email tag library used to somewhat encode email addresses. Code... X-Git-Url: https://git.stderr.nl/gitweb?a=commitdiff_plain;h=2cdeb70eadf0c6cf18049b5b2e4f66a1309a6c96;p=matthijs%2Fprojects%2Fxerxes.git * Add an encode_email tag library used to somewhat encode email addresses. Code taken verbatim from djangosnippets.org/snippets/216/. --- diff --git a/tools/templatetags/encode_email.py b/tools/templatetags/encode_email.py new file mode 100644 index 0000000..6a737d1 --- /dev/null +++ b/tools/templatetags/encode_email.py @@ -0,0 +1,52 @@ +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)