This commit is contained in:
SG
2025-06-15 01:09:05 +03:00
parent 2072bf966f
commit 3ddf1a97d0
3 changed files with 21 additions and 23 deletions

View File

@@ -28,8 +28,10 @@ index_template = env.get_template("index.html")
content_item_template = env.get_template("content_item.html")
class ContentItem:
def render_content(self, categories):
logger.debug(f"Rendering {self.source_filename} to {self.target_filename}")
def render_content(self, categories, target_file = False):
if target_file:
self.target_file = Path(target_file)
logger.debug(f"Rendering {self.source_file} to {self.target_file}")
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}")
@@ -44,19 +46,19 @@ class ContentItem:
js_targetfile = Path(f"{Config.OUTPUT_DIR}/{Config.STATIC_DIR}/js/{self.js_file.name}")
logger.debug(f"Copying {self.js_file} to {js_targetfile}")
shutil.copyfile(self.js_file, js_targetfile)
with self.target_filename.open("w", encoding="utf-8") as f:
with self.target_file.open("w", encoding="utf-8") as f:
f.write(content_item_template.render(content_item = self, page_title = self.title, parent_path = '../', categories = categories))
except Exception as e:
logger.error(f"Renderer: {e}")
def parse_content(self):
logger.debug(f"Parsing file {self.source_filename}")
logger.debug(f"Parsing file {self.source_file}")
try:
self.source_filename = Path(self.source_filename)
self.subdir = self.source_filename.parent
self.slug = self.source_filename.stem
self.target_filename = Path(f"{Config.OUTPUT_DIR}/{self.source_filename.parent}/{self.source_filename.stem}.html")
self.data = frontmatter.load(self.source_filename)
self.source_file = Path(self.source_file)
self.subdir = self.source_file.parent
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)
self.preview = self.data.content.replace('\n', '<br>')[:300]
self.title = self.data.get("title", self.slug)
self.omit_second_title = self.data.get("omit_second_title", False)
@@ -65,20 +67,20 @@ class ContentItem:
self.hidden = self.data.get("hidden", str(False))
self.data.content = self.data.content.replace('\n', ' \n')
self.html = markdown.markdown(self.data.content)
cover_image_path = Path(f"{self.source_filename.parent}/{self.source_filename.stem}.jpg")
cover_image_path = Path(f"{self.source_file.parent}/{self.source_file.stem}.jpg")
self.image_file = cover_image_path if cover_image_path.exists() else ""
css_filepath = Path(f"{self.source_filename.parent}/{self.source_filename.stem}.css")
css_filepath = Path(f"{self.source_file.parent}/{self.source_file.stem}.css")
self.css_file = css_filepath if css_filepath.exists() else ""
js_filepath = Path(f"{self.source_filename.parent}/{self.source_filename.stem}.js")
js_filepath = Path(f"{self.source_file.parent}/{self.source_file.stem}.js")
self.js_file = js_filepath if js_filepath.exists() else ""
except Exception as e:
logger.error(f"Parser error, {e}")
def create_content(self):
with open(self.source_filename, mode="w", encoding="utf-8") as f:
with open(self.source_file, mode="w", encoding="utf-8") as f:
f.writelines([
"---\n",
f"title: {self.source_filename.stem}\n",
f"title: {self.source_file.stem}\n",
f"date: '{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}'\n",
"description: ''\n",
"author: ''\n",
@@ -89,8 +91,8 @@ class ContentItem:
"\n\n\n"
])
def __init__(self, filename):
self.source_filename = filename
def __init__(self, file):
self.source_file = file
class Site:
def __init__(self):
@@ -113,9 +115,6 @@ class Site:
# copy default theme
shutil.copytree(f"{base_dir}/themes/default", "themes/default", dirs_exist_ok=True)
# Create static/about.md
#about_item = ContentItem(filename = Path('static/about.md'))
#about_item.create_content()
def get_content_items(self):
logger.debug("Getting content items")
@@ -141,7 +140,7 @@ class Site:
# Create public subdirs
#subdirs = [self.images_dir, self.css_dir, self.js_dir, self.content_dir, "categories"]
subdirs = ["static", "categories"]
subdirs = ["categories", "content", "static"]
for subdir in subdirs:
subdir = self.output_dir / subdir
subdir.mkdir(parents=True, exist_ok=True)
@@ -162,7 +161,7 @@ class Site:
# Render the about file
about_content = ContentItem(Path(f'themes/{Config.THEME}/static/about.md'))
about_content.parse_content()
about_content.render_content(categories = self.categories)
about_content.render_content(categories = self.categories, target_file='public/static/about.html')
# Render the index file
visible_content_items = [c for c in self.content_items if c.data.get("hidden") != True]

View File

@@ -3,4 +3,3 @@ title: "About"
omit_second_title: True
---

View File

@@ -17,7 +17,7 @@
position: relative;
height: 200px;
background: rgb(70, 70, 124);
background-image: url("{{ parent_path }}static/header.jpg");
background-image: url("{{ parent_path }}static/images/header.jpg");
background-size: cover;
background-position: center;
display: flex;