blog/app/__init__.py

102 lines
2.9 KiB
Python
Raw Normal View History

2021-01-24 20:39:27 +01:00
import datetime
import os
import re
2021-01-24 00:55:49 +01:00
2021-01-24 23:24:49 +01:00
from glob import glob
from typing import Optional
2021-01-24 00:55:49 +01:00
2021-01-24 20:39:27 +01:00
from flask import Flask, abort, send_from_directory, render_template
2021-01-24 23:24:49 +01:00
from markdown import markdown
2021-01-24 00:55:49 +01:00
2021-01-24 20:39:27 +01:00
basedir = os.path.abspath(os.path.join(os.path.realpath(__file__), '..', '..'))
templates_dir = os.path.join(basedir, 'templates')
static_dir = os.path.join(basedir, 'static')
pages_dir = os.path.join(static_dir, 'pages')
img_dir = os.path.join(static_dir, 'img')
css_dir = os.path.join(static_dir, 'css')
app = Flask(__name__, template_folder=templates_dir)
def parse_page_title(page: str) -> str:
if page.endswith('.md'):
page = page[:-3]
return page.replace('-', ' ')
def get_page_metadata(page: str) -> dict:
if not page.endswith('.md'):
page = page + '.md'
if not os.path.isfile(os.path.join(pages_dir, page)):
abort(404)
metadata = {}
with open(os.path.join(pages_dir, page), 'r') as f:
2021-01-24 23:24:49 +01:00
metadata['uri'] = '/article/' + page[:-3]
2021-01-24 20:39:27 +01:00
for line in f.readlines():
if not line:
continue
if not (m := re.match(r'^\[//]: # \(([^:]+):\s*([^)]+)\)\s*$', line)):
break
2021-01-24 23:24:49 +01:00
if m.group(1) == 'published':
metadata[m.group(1)] = datetime.date.fromisoformat(m.group(2))
else:
metadata[m.group(1)] = m.group(2)
2021-01-24 20:39:27 +01:00
return metadata
def get_page(page: str, title: Optional[str] = None):
if not page.endswith('.md'):
page = page + '.md'
metadata = get_page_metadata(page)
with open(os.path.join(pages_dir, page), 'r') as f:
return render_template('article.html',
2021-01-24 23:24:49 +01:00
title=title if title else metadata.get('title', 'Platypush blog'),
image=metadata.get('image'),
description=metadata.get('description'),
published=(metadata['published'].strftime('%b %d, %Y')
2021-01-24 20:39:27 +01:00
if metadata.get('published') else None),
content=markdown(f.read(),extensions=['fenced_code', 'codehilite']))
2021-01-24 23:24:49 +01:00
def get_pages() -> list:
return sorted([
{
'path': path,
**get_page_metadata(os.path.basename(path)),
}
for path in glob(os.path.join(pages_dir, '*.md'))
], key=lambda page: page.get('published'), reverse=True)
2021-01-24 20:39:27 +01:00
@app.route('/', methods=['GET'])
def home_route():
2021-01-24 23:24:49 +01:00
return render_template('index.html', pages=get_pages())
2021-01-24 20:39:27 +01:00
@app.route('/favicon.ico', methods=['GET'])
def favicon_route():
return send_from_directory(img_dir, 'favicon.ico')
@app.route('/img/<img>', methods=['GET'])
def img_route(img: str):
return send_from_directory(img_dir, img)
@app.route('/css/<style>', methods=['GET'])
def css_route(style: str):
return send_from_directory(css_dir, style)
2021-01-24 23:24:49 +01:00
@app.route('/article/<article>', methods=['GET'])
def article_route(article: str):
return get_page(article)