Keep contents in-memory

This commit is contained in:
Augusto Gunsch 2022-03-30 12:35:08 -03:00
parent 88de73d3cb
commit cab16f4c01
1 changed files with 13 additions and 9 deletions

View File

@ -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 '<h1>This page does not exist</h1>', 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('/<page>')
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: