Eclectic Media Git eclecticmedia.space / 899e28c utilities / __init__.py
899e28c

Tree @899e28c (Download .tar.gz)

__init__.py @899e28craw · 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 = 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)