From 4e599be1733be283b5c86aee8572284a67afbfa2 Mon Sep 17 00:00:00 2001 From: Johannes Thumshirn Date: Thu, 25 Feb 2021 15:38:37 +0100 Subject: [PATCH] app: add rendering of markdown Add rendering of markdown files stored in the pages directory using templates in the templates directory. Signed-off-by: Johannes Thumshirn --- app.py | 17 ++++++++++++++--- pages/blogpost.md | 6 ++++++ templates/bloghome.html | 9 +++++++++ templates/page.html | 6 ++++++ templates/template.html | 3 +++ 5 files changed, 38 insertions(+), 3 deletions(-) mode change 100644 => 100755 app.py create mode 100644 pages/blogpost.md create mode 100644 templates/bloghome.html create mode 100644 templates/page.html create mode 100644 templates/template.html diff --git a/app.py b/app.py old mode 100644 new mode 100755 index c58ea90..3567727 --- a/app.py +++ b/app.py @@ -5,10 +5,21 @@ from flask_flatpages import FlatPages from datetime import datetime app = Flask(__name__) +app.config['FLATPAGES_EXTENSION'] = '.md' -@app.route("/") -def hello_world(): - return 'Hello, World!' +pages = FlatPages(app) + +@app.route('/.html') +def page(path): + page = pages.get_or_404(path) + return render_template('page.html', page=page) + +@app.route('/') +def index(): + posts = [p for p in pages if "date" in p.meta] + sorted_pages = sorted(posts, reverse=True, key=lambda page: + datetime.strptime(page.meta["date"], "%d %b %y")) + return render_template('bloghome.html', pages=sorted_pages) if __name__ == '__main__': app.run(host='0.0.0.0') diff --git a/pages/blogpost.md b/pages/blogpost.md new file mode 100644 index 0000000..c1bcfa7 --- /dev/null +++ b/pages/blogpost.md @@ -0,0 +1,6 @@ +title: Post title goes here +date: 25 Feb 21 +author: Me +description: Hello World + +Hello World diff --git a/templates/bloghome.html b/templates/bloghome.html new file mode 100644 index 0000000..d9d7b38 --- /dev/null +++ b/templates/bloghome.html @@ -0,0 +1,9 @@ +{% extends "template.html" %} +{% block content %} + {% for page in pages %} +

{{ page.title }}

+

{{ page.description }}

+

{{ page.date }}

+
+ {% endfor %} +{% endblock %} diff --git a/templates/page.html b/templates/page.html new file mode 100644 index 0000000..74d52ec --- /dev/null +++ b/templates/page.html @@ -0,0 +1,6 @@ +{% extends "template.html" %} +{% block content %} +

{{ page.title }}

+

{{page.html|safe }}

+ {{ page.author }} {{ page.date }} +{% endblock %} diff --git a/templates/template.html b/templates/template.html new file mode 100644 index 0000000..10bf993 --- /dev/null +++ b/templates/template.html @@ -0,0 +1,3 @@ + +{% block content %} +{% endblock %}