import datetime import email.utils import os.path import shutil import sys import jinja2 from .config import Config from .modules import blogpost_processor def init_jinja_env() -> jinja2.Environment: jinja_env = jinja2.Environment(loader=jinja2.FileSystemLoader(Config.TEMPLATES_SOURCE_DIR)) return jinja_env def main(output_root_path: str, local: bool): if local: Config.BLOG_ROOT_URL = "http://localhost" if os.path.exists(output_root_path): shutil.rmtree(output_root_path) os.mkdir(output_root_path) jinja_env = init_jinja_env() posts = blogpost_processor.get_posts(Config.POST_SOURCE_DIR, local) tag_occurrence_count = {} for post in posts: for tag in post.meta_data[blogpost_processor.TAGS_KEY]: if tag in tag_occurrence_count: tag_occurrence_count[tag] += 1 else: tag_occurrence_count[tag] = 1 tags = [str(key) for key in tag_occurrence_count.keys()] tags.sort(key=lambda key: tag_occurrence_count[key], reverse=True) top_tags = tags[:10] jinja_env.globals.update(site={ "assets_path": Config.ASSETS_IMPORT_PATH, "assets_path_static": Config.ASSETS_IMPORT_PATH_STATIC, "host_name": Config.BLOG_HOSTNAME, "blog_name": Config.BLOG_NAME, "subtitle": Config.BLOG_SUBTITLE, "top_tags": top_tags }) blogpost_template = jinja_env.get_template("blogpost.html.j2") subpage_template = jinja_env.get_template("blogpost_sub.html.j2") for post in posts: output_dir = os.path.join(output_root_path, "posts", post.get_publish_year(), post.name) os.makedirs(output_dir, exist_ok=True) for extra_file in post.extra_files: from_path = os.path.join(post.path, extra_file) if os.path.isdir(from_path): shutil.copytree(from_path, os.path.join(output_dir, extra_file)) else: shutil.copyfile(from_path, os.path.join(output_dir, extra_file)) ctx = { "post": post, "url": f"{Config.BLOG_ROOT_URL}{post.href}" } with open(os.path.join(output_dir, "index.html"), "w") as f: f.write(blogpost_template.render(ctx)) for subpage in post.subpages: ctx = { "post": post, "subpage_name": subpage[0], "subpage_html": subpage[1], "url": f"{Config.BLOG_ROOT_URL}{post.href}/{subpage[0]}.html" } with open(os.path.join(output_dir, f"{subpage[0]}.html"), "w") as f: f.write(subpage_template.render(ctx)) tags_template = jinja_env.get_template("tags.html.j2") output_dir = os.path.join(output_root_path, "tags") os.makedirs(output_dir, exist_ok=True) ctx = { "config": Config, "tags_ls": " ".join([f'{tag}/' for tag in tags]), "url": f"{Config.BLOG_ROOT_URL}/tags" } with open(os.path.join(output_dir, "index.html"), "w") as f: f.write(tags_template.render(ctx)) tag_template = jinja_env.get_template("tag.html.j2") for tag in tags: ctx = { "config": Config, "tag": tag, "posts": [post for post in posts if tag in post.meta_data["tags"]], "url": f"{Config.BLOG_ROOT_URL}/tags/{tag}.html" } with open(os.path.join(output_dir, "{}.html".format(tag)), "w") as f: f.write(tag_template.render(ctx)) index_template = jinja_env.get_template("index.html.j2") ctx = { "posts": posts, "url": Config.BLOG_ROOT_URL } with open(os.path.join(output_root_path, "index.html"), "w") as f: f.write(index_template.render(ctx)) acknowledgement_template = jinja_env.get_template("acknowledgement.html.j2") with open(os.path.join(output_root_path, "acknowledgement.html"), "w") as f: f.write(acknowledgement_template.render({})) rss_template = jinja_env.get_template("feed.xml.j2") ctx = { "build_date": email.utils.format_datetime(datetime.datetime.now(Config.TIMEZONE)), "posts": posts[:5] } with open(os.path.join(output_root_path, "feed.xml"), "w") as f: f.write(rss_template.render(ctx)) shutil.copytree(Config.ASSETS_SOURCE_DIR, output_root_path + Config.ASSETS_IMPORT_PATH) if __name__ == '__main__': main(sys.argv[1], bool(int(sys.argv[2])) if len(sys.argv) > 2 else False)