From cab16f4c01db83ceff17bdad86eb9250b810d8ba Mon Sep 17 00:00:00 2001 From: Augusto Gunsch Date: Wed, 30 Mar 2022 12:35:08 -0300 Subject: [PATCH] Keep contents in-memory --- index.py | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/index.py b/index.py index 7fa11c8..27d72c2 100644 --- a/index.py +++ b/index.py @@ -1,23 +1,26 @@ import os import re import random +from pathlib import Path from flask import Flask from flask import render_template, url_for, redirect import markdown2 app = Flask(__name__) -CONTENT_DIR = 'content/' +CONTENT_DIR = Path('content') STATIC_DIR = 'static/' lang = 'en' +contents = {} + +for md in CONTENT_DIR.glob('*'): + with md.open() as f: + contents[md.name] = markdown2.markdown(f.read()) + def not_found(): return '

This page does not exist

', 404 -def read_md(fname): - with open(fname, 'r') as f: - return markdown2.markdown(f.read()) - def get_quote(lang): quotes = STATIC_DIR + 'quotes_%s.txt' % lang @@ -43,11 +46,12 @@ def index(): @app.route('/') def page(page): - md = CONTENT_DIR + '%s_%s.md' % (page, lang) - html = 'page_%s.html' % lang + md = '%s_%s.md' % (page, lang) - if os.path.isfile(md): - body = read_md(md) + if md in contents: + body = contents[md] + + html = 'page_%s.html' % lang return render_template(html, body=body, page=page.capitalize()) else: