diff --git a/classes.py b/classes.py index 52d8dcb..4412497 100644 --- a/classes.py +++ b/classes.py @@ -1,12 +1,13 @@ from datetime import datetime import os import shutil +from collections import defaultdict import markdown import frontmatter from pathlib import Path from logger import logger from config import Config -from jinja_env import env, content_item_template, index_template +from jinja_env import env, content_item_template, index_template, categories_index_template class ContentItem: def render_content(self): @@ -83,6 +84,8 @@ class Site: self.css_dir = Path(f"{Config.STATIC_DIR}/css") self.js_dir = Path(f"{Config.STATIC_DIR}/js") self.output_dir = Path(Config.OUTPUT_DIR) + self.content_items = [] + self.categories = defaultdict(list) def init_site(self): @@ -99,6 +102,19 @@ class Site: f.write(template_content) # Create static/about.md + def get_content_items(self): + logger.debug(f"Scanning {Path(Config.CONTENT_DIR)}") + for md_file in Path(Config.CONTENT_DIR).glob("*.md"): + logger.debug(f"Loading {md_file}") + content_item = ContentItem(md_file) + content_item.parse_content() + self.content_items.append(content_item) + + def map_categories(self): + for content_item in self.content_items: + for category in content_item.data.get("categories"): + self.categories[category].append(content_item.slug) + print(self.categories) def build(self): # Recreate the output dir if needed @@ -117,21 +133,27 @@ class Site: shutil.copytree(self.static_dir, self.output_dir / self.static_dir, dirs_exist_ok=True) # Parse the content files - content_items = [] - for md_file in self.content_dir.glob("*.md"): - current_content_item = ContentItem(md_file) - current_content_item.parse_content() - current_content_item.render_content() - content_items.append(current_content_item) + logger.debug("Getting content items") + self.get_content_items() + logger.debug(f"Content items: {self.content_items}") + for content_item in self.content_items: + content_item.render_content() + + # Build categories map + self.map_categories() + + for category in self.categories: + with (self.output_dir / "categories.html").open(mode="w", encoding="utf-8") as f: + f.write(categories_index_template.render(page_title = Config.MAIN_PAGE_TITLE, categories_list = self.categories)) # Render the index file with (self.output_dir / "index.html").open("w", encoding="utf-8") as f: - f.write(index_template.render(page_title = Config.MAIN_PAGE_TITLE, content_items=content_items)) + f.write(index_template.render(page_title = Config.MAIN_PAGE_TITLE, content_items=self.content_items)) # Render the about file about_content = ContentItem(Path('static/about.md')) about_content.parse_content() about_content.render_content() - logger.info(f"Created {len(content_items)} content items.") + logger.info(f"Created {len(self.content_items)} content items.") \ No newline at end of file diff --git a/jinja_env.py b/jinja_env.py index 92d9a26..127b4a5 100644 --- a/jinja_env.py +++ b/jinja_env.py @@ -5,4 +5,5 @@ from config import Config env = Environment(loader=FileSystemLoader(Config.TEMPLATES_DIR)) env.globals['header_image'] = Config.HEADER_IMAGE index_template = env.get_template("index.html") -content_item_template = env.get_template("content_item.html") \ No newline at end of file +content_item_template = env.get_template("content_item.html") +categories_index_template = env.get_template("categories_index.html") \ No newline at end of file diff --git a/static/images/1x1.png.1 b/static/images/1x1.png.1 deleted file mode 100644 index 1914264..0000000 Binary files a/static/images/1x1.png.1 and /dev/null differ diff --git a/templates/categories_index.html b/templates/categories_index.html new file mode 100644 index 0000000..7aacac9 --- /dev/null +++ b/templates/categories_index.html @@ -0,0 +1,16 @@ +{% extends "default.html" %} + +{% block menu_links %} + Home + About +{% endblock %} + +{% block content %} +
+
+ {% for category in categories_list %} + {{ category }} + {% endfor %} +
+ +{% endblock %} \ No newline at end of file