galerie-site/app.py
2020-11-27 18:43:47 +01:00

136 lines
4.4 KiB
Python

# -*- coding: UTF-8 -*-
from os.path import exists, splitext
import os
from datetime import date
from markdown2 import markdown
import shutil
from jinja2 import Environment, FileSystemLoader
env = Environment(
loader=FileSystemLoader("./templates")
)
def render(filename):
return markdown(
"".join(open("data/" + filename, encoding="UTF-8").readlines()),
use_file_vars=True,
extras=["use-file-vars", "tables"])
file_exts = [".html", ".md", ".txt", ".pdf", ""]
update = date.today()
tijden = render("tijden.md")
nieuws = render("nieuws.md")
inject_now = {'now': date.today().year,
'tijden': tijden,
'nieuws': nieuws,
'update': update}
# @app.route('/')
# def index():
# return render_template("index.html", content=render("content.md"))
# @app.route("/<string:url>")
# def site(url: str):
# # error handling
# if url.startswith("hoofdexpo") or url.startswith("fotos"):
# return redirect("/schilderijen", 301)
# if url.startswith("boek&foto"):
# return redirect("/boekenmeer", 301)
# if url.endswith(".html"):
# return redirect(url.rstrip(".html"), 301)
# if url.endswith(".ico"):
# return send_from_directory("static", url)
# # actual code
# if url == "content":
# return redirect("/", 301)
# if exists("data/" + url.lstrip("/") + ".md"):
# filename = url.lstrip("/") + ".md"
# else:
# filename = "404.md"
# return render_template("index.html",
# content=render(filename))
# @app.route("/archief/<string:url>")
# def archief(url: str):
# if url == "content":
# return redirect("/")
# filename = ""
# ex = ""
# url, _ = splitext(url)
# print(url)
# for ext in file_exts:
# print("data/archief/" + url + ext)
# if exists("data/archief/" + url + ext):
# filename = "data/archief/" + url + ext
# ex = ext
# break
# if filename == "":
# filename = "data/404.md"
# ex = ".md"
# if ex == ".html":
# return Markup("".join(open("data/archief/" + url + ".html", encoding="utf-8").readlines()))
# elif ex == ".md":
# return render_template("index.html",
# content=Markup(markdown("".join(open(filename, encoding="UTF-8").readlines()),
# use_file_vars=True,
# extras=["use-file-vars"])))
# elif ex == ".txt":
# return render_template("index.html", content="".join(open(filename, encoding="UTF-8").readlines()))
# elif ex == ".pdf":
# return send_file("data/archief/" + url + ".pdf", mimetype="application/pdf")
# else:
# return render_template("index.html", content="".join(open(filename, encoding="UTF-8").readlines()))
if __name__ == '__main__':
# app.run(host="0.0.0.0", port=5667)
t = env.get_template("index.html")
if exists("./dist"):
shutil.rmtree("./dist")
os.makedirs("./dist")
for d in os.listdir("./data/"):
d = d.replace(".md", "")
if d == "content":
f = open(f"./dist/index.html", "w", encoding="utf-8")
r = t.render(content=render(f"{d}.md"), request={'path': f"/"}, **inject_now)
f.write(r)
f.close()
else:
f = open(f"./dist/{d}.html", "w", encoding="utf-8")
r = t.render( content=render(f"{d}.md"), request={'path': f"/{d}"}, **inject_now)
f.write(r)
f.close()
os.makedirs("./dist/archief")
for d in os.listdir("./data/archief"):
name,ext = splitext(d)
if ext in [".html", ".pdf"]:
shutil.copyfile(f"./data/archief/{d}", f"./dist/archief/{d}")
elif ext == ".txt":
d = d.replace(".txt", "")
# print(d)
r = t.render( content=render(f"archief/{d}.txt"), request={'url': f"/{d}"}, **inject_now)
f = open(f"./dist/archief/{d}.html", "w", encoding="utf-8")
f.write(r)
f.close()
elif ext == ".md":
d = d.replace(".md", "")
# print(d)
r = t.render( content=render(f"archief/{d}.md"), request={'url': f"/{d}"}, **inject_now)
f = open(f"./dist/archief/{d}.html", "w", encoding="utf-8")
f.write(r)
f.close()