website/index.py

58 lines
1.3 KiB
Python
Raw Normal View History

2022-03-28 15:24:48 -04:00
import os
import re
import random
from flask import Flask
from flask import render_template, url_for, redirect
import markdown2
app = Flask(__name__)
CONTENT_DIR = 'content/'
STATIC_DIR = 'static/'
2022-03-30 07:14:42 -04:00
lang = 'en'
2022-03-28 15:24:48 -04:00
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
if not os.path.isfile(quotes):
quotes = STATIC_DIR + 'quotes_en.txt'
with open(quotes, 'r') as f:
quotes = f.read()
quotes = quotes.splitlines()
quote = quotes[random.randrange(0, len(quotes)-1)]
m = re.search('^(.+)(\.|!|\?) ((\d )?\w+ (\(.*\))?\d+:\d+)$', quote)
return m.group(1), m.group(3)
@app.route('/')
def index():
2022-03-30 07:14:42 -04:00
quote, author = get_quote(lang)
index = 'index_%s.html' % lang
2022-03-28 15:24:48 -04:00
2022-03-30 07:14:42 -04:00
return render_template(index, quote=quote, author=author)
2022-03-28 15:24:48 -04:00
2022-03-30 07:14:42 -04:00
@app.route('/<page>')
def page(page):
2022-03-28 15:24:48 -04:00
md = CONTENT_DIR + '%s_%s.md' % (page, lang)
2022-03-30 08:22:10 -04:00
html = 'page_%s.html' % lang
2022-03-28 15:24:48 -04:00
if os.path.isfile(md):
body = read_md(md)
2022-03-30 08:22:10 -04:00
return render_template(html, body=body)
2022-03-28 15:24:48 -04:00
else:
return not_found()
if __name__ == '__main__':
app.run(host='0.0.0.0')