Eclectic Media Git archived_em.space_site / master utilities / __init__.py
master

Tree @master (Download .tar.gz)

__init__.py @masterraw · history · blame

import os
from .renderer import render_markdown


def __yield_files():
    for path, subdirs, files in os.walk('content/blog/'):
        for name in reversed(sorted(files, key=lambda name:
                             os.path.getmtime(os.path.join(path, name)))):
            yield name, os.path.join(path, name)


def __process_blog_names():
    for name, path in __yield_files():
        name = name.replace('-', ' ')
        name = name.replace('.md', '')
        name = name.title()

        link = os.path.split(path)[0].replace('content/blog', '')
        link = link[1:] + '/' if len(link) > 0 else link
        link += name.lower().replace(' ', '-')

        yield name, link, path


def __get_blog_text(path):
    text = render_markdown(path)
    for line in text.split('\n'):
        if not line.count('h1') and line.count('<p>'):
            return line.replace('<p>', '').replace('</p>', '')


def sort_blog():
    for name, link, path in __process_blog_names():
        yield name, link, path, os.path.getmtime(path), __get_blog_text(path)