tinkerer / tinkerer / ext /


    Email obfuscation role
    The obfuscation code was taken from
    Email obfuscation role for Sphinx:

    :copyright: Copyright 2011 by Kevin Teague
    :copyright: Copyright 2012 by Christian Jann
    :license: FreeBSD. Parts of this file are licensed under BSD license. See
    LICENSE file.
from docutils import nodes
import re

    maketrans = ''.maketrans
except AttributeError:
    # fallback for Python 2
    from string import maketrans

rot_13_trans = maketrans(

def rot_13_encrypt(line):
    """Rotate 13 encryption.

    line = line.translate(rot_13_trans)
    line = re.sub('(?=[\\"])', r'\\', line)
    line = re.sub('\n', r'\n', line)
    line = re.sub('@', r'\\100', line)
    line = re.sub('\.', r'\\056', line)
    line = re.sub('/', r'\\057', line)
    return line

def js_obfuscated_text(text):
    ROT 13 encryption with embedded in Javascript code to decrypt
    in the browser.
    return """<noscript>(Javascript must be enabled to see this e-mail address)</noscript>
              <script type="text/javascript">document.write(
                return String.fromCharCode(
                </script>""" % rot_13_encrypt(text)

def js_obfuscated_mailto(email, displayname=None):
    ROT 13 encryption within an Anchor tag w/ a mailto: attribute
    if not displayname:
        displayname = email
    return js_obfuscated_text("""<a href="mailto:%s">%s</a>""" % (
        email, displayname

def email_role(typ, rawtext, text, lineno, inliner, options={}, content=[]):
    Role to obfuscate e-mail addresses.
        # needed in Python 2
        text = text.decode("utf-8").encode("utf-8")
    except AttributeError:
    # Handle addresses of the form "Name <>"
    if '<' in text and '>' in text:
        name, email = text.split('<')
        email = email.split('>')[0]
    elif '(' in text and ')' in text:
        name, email = text.split('(')
        email = email.split(')')[0]
        name = text
        email = name

    obfuscated = js_obfuscated_mailto(email, displayname=name)
    node = nodes.raw('', obfuscated, format="html")
    return [node], []