This commit is contained in:
SG
2025-06-10 20:50:21 +03:00
parent 26c4b9c1e1
commit 7c7dedcfa5
6 changed files with 76 additions and 109 deletions

View File

@@ -10,13 +10,15 @@ from jinja_env import env, content_item_template, index_template
class ContentItem: class ContentItem:
def render_content(self): def render_content(self):
logger.debug(f"Rendering {self.source_filename} to {Config.OUTPUT_DIR}/{self.target_filename}") logger.debug(f"Rendering {self.source_filename} to {self.target_filename}")
if self.image_file.exists(): if self.image_file and self.image_file.exists():
shutil.copyfile(self.image_src_file, Path(Config.OUTPUT_DIR) / self.image) logger.debug(f"Copying {self.image_file} to {Path(Config.OUTPUT_DIR) / self.image_file}")
if self.css_file.exists(): shutil.copyfile(self.image_file, Path(Config.OUTPUT_DIR) / self.image_file)
shutil.copyfile(self.custom_css_src_file,Path(Config.OUTPUT_DIR) / self.custom_css) self.image_file = f"{self.image_file.stem}.jpg"
if self.js_file.exists(): if self.css_file and self.css_file.exists():
shutil.copyfile(self.custom_js_src_file, Path(Config.OUTPUT_DIR) / self.custom_js) shutil.copyfile(self.css_file,Path(Config.OUTPUT_DIR) / self.css_file)
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: 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))
@@ -36,9 +38,10 @@ class ContentItem:
self.date = self.data.get("date", "2000-01-01T00:00:00+03:00") self.date = self.data.get("date", "2000-01-01T00:00:00+03:00")
self.categories = self.data.get("categories", str([])) self.categories = self.data.get("categories", str([]))
self.hidden = self.data.get("hidden", str(False)) self.hidden = self.data.get("hidden", str(False))
self.image_file = Path(self.source_filename.stem + ".jpg") cover_image_path = Path(self.source_filename.parent) / Path(self.source_filename.stem + ".jpg")
self.css_file = Path(self.source_filename.stem + ".css") self.image_file = cover_image_path if cover_image_path.exists() else None
self.js_file = Path(self.source_filename.stem + ".js") self.css_file = Path(self.source_filename.stem + ".css") if Path(self.source_filename.stem + ".css").exists() else None
self.js_file = Path(self.source_filename.stem + ".js") if Path(self.source_filename.stem + ".js").exists else None
except Exception as e: except Exception as e:
logger.error(e) logger.error(e)
@@ -61,68 +64,65 @@ class ContentItem:
self.source_filename = filename self.source_filename = filename
class Site: class Site:
def init_site(): def __init__(self):
self.output_dir = Path(Config.OUTPUT_DIR)
logger.info("Initializing new site") logger.info("Initializing new site")
content_dir = Path(Config.CONTENT_DIR) self.content_dir = Path(Config.CONTENT_DIR)
static_dir = Path(Config.STATIC_DIR) self.static_dir = Path(Config.STATIC_DIR)
templates_dir = Path(Config.TEMPLATES_DIR) self.templates_dir = Path(Config.TEMPLATES_DIR)
images_dir = Path(f"{Config.STATIC_DIR}/images") self.images_dir = Path(f"{Config.STATIC_DIR}/images")
css_dir = Path(f"{Config.STATIC_DIR}/css") self.css_dir = Path(f"{Config.STATIC_DIR}/css")
js_dir = Path(f"{Config.STATIC_DIR}/js") self.js_dir = Path(f"{Config.STATIC_DIR}/js")
self.output_dir = Path(Config.OUTPUT_DIR)
def init_site(self):
# Create directories # Create directories
for subdir in [content_dir, static_dir, templates_dir, images_dir, css_dir, js_dir]: for subdir in [self.content_dir, self.static_dir, self.templates_dir,
self.images_dir, self.css_dir, self.js_dir]:
os.makedirs(subdir, exist_ok=True) os.makedirs(subdir, exist_ok=True)
# Create templates from literals # Create templates from literals
import templates import templates
template_names = [t for t in dir(templates) if not t.startswith('_')] template_names = [t for t in dir(templates) if not t.startswith('_')]
for template_name in template_names: for template_name in template_names:
template_content = getattr(templates, template_name) template_content = getattr(templates, template_name)
with open(templates_dir / f"{template_name}.html", "w", encoding="utf8") as f: with open(self.templates_dir / f"{template_name}.html", "w", encoding="utf8") as f:
f.write(template_content) f.write(template_content)
# Create static/about.md # Create static/about.md
def build_site(self): def build(self):
# Recreate the output dir if needed # Recreate the output dir if needed
if output_dir.exists(): if self.output_dir.exists():
shutil.rmtree(output_dir) shutil.rmtree(self.output_dir)
output_dir.mkdir() self.output_dir.mkdir()
# Create public subdirs # Create public subdirs
subdirs = [img_dir, css_dir, js_dir] subdirs = [self.images_dir, self.css_dir, self.js_dir, self.content_dir]
for subdir in subdirs: for subdir in subdirs:
subdir.mkdir(arents=True, exist_ok=True) subdir = self.output_dir / subdir
subdir.mkdir(parents=True, exist_ok=True)
# Copy static files if exist # Copy static files if exist
if static_dir.exists(): if self.static_dir.exists():
shutil.copytree(static_dir, output_dir / "static", dirs_exist_ok=True) shutil.copytree(self.static_dir, self.output_dir / self.static_dir, dirs_exist_ok=True)
# Parse the content files # Parse the content files
content_items = [] content_items = []
for md_file in content_dir.glob("*.md"): for md_file in self.content_dir.glob("*.md"):
current_content_item = ContentItemPrototype(md_file) current_content_item = ContentItem(md_file)
current_content_item.parse_content()
current_content_item.render_content() current_content_item.render_content()
content_items.append({ content_items.append(current_content_item)
"slug": current_content_item.slug,
"title": current_content_item.title,
"date": current_content_item.date,
# "preview": markdown.markdown(current_content_item.preview),
"image": current_content_item.image,
})
# Render the index file # Render the index file
with (output_dir / "index.html").open("w", encoding="utf-8") as f: 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=content_items))
# Render the about file # Render the about file
about_content = ContentItemPrototype(Path('static/about.md')) about_content = ContentItem(Path('static/about.md'))
about_content.parse_content()
about_content.render_content() about_content.render_content()
# Move 'robots.txt' into output_dir
shutil.copyfile(static_dir / 'robots.txt', output_dir / 'robots.txt')
logger.info(f"Created {len(content_items)} content items.") logger.info(f"Created {len(content_items)} content items.")
def __init__(self):
self.output_dir = Path(Config.OUTPUT_DIR)

