Initial commit
This commit is contained in:
88
hydrogen.py
Normal file
88
hydrogen.py
Normal file
@@ -0,0 +1,88 @@
|
||||
from pathlib import Path
|
||||
import frontmatter
|
||||
import markdown
|
||||
from jinja2 import Environment, FileSystemLoader
|
||||
import shutil
|
||||
from config import Config
|
||||
|
||||
content_dir = Path('content')
|
||||
template_dir = Path('templates')
|
||||
output_dir = Path('public')
|
||||
img_dir = Path('public/images')
|
||||
assets_dir = Path('public/assets')
|
||||
assets_css_dir = Path('public/assets/css')
|
||||
assets_js_dir = Path('public/assets/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(page_title = self.title, header_image = header_image, image = self.image, custom_css = self.custom_css, custom_js = self.custom_js, content=self.html))
|
||||
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
|
||||
if self.slug == 'about':
|
||||
self.image = 'static/about.jpg'
|
||||
self.custom_css_src_file = Path(f"{content_dir}/{md_file.stem}.css")
|
||||
self.custom_css = f"assets/css/{md_file.stem}.css" if self.custom_css_src_file.exists() else None
|
||||
self.custom_js_src_file = Path(f"{content_dir}/{md_file.stem}.js")
|
||||
self.custom_js = f"assets/js/{md_file.stem}.js" if self.custom_js_src_file.exists() else None
|
||||
|
||||
# Recreate the output dir if needed
|
||||
if output_dir.exists():
|
||||
shutil.rmtree(output_dir)
|
||||
output_dir.mkdir()
|
||||
|
||||
# Create public subdirs
|
||||
img_dir.mkdir()
|
||||
assets_dir.mkdir()
|
||||
assets_css_dir.mkdir()
|
||||
assets_js_dir.mkdir()
|
||||
|
||||
# Copy static files if exist
|
||||
if static_dir.exists():
|
||||
shutil.copytree(static_dir, output_dir / "static")
|
||||
|
||||
# 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"):
|
||||
citem = ContentItemPrototype(md_file)
|
||||
citem.render_content()
|
||||
content_items.append({
|
||||
"slug": citem.slug,
|
||||
"title": citem.title,
|
||||
"date": citem.date,
|
||||
"preview": markdown.markdown(citem.preview),
|
||||
"image": citem.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()
|
||||
Reference in New Issue
Block a user