reorganize generate script
This commit is contained in:
parent
51aff04cd8
commit
3aff02c8bb
61
generate.ignore.py
Normal file → Executable file
61
generate.ignore.py
Normal file → Executable file
@ -1,3 +1,5 @@
|
||||
#!python3
|
||||
|
||||
from jinja2 import Environment, FileSystemLoader, select_autoescape
|
||||
import os
|
||||
from pathlib import Path
|
||||
@ -5,9 +7,16 @@ import shutil
|
||||
import argparse
|
||||
import markdown
|
||||
|
||||
parser = argparse.ArgumentParser()
|
||||
|
||||
#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
|
||||
@ -17,26 +26,31 @@ 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("**/*"):
|
||||
|
||||
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): continue
|
||||
if ".git" in str(rel_path): continue
|
||||
|
||||
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
|
||||
|
||||
@ -48,7 +62,6 @@ for path_to_source in source.glob("**/*"):
|
||||
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
|
||||
|
||||
@ -66,16 +79,24 @@ for path_to_source in source.glob("**/*"):
|
||||
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)
|
||||
|
||||
|
||||
|
||||
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}")
|
||||
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user