from app import * import yaml import functools from pathlib import Path from datetime import datetime, date file_path = Path(__file__).parent NUM_RECENT_BLOGS = 20 def full_page(): secs = Sections(["Recent Blogs"], [Div(*[blog_preview(blog_id) for blog_id in sorted_blogs[:NUM_RECENT_BLOGS]])]) return BstPage(0, '', *secs) def blog_preview(blog_id): details = all_blogs[blog_id] return Div( A(H3(details[0]["title"]), href=f"/blog/{blog_id}"), P(details[0].get("date_published", "")), P(details[0].get("desc", "")+"...") ) @functools.lru_cache() def get_blogs(): blogs = (file_path / "blogs").rglob("*.md") blog_dict = {} for blog in blogs: with open(blog, 'r') as f: id_ = blog.stem text = f.read() if "---" not in text: continue metadata, markdown = text.split("---", 2)[1:] metadata = yaml.safe_load(metadata) metadata["id"] = id_ blog_dict[id_] = (metadata, markdown) blog_dict = {k:v for k, v in blog_dict.items() if v[0].get("date_published", "") != ""} sorted_blogs = [x[0]["id"] for x in sorted(blog_dict.values(), key=lambda x: x[0].get("date_published"), reverse=True)] return blog_dict, sorted_blogs all_blogs, sorted_blogs = get_blogs() def single_blog(blog_id): """ Return a single blog post. """ details = all_blogs[blog_id] return BstPage(0, details[0]["title"], Markdown(details[1]))