View File

@@ -39,9 +39,9 @@ def init_site_old():
create_content("about") create_content("about")
def init_site(): def init_site():
logger.debug(f"Initializing new site") site = Site()
site = SitePrototype
site.init_site() site.init_site()
def build_site(): def build_site():
logger.debug("Building site") site = Site()
site.build()

View File

@@ -4,52 +4,6 @@ from argparser import argparser
from classes import * from classes import *
from functions import * from functions import *
def build_site_old():
logger.info(f"Building the '{Config.MAIN_PAGE_TITLE}' site.")
# Recreate the output dir if needed
if output_dir.exists():
shutil.rmtree(output_dir)
output_dir.mkdir()
# Create public subdirs
subdirs = [img_dir, assets_css_dir, assets_js_dir]
for subdir in subdirs:
subdir.mkdir(parents=True, exist_ok=True)
# Copy static files if exist
if static_dir.exists():
shutil.copytree(static_dir, output_dir / "static", dirs_exist_ok=True)
# Parse the content files
content_items = []
for md_file in content_dir.glob("*.md"):
current_content_item = ContentItemPrototype(md_file)
current_content_item.render_content()
content_items.append({
"slug": current_content_item.slug,
"title": current_content_item.title,
"date": current_content_item.date,
# "preview": markdown.markdown(current_content_item.preview),
"image": current_content_item.image,
})
# Render the index file
with (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))
# Render the about file
about_content = ContentItemPrototype(Path('static/about.md'))
about_content.render_content()
# Move 'robots.txt' into output_dir
shutil.copyfile(static_dir / 'robots.txt', output_dir / 'robots.txt')
logger.info(f"Created {len(content_items)} content items.")
def main(): def main():
args = argparser.parse_args() args = argparser.parse_args()
match args.command: match args.command:

