__init__.py 2.72 KB
1 2 3
import shlex
import subprocess

alexandre committed
4
from pelican import signals, contents
5 6 7
from pelican.readers import METADATA_PROCESSORS
from pelican.utils import get_date

alexandre committed
8 9 10 11
#  import html5lib
#  from html5lib.filters.base import Filter

from bs4 import BeautifulSoup
alexandre committed
12 13 14

import random

15 16

def add_metadata_processors(arg):
17
    METADATA_PROCESSORS['end_date'] = METADATA_PROCESSORS.get('end_date', METADATA_PROCESSORS.get('date'))
18 19 20
    METADATA_PROCESSORS['end_date'] = lambda x, y: get_date(x.replace('_', ' '))


colm committed
21
def process_css(pelican):
22 23 24
    path = str(pelican.settings['OUTPUT_PATH'] + '/theme/css/' + pelican.settings['CSS_FILE'])
    cmd = "./node_modules/.bin/postcss --map --use postcss-cssnext --output {} {}".format(path, path)

colm committed
25 26
    call_params = shlex.split(cmd)
    subprocess.call(call_params)
27

28

alexandre committed
29 30 31 32 33 34 35 36 37 38 39 40 41
def obfuscate(value):
    #  return value[::-1]
    # get args.email and transform char in list of ascii code
    ascii_char_list = list(ord(char) for char in list(value))

    # lambda function to transform ascii to html ascii entity
    # randomly keep ascii in decimal or return ascii hex
    transform = lambda s: random.choice(
                    ('&#' + str(s) + ';', str(hex(s)).replace('0', '&#', 1) + ';'))

    return ''.join(list(map(transform, ascii_char_list)))


alexandre committed
42 43 44 45 46
#  class ObfuscateEmailsFilter(Filter):
#      def __iter__(self):
#          for token in Filter.__iter__(self):
#              if token["type"] == "StartTag" and token['name'] == "a":
#                  href = token["data"][(None, 'href')]
alexandre committed
47

alexandre committed
48 49 50 51
#                  if href.startswith("mailto:"):
#                      obfuscated = obfuscate(href[7:])
#                      href = "mailto:{}".format(obfuscated)
#                      token["data"][(None, 'href')] = href
alexandre committed
52

alexandre committed
53
#              yield token
alexandre committed
54 55 56 57 58 59


def obfuscate_emails(content):
    if isinstance(content, contents.Static):
        return

alexandre committed
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
    #  dom = html5lib.parseFragment(content._content, treebuilder="etree")
    #  walker = html5lib.getTreeWalker("etree")
    #  stream = walker(dom)
    #  stream = ObfuscateEmailsFilter(stream)
    #  s = html5lib.serializer.HTMLSerializer(quote_attr_values="always", omit_optional_tags=False)
    #  content._content = s.render(stream)

    soup = BeautifulSoup(content._content, 'html5lib')

    for link in soup.findAll('a'):
        href = link.attrs.get("href")

        if href and href.startswith("mailto:"):
            print(">>>")
            print(href)
            obfuscated = obfuscate(href[7:])
            href = "mailto:{}".format(obfuscated)
            link["href"] = href
            print(link)

    content._content = str(soup)
alexandre committed
81 82


83 84
def register():
    signals.initialized.connect(add_metadata_processors)
alexandre committed
85
    #  signals.content_object_init.connect(obfuscate_emails)
colm committed
86
    signals.finalized.connect(process_css)