diff options
| author | Yuval Adam <_@yuv.al> | 2017-08-07 08:55:33 +0300 |
|---|---|---|
| committer | Yuval Adam <_@yuv.al> | 2017-08-07 08:57:40 +0300 |
| commit | 14958653b9bf92f56316511e8080baeeaf409b97 (patch) | |
| tree | 65503639105fa6074bcbeb11164cc82a70828c4e /app.py | |
Basic structure and initial homepage
Diffstat (limited to 'app.py')
| -rw-r--r-- | app.py | 35 |
1 files changed, 35 insertions, 0 deletions
@@ -0,0 +1,35 @@ +import markdown + +from flask import Flask, render_template +from os import listdir + +app = Flask(__name__) + +BLOG_CONTENT_DIR = 'content/blog' + +@app.route('/') +def home(): + return render_template('index.html') + +def parse_post(post): + y, m, d, *title = post[:-3].split('-') + slug = '-'.join(title) + return {'url': f'/blog/{y}/{m}/{d}/{slug}/', 'title': ' '.join(title)} + +@app.route('/blog/') +def blog(): + posts = sorted(listdir(BLOG_CONTENT_DIR), reverse=True) + posts = map(parse_post, posts) + return render_template('blog.html', posts=posts) + +@app.route('/blog/<y>/<m>/<d>/<title>/') +def blog_post(y, m, d, title): + post_path = f'{y}-{m}-{d}-{title}' + with open(f'{BLOG_CONTENT_DIR}/{post_path}.md', 'r') as f: + text = f.read() + _, header, body = text.split('---') + for h in header: + if h.startswith('title'): + title = h.split('title:')[0].strip() + html = markdown.markdown(body, extensions=['markdown.extensions.extra', 'markdown.extensions.codehilite']) + return render_template('post.html', title=title, html=html) |
