<commit_before>import base64
import binascii
from urllib import urlencode

from Crypto.Cipher import AES
from django.template.loader import render_to_string
from django.utils.safestring import mark_safe
from django.conf import settings
from django import template

register = template.Library()

def pad_string (str, block_size):
    numpad = block_size - (len (str) % block_size)
    return str + numpad * chr (numpad)

def enc_string(str):
    key = binascii.unhexlify(settings.MAILHIDE_PRIV_KEY)
    mode = AES.MODE_CBC
    iv = '\000' * 16
    obj = AES.new(key, mode, iv)
    return obj.encrypt(str)


@register.filter()
def mailhide(value):

    if value is None or not "@" in value:
        return

    args = {}
    padded_value = pad_string(value, 16)
    encrypted_value = enc_string(padded_value)

    args['k'] = settings.MAILHIDE_PUB_KEY
    args['c'] = base64.urlsafe_b64encode(encrypted_value)


    email = value.split("@")[0]

    if len(email) >= 6:
        prefix = email[0:3]
    elif len(email) >= 4:
        prefix = email[0]
    else:
        prefix=''

    href = "http://mailhide.recaptcha.net/d?" + urlencode(args)

    ctx = {}
    ctx['domain'] = value.split("@")[1]
    ctx['prefix'] = prefix
    ctx['target_url'] = href

    return mark_safe(
        render_to_string("mailhide/obfuscated_email.html", ctx)
    )<commit_msg>Added support for custom templates<commit_after>import base64
import binascii
from urllib import urlencode

from Crypto.Cipher import AES
from django.template.loader import render_to_string
from django.utils.safestring import mark_safe
from django.conf import settings
from django import template

register = template.Library()

def pad_string (str, block_size):
    numpad = block_size - (len (str) % block_size)
    return str + numpad * chr (numpad)

def enc_string(str):
    key = binascii.unhexlify(settings.MAILHIDE_PRIV_KEY)
    mode = AES.MODE_CBC
    iv = '\000' * 16
    obj = AES.new(key, mode, iv)
    return obj.encrypt(str)


@register.filter()
def mailhide(value):

    if value is None or not "@" in value:
        return

    args = {}
    padded_value = pad_string(value, 16)
    encrypted_value = enc_string(padded_value)

    args['k'] = settings.MAILHIDE_PUB_KEY
    args['c'] = base64.urlsafe_b64encode(encrypted_value)


    email = value.split("@")[0]

    if len(email) >= 6:
        prefix = email[0:3]
    elif len(email) >= 4:
        prefix = email[0]
    else:
        prefix=''

    href = "http://mailhide.recaptcha.net/d?" + urlencode(args)

    ctx = {}
    ctx['domain'] = value.split("@")[1]
    ctx['prefix'] = prefix
    ctx['target_url'] = href

    return mark_safe(
        render_to_string(("mailhide/custom_obfuscated_email.html",
                          "mailhide/obfuscated_email.html"), ctx)
    )
{'fraction_non_alphanumeric': 0.12035714285714286, 'fraction_numerical': 0.014285714285714285, 'mean_word_length': 3.205705705705706, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2726648', 'n_tokens_mistral': 1000, 'n_tokens_neox': 950, 'n_words': 223}