import os import subprocess import yaml from dotenv import load_dotenv from flask import (Flask, redirect, render_template, request, send_from_directory, url_for) from markdown import markdown import yafg from datetime import datetime 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") 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().replace("../img", "img"), extensions=[ "markdown.extensions.attr_list", "markdown.extensions.codehilite", "markdown.extensions.fenced_code", "markdown.extensions.admonition", "markdown.extensions.abbr", yafg.YafgExtension(stripTitle=False) ], # extension_configs={ # 'markdown.extensions.codehilite': { # 'guess_lang': False, # } # } ) entries.append(html) last_update = datetime.now().strftime('%d-%m-%y %X') with open("index.html", "w") as r: r.write(render_template("render.html", contents=entries, last_update=last_update)) return "rendered" pull() app = Flask(__name__) app.wsgi_app = PrefixMiddleware(app.wsgi_app, prefix=PREFIX) @app.route("/") def home(): return send_from_directory(app.root_path, "index.html") @app.route("/img/") def send_image(filename): return send_from_directory(os.path.join(app.root_path, "contents", "img"), filename) @app.route("/update", methods=["GET", "POST"]) def update(): if request.method == "POST": print("Rendering the homepage after weebhook request") pull() render() return "GET method not supported" @app.route("/render") def manual_render(): print("Manual rendering the homepage") render() return redirect(url_for("home")) @app.before_first_request def prepare_page(): print("Rendering the homepage") render() app.run(port=PORT, debug=DEBUG)