View File

@@ -7,14 +7,19 @@
{% endif %} {% endif %}
{% endblock %} {% endblock %}
{% block menu_links %}
<a href="../index.html">Home</a>
<a href="../static/about.html">About</a>
{% endblock %}
{% block content %} {% block content %}
<div class="container mt-4 mb-5"> <div class="container mt-4 mb-5">
{% if content_item.image %}<img src="{{ content_item.image }}" class="img-fluid mb-5" style="border-radius: 5px;">{% endif %} {% if content_item.image_file %}<img src="{{ content_item.image_file }}" class="img-fluid mb-5" style="border-radius: 5px;">{% endif %}
{% if not content_item.omit_second_title %} {% if not content_item.omit_second_title %}
<h1>{{ content_item.title }}</h1> <h1>{{ content_item.title }}</h1>
{% endif %} {% endif %}
<article>{{ content_item.html | safe }}</article> <article>{{ content_item.html | safe }}</article>
<a href="index.html" class="btn btn-secondary mt-4">← Back</a> <a href="../index.html" class="btn btn-secondary mt-4">← Back</a>
</div> </div>
{% endblock %} {% endblock %}

View File

@@ -74,9 +74,8 @@
</div> </div>
</div> </div>
<div class="top_menu d-flex justify-content-end" style="margin-bottom: 20px;"> <div class="top_menu d-flex justify-content-end" style="margin-bottom: 20px;">
{% block menu_links %}
<a href="index.html">Home</a> {% endblock %}
<a href="about.html">About</a>
</div> </div>

View File

@@ -1,20 +1,29 @@
{% extends "default.html" %} {% extends "default.html" %}
{% block menu_links %}
<a href="index.html">Home</a>
<a href="static/about.html">About</a>
{% endblock %}
{% block content %} {% block content %}
<div class="row row-cols-1 row-cols-md-3 g-4 px-1 py-2"> <!-- <div class="row row-cols-1 row-cols-md-3 row-cols-xxl-5 g-4 px-1 py-2"> -->
{% for content_item in content_items %} <div class="container-fluid">
<div class="col mb-3"> <div class="row justify-content-center">
<div class="card h-100" style="border-radius: 5px;"> {% for content_item in content_items %}
<img src="{{ content_item.image }}" alt="{{ content_item.title }}" class="card-img-top img-fluid" style="background: rgb(77, 77, 77); object-fit: cover; height: 200px; border-radius: 5px;"> <div class="card h-100 rounded mx-1 my-3" style="width: 100%; max-width: 400px;">
<div class="card-body d-flex flex-column"> <div class="card-header bg-dark">
<h5 class="card-title"><a class="text-decoration-none text-body" href="{{ content_item.slug}}.html ">{{ content_item.title }}</a></h5> <img src="content/{{ content_item.image_file }}" alt="{{ content_item.title }}" class="card-img-top img-fluid mx-auto d-block object-fit-scale rounded" style="max-height: 200px; width: auto;">
<p class="card-text mb-3">{{ content_item.preview | safe}}</p> </div>
<div class="card-body d-flex flex-column">
<h5 class="card-title"><a class="text-decoration-none text-body" href="content/{{ content_item.slug}}.html ">{{ content_item.title }}</a></h5>
<p class="card-text mb-3">{{ content_item.preview | safe}}</p>
</div>
</div> </div>
</div> {% endfor %}
</div> </div>
{% endfor %}
</div> </div>
{% endblock %} {% endblock %}