updates - categories

This commit is contained in:
SG
2025-06-11 22:35:20 +03:00
parent 2132cfd48d
commit 3f832a1af5
5 changed files with 41 additions and 60 deletions

View File

@@ -10,7 +10,8 @@ from config import Config
from jinja_env import env, content_item_template, index_template
class ContentItem:
def render_content(self):
def render_content(self, categories):
logger.debug(f"categ: {categories}")
logger.debug(f"Rendering {self.source_filename} to {self.target_filename}")
try:
logger.debug(f"Title: {self.title}")
@@ -26,7 +27,7 @@ class ContentItem:
if self.js_file and self.js_file.exists():
shutil.copyfile(self.js_file, Path(Config.OUTPUT_DIR) / self.js_file)
with self.target_filename.open("w", encoding="utf-8") as f:
f.write(content_item_template.render(content_item = self, page_title = self.title))
f.write(content_item_template.render(content_item = self, page_title = self.title, parent_path = '../', categories = categories))
except Exception as e:
logger.error(e)
@@ -103,6 +104,8 @@ class Site:
# Create static/about.md
def get_content_items(self):
logger.debug("Getting content items")
self.get_content_items = []
logger.debug(f"Scanning {Path(Config.CONTENT_DIR)}")
for md_file in Path(Config.CONTENT_DIR).glob("*.md"):
logger.debug(f"Loading {md_file}")
@@ -113,9 +116,9 @@ class Site:
def map_categories(self):
for content_item in self.content_items:
for category in content_item.data.get("categories"):
print(f"!!!!!!! {category}")
if not category == "default":
self.categories[category].append(content_item.slug)
print(self.categories)
def build(self):
# Recreate the output dir if needed
@@ -133,32 +136,33 @@ class Site:
if self.static_dir.exists():
shutil.copytree(self.static_dir, self.output_dir / self.static_dir, dirs_exist_ok=True)
# Parse the content files
logger.debug("Getting content items")
# Get 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()
# Parse the content files
logger.debug(f"Content items: {self.content_items}")
for content_item in self.content_items:
content_item.render_content(categories = 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=self.content_items, categories = self.categories))
# Render the categories indices
for category in self.categories:
print(F"CREATING CATEGORY {category}.html")
category_index = Path(f"{self.output_dir}/categories/{category}.html")
category_items = [i for i in self.content_items if category in i.data.get("categories")]
with (category_index).open(mode="w", encoding="utf-8") as f:
f.write(index_template.render(page_title=category, content_items=category_items, categories = self.categories))
f.write(index_template.render(page_title=category, content_items=category_items, categories = self.categories, parent_path = '../'))
# Render the about file
about_content = ContentItem(Path('static/about.md'))
about_content.parse_content()
about_content.render_content()
about_content.render_content(categories = self.categories)
logger.info(f"Created {len(self.content_items)} content items.")