from datetime import datetime import shutil import markdown import frontmatter from jinja2 import Environment, FileSystemLoader from pathlib import Path from logger import logger from config import Config # Prepare template rendering engine env = Environment(loader=FileSystemLoader(Config.TEMPLATE_DIR)) env.globals['header_image'] = Config.HEADER_IMAGE index_template = env.get_template("index.html") content_item_template = env.get_template("content_item.html") class DefaultFrontmatterHeader: def __init__(self, title): self.title = f"title: '{title}'\n" self.date = f"date: '{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}'\n" self.omit_second_title = f"omit_second_title: '{str(False)}'\n" self.description = "description: \n" self.author = "author: \n" class ContentItemPrototype: def render_content(self): logger.debug(f"Rendering {self.source_filename} to {Config.OUTPUT_DIR}/{self.target_filename}") if self.image_src_file.exists(): shutil.copyfile(self.image_src_file, Path(Config.OUTPUT_DIR) / self.image) if self.custom_css_src_file.exists(): shutil.copyfile(self.custom_css_src_file,Path(Config.OUTPUT_DIR) / self.custom_css) if self.custom_js_src_file.exists(): shutil.copyfile(self.custom_js_src_file, Path(Config.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): logger.debug(f"Parsing {md_file}") self.source_filename = md_file self.slug = md_file.stem self.target_filename = self.slug + ".html" self.data = frontmatter.load(md_file) self.html = markdown.markdown(self.data.content) self.preview = self.html[:300] + "... read more" 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 = Path(Config.OUTPUT_DIR) / f"{self.slug}.html" self.image_src_file = Path(f"{Config.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"{Config.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"{Config.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'