balsamine.www
clone your own copy | download snapshot

Snapshots | iceberg

Inside this repository

__init__.py
text/x-python

Download raw (1.9 KB)

import shlex
import subprocess

from pelican import signals, contents
from pelican.readers import METADATA_PROCESSORS
from pelican.utils import get_date

from bs4 import BeautifulSoup

import random

import re
import datetime

date_range_patt = r'(\d+([/\-\.\s])\d+\2\d+)(?:\s*(?:→|au|->)\s*)(\d+\2\d+\2\d+)'

def parse_date_line (line):
    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) }

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']}]

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

def process_css(pelican):
    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)

    call_params = shlex.split(cmd)
    subprocess.call(call_params)


def register():
    signals.initialized.connect(add_metadata_processors)
    signals.article_generator_context.connect(parse_dates)
    signals.finalized.connect(process_css)