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