shooflenet/generate.ignore.py

82 lines
3.0 KiB
Python

from jinja2 import Environment, FileSystemLoader, select_autoescape
import os
from pathlib import Path
import shutil
import argparse
import markdown
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")
md = markdown.Markdown(extensions=["tables"])
template_for_markdown = env.get_template("articles/article.template.md")
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 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))
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
elif ".template.md" in str(path_to_source): pass
else:
os.makedirs(path_to_output.parent, exist_ok=True)
shutil.copy(path_to_source, path_to_output)