from jinja2 import Environment, FileSystemLoader, select_autoescape import os from pathlib import Path import shutil import argparse parser = argparse.ArgumentParser() parser.add_argument("-i", "--input", type=Path, help="source materials") parser.add_argument("-o", "--output", type=Path, help="where to put the rendered site") args = parser.parse_args() source = args.input finals = args.output env = Environment( loader=FileSystemLoader(source), autoescape=select_autoescape()) # clear out the folder finalized # walk the source directory and make all the corresponding files into finalized # article template is used for all .article.html files article_template = env.get_template("articles/article.template.html") for path_to_source in source.glob("**/*"): rel_path = path_to_source.relative_to(source) path_to_output = finals / rel_path if ".ignore" in str(rel_path): continue if ".git" in str(rel_path): continue if path_to_source.is_dir(): pass elif ".article.html" in str(path_to_source): # all .article.html files are transformed into a folder of the same name and then rendered with the template at index.html folder_name = path_to_output.parent / path_to_output.name.replace(".article.html","") os.makedirs(folder_name, exist_ok=True) # make the folder /var/www/shoofle.net/articles/circle_script path_to_output = folder_name / "index.html" with open(path_to_output, "w") as output_file: output_file.write(article_template.render(title="Article", target=str(rel_path))) elif ".renderme" in str(path_to_source): path_to_output = path_to_output.parent / path_to_output.name.replace(".renderme","") with open(path_to_output, "w") as output_file: os.makedirs(path_to_output.parent, exist_ok=True) output_file.write(env.get_template(rel_path).render()) elif ".template.html" in str(path_to_source): pass else: os.makedirs(path_to_output.parent, exist_ok=True) shutil.copy(path_to_source, path_to_output)