updates
This commit is contained in:
40
classes.py
40
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.")
|
||||
|
||||
Reference in New Issue
Block a user