Use the configured pages from config.json
[wiki] / app.py
1 import json
2 import pathlib
3
4 import commonmark
5 import flask
6
7 with open(pathlib.Path(__file__).parent / 'config.json') as f:
8     CONFIGURATION = json.loads(f.read())
9
10 app = flask.Flask(__name__)
11
12 @app.route('/')
13 def index():
14     return 'Hello, world'
15
16 @app.route('/p/<name>')
17 def page(name):
18     for ch in name:
19         if not ch in 'abcdefghijklmnopqrstuvwxyz-0123456789':
20             flask.abort(404)
21
22     try:
23         with open(pathlib.Path(CONFIGURATION['directory']) / '{}.md'.format(name), 'r') as f:
24             content = commonmark.commonmark(f.read())
25     except FileNotFoundError as e:
26         flask.abort(404)
27
28     title = name.replace('-', ' ').title()
29
30     return flask.render_template('page.html', content=content, title=title)
31
32