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.")

View File

@@ -6,4 +6,3 @@ 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")
categories_index_template = env.get_template("categories_index.html")

View File

@@ -1,16 +0,0 @@
{% extends "default.html" %}
{% block menu_links %}
<a href="index.html">Home</a>
<a href="static/about.html">About</a>
{% endblock %}
{% block content %}
<div class="container">
<div class="row"></div>
{% for category in categories_list %}
<a href="{{category}}.html">{{ category }}</a>
{% endfor %}
</div>
</div>
{% endblock %}

View File

@@ -74,9 +74,16 @@
</div>
</div>
<div class="top_menu d-flex justify-content-end" style="margin-bottom: 20px;">
{% block menu_links %}
{% endblock %}
<a href="{{ parent_path }}index.html">Home</a>
<a id="categories_toggle" href="#">Categories</a>
<a href="{{ parent_path }}static/about.html">About</a>
</div>
<div id="categories_container" class="container">
<div id="categories_menu" class="row justify-content-around mx-auto" style="visibility: hidden">
{% for category in categories %}
<a href="{{ parent_path }}categories/{{ category }}.html" class="mx-2">{{ category }}</a>
{% endfor %}
</div>
</div>
<div id="content">{% block content %}{% endblock %}</div>
@@ -92,4 +99,16 @@
</div>
<div class="row small text-muted justify-content-end"><p class="mx-4">Page created with <a href="">microgen</a></p></div>
</div>
<script defer>
document.getElementById("categories_toggle").onclick = function() {
categories_menu_item = document.getElementById("categories_menu")
console.log(categories_menu_item.style.visibility)
if (categories_menu_item.style.visibility == "hidden") {
categories_menu_item.style.visibility = "visible"
}
else {
categories_menu_item.style.visibility = "hidden"
}
}
</script>
</body>

View File

@@ -5,20 +5,10 @@
{% endblock %}
{% block menu_links %}
<a href="index.html">Home</a>
<a id="categories_toggle" href="#">Categories</a>
<a href="static/about.html">About</a>
{% endblock %}
{% block content %}
<div class="container">
<div id="categories_menu" class="row justify-content-around mx-auto" style="visibility: hidden">
{% for category in categories %}
<a href="categories/{{ category }}.html" class="mx-2">{{ category }}</a>
{% endfor %}
</div>
</div>
<div class="container-fluid content-row mx-auto">
<div class="row justify-content-start mx-auto">
{% for content_item in content_items %}
@@ -43,18 +33,3 @@
{% endblock %}
{% block footer_includes %}
<script defer>
document.getElementById("categories_toggle").onclick = function() {
categories_menu_item = document.getElementById("categories_menu")
console.log(categories_menu_item.style.visibility)
if (categories_menu_item.style.visibility == "hidden") {
categories_menu_item.style.visibility = "visible"
}
else {
categories_menu_item.style.visibility = "hidden"
}
}
</script>
{% endblock %}