galerie-site/app.py

108 lines
3.3 KiB
Python
Raw Normal View History

2019-06-08 13:33:13 +02:00
# -*- coding: UTF-8 -*-
from flask import Flask, render_template, Markup, redirect, request, send_file, send_from_directory
from datetime import datetime
from markdown2 import markdown
from os.path import exists, splitext
import json
from pathlib import Path
app = Flask(__name__, static_url_path="", static_folder="static")
file_exts = [".html", ".md", ".txt", ".pdf", ""]
update = open("date", encoding="UTF-8").readline()
tijden = Markup(
markdown("".join(open("data/tijden.md", encoding="UTF-8").readlines())))
nieuws = Markup(
markdown("".join(open("data/nieuws.md", encoding="UTF-8").readlines())))
renders = dict()
2019-06-08 13:33:13 +02:00
@app.context_processor
def inject_now():
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(
"".join(open("data/"+filename, encoding="UTF-8").readlines()),
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():
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)
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"):
filename = url.lstrip("/") + ".md"
2019-06-08 13:33:13 +02:00
else:
filename = "404.md"
2019-06-08 13:33:13 +02:00
return render_template("index.html",
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)