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

View File

@@ -44,7 +44,8 @@ class Config:
defaults = {
"site_name": self.site_name,
"theme": "default",
"debug": False
"debug": False,
"footer": "'' # Author / copyright / date / links, can be plaintext or valid HTML"
}
with open(self.config_path, mode="w", encoding="utf-8") as f:
yaml.safe_dump(defaults, f)

View File

@@ -1,3 +0,0 @@
debug: false
site_name: microgen library
theme: default

View File

@@ -6,6 +6,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta charset="UTF-8">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.6/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-4Q6Gf2aSP4eDXB8Miphtr37CMZZQ5oXLH2yaXMJ2w8e2ZtHTl7GptT4jmndRuHDT" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.13.1/font/bootstrap-icons.min.css">
<link href="{{ parent_path }}static/css/theme.css" rel="stylesheet">
<title>{{ page_title }}</title>
{% block head_includes %}
@@ -45,9 +46,11 @@
<div id="footer-data" class="row d-flex text-muted justify-content-end">
{% block footer_includes %}
{% endblock %}
{% if footer_data %}
<div class="d-flex align-items-center justify-content-center">
<p class="my-0">© 2025 Kirill Bondarev</p>
<p class="my-0">{{ footer_data }}</p>
</div>
{% endif %}
</div>
<div id="footer-data-secondary" class="row d-flex text-muted justify-content-end">
<div class="d-flex align-items-center justify-content-center">