This commit is contained in:
SG
2025-06-18 14:00:00 +03:00
parent 66d3132e53
commit d3b369be46
4 changed files with 25 additions and 12 deletions

View File

@@ -41,6 +41,10 @@ class ContentItem:
if target_file:
self.target_file = Path(target_file)
logger.debug(f"Rendering {self.source_file} to {self.target_file}")
if hasattr(config, "footer"):
footer_data = config.footer
else:
footer_data = ''
try:
if self.image_file and self.image_file.exists():
image_targetfile = Path(f"{config.output_dir}/{config.static_dir}/images/{self.image_file.name}")
@@ -56,7 +60,7 @@ class ContentItem:
logger.debug(f"Copying {self.js_file} to {js_targetfile}")
shutil.copyfile(self.js_file, js_targetfile)
with self.target_file.open("w", encoding="utf-8") as f:
f.write(content_item_template.render(content_item = self, page_title = f"{config.site_name}: {self.title}", parent_path = '../', categories = categories))
f.write(content_item_template.render(content_item = self, page_title = f"{config.site_name}: {self.title}", parent_path = '../', categories = categories, footer_data = footer_data))
except Exception as e:
logger.error(f"Renderer: {e}")
@@ -64,7 +68,7 @@ class ContentItem:
logger.debug(f"Parsing file {self.source_file}")
try:
self.source_file = Path(self.source_file)
self.parent_dir = self.source_file.parent
self.parent_dir = self.source_file.parent # Most likely './content'
self.slug = self.source_file.stem
self.target_file = Path(f"{config.output_dir}/{self.source_file.parent}/{self.source_file.stem}.html")
self.data = frontmatter.load(self.source_file)
@@ -118,6 +122,7 @@ class Site:
self.categories = defaultdict(list)
def init_site(self):
# Exit if current directory not empty
if os.path.isdir('.') and os.listdir('.'):
logger.error("Current directory is not empty.")
sys.exit(1)
@@ -127,7 +132,7 @@ class Site:
for subdir in [self.content_dir]:
os.makedirs(subdir, exist_ok=True)
# copy default theme
# Copy default theme
shutil.copytree(f"{base_dir}/themes/default", "themes/default", dirs_exist_ok=True)
def get_content_items(self):
@@ -147,13 +152,12 @@ class Site:
self.categories[category].append(content_item.slug)
def build(self):
# Recreate the output dir if needed
# Recreate the output dir if exists
if self.output_dir.exists():
shutil.rmtree(self.output_dir)
self.output_dir.mkdir()
# Create public subdirs
#subdirs = [self.images_dir, self.css_dir, self.js_dir, self.content_dir, "categories"]
subdirs = ["categories", "content", "static"]
for subdir in subdirs:
subdir = self.output_dir / subdir
@@ -178,17 +182,25 @@ class Site:
about_content.render_content(categories = self.categories, target_file='public/static/about.html')
# Render the index file
if hasattr(config, "footer"):
footer_data = config.footer
else:
footer_data = ''
visible_content_items = [c for c in self.content_items if c.data.get("hidden") != True]
with (self.output_dir / "index.html").open("w", encoding="utf-8") as f:
f.write(index_template.render(page_title = config.site_name, content_items=visible_content_items, categories = self.categories))
f.write(index_template.render(page_title = config.site_name, content_items=visible_content_items, categories = self.categories, footer_data = footer_data))
# Render the categories indices
if hasattr(config, "footer"):
footer_data = config.footer
else:
footer_data = ''
visible_content_items = [c for c in self.content_items if c.data.get("hidden") != True]
for category in self.categories:
category_index = Path(f"{self.output_dir}/categories/{category}.html")
category_items = [i for i in visible_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=f"{config.site_name}: {category}", content_items=category_items, categories = self.categories, parent_path = '../'))
f.write(index_template.render(page_title=f"{config.site_name}: {category}", content_items=category_items, categories = self.categories, parent_path = '../', footer_data = footer_data))
logger.info(f"Created {len(self.content_items)} content items.")