#!python3 from jinja2 import Environment, FileSystemLoader, select_autoescape import os from pathlib import Path import shutil import argparse import markdown #TODO: make this find templates first and save them, rather than hardcoding all of them #TODO: build some kind of functionality for listing and categorizing and otherwise having metadata for each file parser = argparse.ArgumentParser(prog="generate.ignore.py", description="shoofle's static site generator for shoofle.net") parser.add_argument("-i", "--input", type=Path, help="source materials") parser.add_argument("-o", "--output", type=Path, help="where to put the rendered site") parser.add_argument("-v", "--verbose", action="store_true", help="show verbose output") args = parser.parse_args() source = args.input finals = args.output env = Environment( loader=FileSystemLoader(source), autoescape=select_autoescape()) article_template = env.get_template("articles/article.template.html") md = markdown.Markdown(extensions=["tables"]) template_for_markdown = env.get_template("articles/article.template.md") def process_file(path_to_source, source, finals): rel_path = path_to_source.relative_to(source) path_to_output = finals / rel_path if ".ignore" in str(rel_path): return if ".git" in str(rel_path): return if path_to_source.is_dir(): pass elif ".template.html" in str(path_to_source): pass elif ".template.md" in str(path_to_source): pass 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 ".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 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 ".article.md" in str(path_to_source): # all .article.md files are rendered, then the same thing done as for article.html files text = f"something must have gone wrong in the parsing of {path_to_source} for you to see this" with open(path_to_source, "r") as input_file: text = input_file.read() html_source = md.convert(text) folder_name = path_to_output.parent / path_to_output.name.replace(".article.md", "") os.makedirs(folder_name, exist_ok=True) path_to_output = folder_name / "index.html" with open(path_to_output, "w") as output_file: output_file.write(template_for_markdown.render(title="Article", contents=html_source)) else: os.makedirs(path_to_output.parent, exist_ok=True) shutil.copy(path_to_source, path_to_output) if __name__ == "__main__": for directory_path, directory_names, file_names in os.walk(source, topdown=True): #filter out all .ignore and .git directories for i in reversed(range(len(directory_names))): if ".ignore" in directory_names[i] or ".git" in directory_names[i]: del directory_names[i] for file in file_names: process_file( Path(directory_path) / Path(file), source, finals ) if args.verbose: print(f"processing {file} in {directory_path}")