From 2cdeb70eadf0c6cf18049b5b2e4f66a1309a6c96 Mon Sep 17 00:00:00 2001 From: Matthijs Kooijman Date: Sun, 10 Feb 2008 16:43:55 +0100 Subject: [PATCH] * Add an encode_email tag library used to somewhat encode email addresses. Code taken verbatim from http://www.djangosnippets.org/snippets/216/. --- tools/templatetags/encode_email.py | 52 ++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 tools/templatetags/encode_email.py 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) -- 2.30.2