Keep contents in-memory
This commit is contained in:
parent
88de73d3cb
commit
cab16f4c01
22
index.py
22
index.py
|
@ -1,23 +1,26 @@
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
import random
|
import random
|
||||||
|
from pathlib import Path
|
||||||
from flask import Flask
|
from flask import Flask
|
||||||
from flask import render_template, url_for, redirect
|
from flask import render_template, url_for, redirect
|
||||||
import markdown2
|
import markdown2
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
CONTENT_DIR = 'content/'
|
CONTENT_DIR = Path('content')
|
||||||
STATIC_DIR = 'static/'
|
STATIC_DIR = 'static/'
|
||||||
|
|
||||||
lang = 'en'
|
lang = 'en'
|
||||||
|
|
||||||
|
contents = {}
|
||||||
|
|
||||||
|
for md in CONTENT_DIR.glob('*'):
|
||||||
|
with md.open() as f:
|
||||||
|
contents[md.name] = markdown2.markdown(f.read())
|
||||||
|
|
||||||
def not_found():
|
def not_found():
|
||||||
return '<h1>This page does not exist</h1>', 404
|
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):
|
def get_quote(lang):
|
||||||
quotes = STATIC_DIR + 'quotes_%s.txt' % lang
|
quotes = STATIC_DIR + 'quotes_%s.txt' % lang
|
||||||
|
|
||||||
|
@ -43,11 +46,12 @@ def index():
|
||||||
|
|
||||||
@app.route('/<page>')
|
@app.route('/<page>')
|
||||||
def page(page):
|
def page(page):
|
||||||
md = CONTENT_DIR + '%s_%s.md' % (page, lang)
|
md = '%s_%s.md' % (page, lang)
|
||||||
html = 'page_%s.html' % lang
|
|
||||||
|
|
||||||
if os.path.isfile(md):
|
if md in contents:
|
||||||
body = read_md(md)
|
body = contents[md]
|
||||||
|
|
||||||
|
html = 'page_%s.html' % lang
|
||||||
|
|
||||||
return render_template(html, body=body, page=page.capitalize())
|
return render_template(html, body=body, page=page.capitalize())
|
||||||
else:
|
else:
|
||||||
|
|
Loading…
Reference in New Issue