encode_mailto

 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 }}
    
    {{ "j@j.com"|encode_mailto:"j" }} Becomes something like:
    <a href="&#x6d;&#x61;&#x69;&#x6c;&#x74;&#111;&#x3a;&#106;&#x40;&#106;\
    &#46;&#99;&#x6f;&#x6d;">&#x6a;</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

  1. Markdown and Syntax Highlighting in Django by blinks 7 years, 1 month ago
  2. typygmentdown by ubernostrum 6 years, 8 months ago
  3. Revisiting Pygments and Markdown by djypsy 6 years, 8 months ago
  4. Decode HTML Template Tag by megamark16 2 years, 1 month ago
  5. render_markup filter, specify the markup filter as a string by exogen 7 years ago

Comments

(Forgotten your password?)