some reworking
This commit is contained in:
130
hydrogen.py
130
hydrogen.py
@@ -1,12 +1,13 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
from pathlib import Path
|
||||
import frontmatter
|
||||
import markdown
|
||||
from jinja2 import Environment, FileSystemLoader
|
||||
|
||||
import shutil
|
||||
from config import Config
|
||||
from logger import logger
|
||||
from argparser import argparser
|
||||
from classes import *
|
||||
from functions import *
|
||||
|
||||
content_dir = Path('content')
|
||||
template_dir = Path('templates')
|
||||
@@ -17,82 +18,69 @@ assets_js_dir = Path(output_dir / 'static/js')
|
||||
static_dir = Path('static')
|
||||
header_image = Config.HEADER_IMAGE or '/static/header.jpg'
|
||||
|
||||
class ContentItemPrototype:
|
||||
def render_content(self):
|
||||
if self.image_src_file.exists():
|
||||
shutil.copyfile(self.image_src_file, output_dir / self.image)
|
||||
if self.custom_css_src_file.exists():
|
||||
shutil.copyfile(self.custom_css_src_file,output_dir / self.custom_css)
|
||||
if self.custom_js_src_file.exists():
|
||||
shutil.copyfile(self.custom_js_src_file, output_dir / self.custom_js)
|
||||
with self.content_output_path.open("w", encoding="utf-8") as f:
|
||||
f.write(content_item_template.render(content_item = self, page_title = self.title))
|
||||
|
||||
def __init__(self, md_file):
|
||||
self.slug = md_file.stem
|
||||
self.data = frontmatter.load(md_file)
|
||||
self.html = markdown.markdown(self.data.content)
|
||||
self.preview = self.html[:300] + "<a href=" + f"{self.slug}.html" + ">... read more</a>"
|
||||
self.title = self.data.get("title", self.slug)
|
||||
self.omit_second_title = self.data.get("omit_second_title", False)
|
||||
self.date = self.data.get("data", "2000-01-01T00:00:00+03:00")
|
||||
self.content_output_path = output_dir / f"{self.slug}.html"
|
||||
self.image_src_file = Path(f"{content_dir}/{md_file.stem}.jpg")
|
||||
self.image = f"images/{md_file.stem}.jpg" if self.image_src_file.exists() else None
|
||||
self.custom_css_src_file = Path(f"{content_dir}/{md_file.stem}.css")
|
||||
self.custom_css = f"static/css/{md_file.stem}.css" if self.custom_css_src_file.exists() else None
|
||||
logger.debug(f"Custom CSS: {self.slug}: {self.custom_css}")
|
||||
self.custom_js_src_file = Path(f"{content_dir}/{md_file.stem}.js")
|
||||
self.custom_js = f"static/js/{md_file.stem}.js" if self.custom_js_src_file.exists() else None
|
||||
logger.debug(f"Custom JS: {self.slug}: {self.custom_js}")
|
||||
# Special handling for the 'about' item
|
||||
if self.slug == 'about':
|
||||
self.image = 'static/about.jpg'
|
||||
|
||||
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)
|
||||
|
||||
# Prepare template rendering engine
|
||||
env = Environment(loader=FileSystemLoader(str(template_dir)))
|
||||
env.globals['header_image'] = header_image
|
||||
index_template = env.get_template("index.html")
|
||||
content_item_template = env.get_template("content_item.html")
|
||||
|
||||
# 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,
|
||||
})
|
||||
def build_site():
|
||||
logger.info(f"Building the '{Config.MAIN_PAGE_TITLE}' site.")
|
||||
|
||||
# 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))
|
||||
# Recreate the output dir if needed
|
||||
if output_dir.exists():
|
||||
shutil.rmtree(output_dir)
|
||||
output_dir.mkdir()
|
||||
|
||||
# Render the about file
|
||||
about_content = ContentItemPrototype(Path('static/about.md'))
|
||||
about_content.render_content()
|
||||
# Create public subdirs
|
||||
subdirs = [img_dir, assets_css_dir, assets_js_dir]
|
||||
for subdir in subdirs:
|
||||
subdir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
# Move 'robots.txt' into output_dir
|
||||
shutil.copyfile(static_dir / 'robots.txt', output_dir / 'robots.txt')
|
||||
# Copy static files if exist
|
||||
if static_dir.exists():
|
||||
shutil.copytree(static_dir, output_dir / "static", dirs_exist_ok=True)
|
||||
|
||||
logger.info(f"Created {len(content_items)} content items.")
|
||||
# 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():
|
||||
args = argparser.parse_args()
|
||||
if args.content:
|
||||
content = args.content or args.edit
|
||||
create_content(content)
|
||||
edit_content(content)
|
||||
if args.build:
|
||||
build_site()
|
||||
if args.init:
|
||||
init_site()
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in New Issue
Block a user