diff options
Diffstat (limited to 'tools/gen.py')
| -rw-r--r-- | tools/gen.py | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/tools/gen.py b/tools/gen.py new file mode 100644 index 0000000..e2f65e1 --- /dev/null +++ b/tools/gen.py @@ -0,0 +1,80 @@ +import os, shutil +from discord_md import md +import json +from collections import UserDict + +class IgnoreUnfoundPlaceholders(UserDict): + """Wrapper around dicts for using in str.format() + It basically tries to lookup the key it gets, and if the key doesn't exist, + it returns ``{keyname}`` where ``keyname`` is the key it got + """ + def __missing__(self, key): + return "{%s}"%str(key) + +# with open("conf.json", "r") as fd: +# conf=json.load(fd) + +with open("html/footer.html","r") as fd: + footer=fd.read() + +with open("html/header.html","r") as fd: + header=fd.read() + +with open("html/pagetemplate.html","r") as fd: + template=fd.read() + +template=template.format_map( + IgnoreUnfoundPlaceholders(header=header, footer=footer) +) + +if os.path.isdir("out"): + shutil.rmtree("out") + +out=os.path.abspath('out') +os.makedirs(out) +os.symlink(os.path.abspath("static"), os.path.join(out,"static"), True) +#os.chdir("content") + +def processdir(path): + print("processing",path) + os.chdir(path) + parent=os.path.dirname(path) + for file in os.listdir(): + if file.endswith(".md"): + + with open(file, "r") as fd: + print("| reading",file) + text=md.convert(fd.read()) + + meta:dict=md.Meta + print("Meta:",meta) + section=meta.get("section",[""])[0] + + html=template.format( + title=meta.get("title",["<No title>"])[0] + (" - "+section if section else ''), + section=section, + content=text, + toc=md.toc if not meta.get("notoc") else "" + ) + + newname=file.removesuffix(".md")+".html" + newname=newname.replace("README","index") + + target=os.path.join(out, section) + if not os.path.isdir(target): + os.makedirs(target) + + with open(os.path.join(out, section, newname), "x") as fd: + print("| writing") + fd.write(html) + + elif os.path.isdir(file): + processdir(os.path.join(path,file)) + + else: + shutil.copy2(file, os.path.join(out, os.path.basename(path))) + + os.chdir(parent) + +processdir("./content") + |
