# -*- 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() @app.context_processor def inject_now(): return {'now': datetime.utcnow().year, 'tijden': tijden, 'nieuws': nieuws, 'update': update} def render(filename): 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 @app.route('/') def index(): return render_template("index.html", content=render("content.md")) @app.route("/") 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/") 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)