2019-06-08 13:33:13 +02:00
|
|
|
# -*- coding: UTF-8 -*-
|
|
|
|
|
|
|
|
from datetime import datetime
|
|
|
|
from os.path import exists, splitext
|
2020-06-22 21:31:16 +02:00
|
|
|
|
|
|
|
from flask import Flask, render_template, Markup, redirect, send_file, send_from_directory
|
|
|
|
from markdown2 import markdown
|
2019-06-08 13:33:13 +02:00
|
|
|
|
|
|
|
app = Flask(__name__, static_url_path="", static_folder="static")
|
|
|
|
|
|
|
|
file_exts = [".html", ".md", ".txt", ".pdf", ""]
|
|
|
|
|
2019-06-08 19:54:47 +02:00
|
|
|
update = open("date", encoding="UTF-8").readline()
|
|
|
|
tijden = Markup(
|
2020-06-23 16:32:36 +02:00
|
|
|
markdown("".join(open("data/tijden.md", encoding="UTF-8").readlines()),
|
|
|
|
use_file_vars=True,
|
|
|
|
extras=["use-file-vars", "tables"]))
|
2019-06-08 19:54:47 +02:00
|
|
|
nieuws = Markup(
|
2020-06-23 16:32:36 +02:00
|
|
|
markdown("".join(open("data/nieuws.md", encoding="UTF-8").readlines()),
|
|
|
|
use_file_vars=True,
|
|
|
|
extras=["use-file-vars", "tables"]))
|
2019-06-08 19:54:47 +02:00
|
|
|
renders = dict()
|
2019-06-08 13:33:13 +02:00
|
|
|
|
|
|
|
|
|
|
|
@app.context_processor
|
|
|
|
def inject_now():
|
2019-06-08 19:54:47 +02:00
|
|
|
return {'now': datetime.utcnow().year,
|
|
|
|
'tijden': tijden,
|
|
|
|
'nieuws': nieuws,
|
|
|
|
'update': update}
|
|
|
|
|
|
|
|
|
|
|
|
def render(filename):
|
2019-06-12 09:09:59 +02:00
|
|
|
return Markup(markdown(
|
2020-06-22 21:31:16 +02:00
|
|
|
"".join(open("data/" + filename, encoding="UTF-8").readlines()),
|
2019-06-12 09:09:59 +02:00
|
|
|
use_file_vars=True,
|
|
|
|
extras=["use-file-vars", "tables"]))
|
|
|
|
# if filename in renders:
|
|
|
|
# return renders[filename]
|
|
|
|
# else:
|
|
|
|
# ren = ""
|
|
|
|
# renders[filename] = ren
|
|
|
|
# return ren
|
2019-06-08 13:33:13 +02:00
|
|
|
|
|
|
|
|
|
|
|
@app.route('/')
|
|
|
|
def index():
|
2019-06-08 19:54:47 +02:00
|
|
|
return render_template("index.html", content=render("content.md"))
|
2019-06-08 13:33:13 +02:00
|
|
|
|
|
|
|
|
|
|
|
@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)
|
|
|
|
|
2019-06-08 19:54:47 +02:00
|
|
|
if url.endswith(".ico"):
|
2019-06-08 13:33:13 +02:00
|
|
|
return send_from_directory("static", url)
|
|
|
|
|
|
|
|
# actual code
|
|
|
|
if url == "content":
|
|
|
|
return redirect("/", 301)
|
|
|
|
if exists("data/" + url.lstrip("/") + ".md"):
|
2019-06-08 19:54:47 +02:00
|
|
|
filename = url.lstrip("/") + ".md"
|
2019-06-08 13:33:13 +02:00
|
|
|
else:
|
2019-06-08 19:54:47 +02:00
|
|
|
filename = "404.md"
|
2019-06-08 13:33:13 +02:00
|
|
|
|
|
|
|
return render_template("index.html",
|
2019-06-08 19:54:47 +02:00
|
|
|
content=render(filename))
|
2019-06-08 13:33:13 +02:00
|
|
|
|
|
|
|
|
|
|
|
@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)
|