galerie-site/app.py

141 lines
4.5 KiB
Python
Raw Normal View History

2019-06-08 13:33:13 +02:00
# -*- coding: UTF-8 -*-
from os.path import exists, splitext
2020-11-27 18:43:47 +01:00
import os
from datetime import date
2020-06-22 21:31:16 +02:00
from markdown2 import markdown
2019-06-08 13:33:13 +02:00
2020-11-27 18:43:47 +01:00
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"])
2019-06-08 13:33:13 +02:00
file_exts = [".html", ".md", ".txt", ".pdf", ""]
2020-11-27 18:43:47 +01:00
update = date.today()
tijden = render("tijden.md")
nieuws = render("nieuws.md")
2021-11-14 16:08:44 +01:00
def read_hash():
with open("hash") as f:
line = f.readline()
return {"long": line, "short": line[0:7]}
2020-11-27 18:43:47 +01:00
inject_now = {'now': date.today().year,
'tijden': tijden,
'nieuws': nieuws,
2021-11-14 16:08:44 +01:00
'update': update,
'hash': read_hash()}
2019-06-08 13:33:13 +02:00
2020-11-27 18:43:47 +01:00
# @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()))
2019-06-08 13:33:13 +02:00
if __name__ == '__main__':
2020-11-27 18:43:47 +01:00
# 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()