- Author:
- santuri
- Posted:
- May 6, 2007
- Language:
- Python
- Version:
- .96
- Tags:
- filter email encode
- Score:
- 4 (after 6 ratings)
This is a django filter which will create an html mailto link when you use the syntax:
{{ "[email protected]"|encode_mailto:"Name" }}
Results in:
<a href="mailto:[email protected]">Name</a>
Except everything is encoded as html digits.
The encoding is a string of html hex and decimal entities generated randomly.
If you simply want a string encoding use:
{{ "name"|encode_string }}
This was inspired by John Gruber's Markdown project
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 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 }}
{{ "[email protected]"|encode_mailto:"j" }} Becomes something like:
<a href="mailto:j@j\
.com">j</a>
"""
address = 'mailto:%s' % value
address = encode_string(address)
name = encode_string(arg)
tag = "<a href=\"%s\">%s</a>" % (address, name)
return tag
register.filter("encode_mailto", encode_mailto)
|
More like this
- Serialize a model instance by chriswedgwood 1 week, 1 day ago
- Automatically setup raw_id_fields ForeignKey & OneToOneField by agusmakmun 9 months, 1 week ago
- Crispy Form by sourabhsinha396 10 months ago
- ReadOnlySelect by mkoistinen 10 months, 2 weeks ago
- Verify events sent to your webhook endpoints by santos22 11 months, 2 weeks ago
Comments
Please login first before commenting.