from typing import Dict from os import walk, makedirs from os.path import isdir, join from shutil import copytree from article import read_article_file def compile(template_directory: str, work_directory: str, target_directory: str): copytree(join(template_directory, 'static'), join(target_directory, 'static'), dirs_exist_ok=True) copytree(join(work_directory, 'posts'), join(target_directory, 'post'), dirs_exist_ok=True) file = open(join(template_directory, 'post.html')) post_template = file.read() file.close() for root, dirs, files in walk(join(target_directory, 'post')): for fn in files: if fn.endswith('.html'): file = open(join(root, fn), 'r+') article = read_article_file(file) content = process_html(post_template, { 'title': article.title, 'summary': article.summary, 'content': article.content }) file.seek(0) file.write(content) file.close() def process_html(content: str, variables: Dict[str, str]) -> str: for k, v in variables.items(): content = replace_variable(k, v) return content def replace_variable(var: str, content: str) -> str: return content.replace('{{ ' + var + ' }}', content)