import os import subprocess import yaml from dotenv import load_dotenv from flask import Flask, render_template, request, send_from_directory from markdown import markdown from prefix import PrefixMiddleware load_dotenv() PORT = os.environ.get("PORT", 3000) DEBUG = os.environ.get("DEBUG", True) PREFIX = os.environ.get("PREFIX", "") REPO = os.environ.get("REPO", "https://git.xpub.nl/kamo/thesis.git") SERVER_NAME = os.environ.get("SERVER_NAME", "localhost:3000") def pull(): print("Retrieving contents") subprocess.call(["sh", "update.sh", REPO]) def render(): with open("index.yaml", "r") as i: index = yaml.safe_load(i) entries = [] for entry in index: with open(os.path.join("contents", entry)) as e: html = markdown( e.read(), extensions=[ "markdown.extensions.attr_list", "markdown.extensions.codehilite", "markdown.extensions.fenced_code", ], ) entries.append(html) with open("index.html", "w") as r: r.write(render_template("render.html", contents=entries)) return "rendered" app = Flask(__name__) app.wsgi_app = PrefixMiddleware(app.wsgi_app, prefix=PREFIX) app.config["SERVER_NAME"] = SERVER_NAME pull() with app.app_context(): render() @app.route("/") def home(): return send_from_directory(app.root_path, "index.html") @app.route("/update", methods=["GET", "POST"]) def update(): if request.method == "POST": pull() return "GET method not supported" app.run(port=PORT, debug=DEBUG)