__init__.py 3.42 KB
Newer Older
1 2 3
import shlex
import subprocess

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

alexandre's avatar
alexandre committed
8
from bs4 import BeautifulSoup
alexandre's avatar
alexandre committed
9 10 11

import random

gijs's avatar
gijs committed
12 13 14
import re
import datetime

gijs's avatar
gijs committed
15 16 17 18
time_chunk_patt = r'(\d+)(?:\:|h)(\d+)?'

# Recognize time part in the description
# \2 (group 2) is the separator, as to have the same separator in the match
gijs's avatar
gijs committed
19
time_patt = r'(\d+(:|h)(?:\d+)?)(?:\s*(?:→|au|->|à)\s*)?(\d+\2(?:\d+)?)?'
gijs's avatar
gijs committed
20 21

# \2 (group 2) is the separator, as to have the same separator in the match
gijs's avatar
gijs committed
22 23
date_range_patt = r'(\d+([/\-\.\s])\d+\2\d+)(?:\s*(?:→|au|->)\s*)(\d+\2\d+\2\d+)'

gijs's avatar
gijs committed
24
def parse_time_chunk (chunk):
gijs's avatar
gijs committed
25
    m = re.match(time_chunk_patt, chunk)
gijs's avatar
gijs committed
26 27 28
    hour = int(m.group(1))
    minutes = int(m.group(2) if m.group(2) else 0)
    return datetime.time(hour, minutes)
gijs's avatar
gijs committed
29 30

def parse_time_part (line):
gijs's avatar
gijs committed
31
    m = re.search(time_patt, line)
gijs's avatar
gijs committed
32 33 34 35

    if m:
        if m.group(3):
            return {
gijs's avatar
gijs committed
36 37
                'start': parse_time_chunk(m.group(1)),
                'end': parse_time_chunk(m.group(3))
gijs's avatar
gijs committed
38 39 40
            }
        else:
            return {
gijs's avatar
gijs committed
41
                'start': parse_time_chunk(m.group(1))
gijs's avatar
gijs committed
42 43
            }
    else:
gijs's avatar
gijs committed
44 45 46 47 48 49
        return None

def parse_time (time):
    parsed = parse_time_part(time)

    return parsed if parsed else time
gijs's avatar
gijs committed
50 51

def parse_date_part (line):
gijs's avatar
gijs committed
52 53 54 55 56 57 58 59 60
    m = re.match(date_range_patt, line)
    if m:
        return {
            'date': get_date(m.group(1)),
            'end_date': get_date(m.group(3))
        }
    else:
        return { 'date': get_date(line) }

gijs's avatar
gijs committed
61 62 63 64 65
def parse_date_line (line):
    date = parse_date_part(line)
    time = parse_time_part(line)

    if time:
gijs's avatar
gijs committed
66 67 68
        date['time'] = time

    return date
gijs's avatar
gijs committed
69

gijs's avatar
gijs committed
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
"""
    For future if there is a different time per event?

    Date formatting

    'dates': [
        { 
            'date': date,
            'time': (time,) | (time,time)
        },
        { 
            'start_date': date,
            'end_date': date,
            'time': (time,) | (time,time)
        },
    ]
"""
gijs's avatar
gijs committed
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
def parse_dates (generator, metadata):
    if 'dates' in metadata:
        dates = metadata['dates']
        if isinstance(metadata['dates'], list):
            metadata['dates'] = [parse_date_line(show) for show in metadata['dates']]
        else:
            metadata['dates'] = [parse_date_line(metadata['dates'])]
        metadata['end_date'] = metadata['dates'][-1]['end_date'] if ('end_date' in metadata['dates'][-1]) else metadata['dates'][-1]['date']

        if not 'date' in metadata:
            metadata['date'] = metadata['dates'][0]['date']
    elif 'end_date' in metadata:
        metadata['dates'] = [{'date': metadata['date'], 'end_date': metadata['end_date']}]
    else:
        metadata['dates'] = [{'date': metadata['date']}]
102 103

def add_metadata_processors(arg):
104
    METADATA_PROCESSORS['end_date'] = METADATA_PROCESSORS.get('end_date', METADATA_PROCESSORS.get('date'))
105
    METADATA_PROCESSORS['end_date'] = lambda x, y: get_date(x.replace('_', ' '))
gijs's avatar
gijs committed
106
    METADATA_PROCESSORS['time'] = lambda x, y: parse_time(x)
107

colm's avatar
colm committed
108
def process_css(pelican):
109 110 111
    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's avatar
colm committed
112 113
    call_params = shlex.split(cmd)
    subprocess.call(call_params)
114

115

116 117
def register():
    signals.initialized.connect(add_metadata_processors)
gijs's avatar
gijs committed
118 119
    signals.article_generator_context.connect(parse_dates)
    signals.finalized.connect(process_css)