From 6ac8a7ffa5a585159f322f2016d97fe86ba1da7e Mon Sep 17 00:00:00 2001 From: Max Value Date: Mon, 16 Dec 2024 13:07:33 +0000 Subject: [PATCH] Renamed blog.py and wrote setup script --- index.py => blog.py | 17 +++++++++++------ setup.py | 5 +++++ 2 files changed, 16 insertions(+), 6 deletions(-) rename index.py => blog.py (91%) create mode 100644 setup.py diff --git a/index.py b/blog.py similarity index 91% rename from index.py rename to blog.py index 8625e37..16ad6e5 100755 --- a/index.py +++ b/blog.py @@ -1,8 +1,8 @@ #! .venv/bin/python3 -from flask import Flask, Response, request, render_template, redirect, send_from_directory +from flask import Flask, Response, request, render_template_string, redirect, send_from_directory import markdown -from os import listdir +from os import listdir, environ from markdown.extensions.toc import TocExtension import re import mmap @@ -10,6 +10,8 @@ import mmap app = Flask(__name__) md = markdown.Markdown(extensions = ['meta', 'tables', 'footnotes', TocExtension(title = "Table of contents")]) +blog_path = environ["BLOG_PATH"] + class Page(): def __init__( self, @@ -26,6 +28,7 @@ class Page(): self.keywords = keywords self.date = date self.content = content + self.template = template def from_metadata(self, metadata): if "title" in metadata: self.title = metadata["title"].title() @@ -36,8 +39,10 @@ class Page(): def make(self): format_keywords = " - ".join([f"{key}" for key in self.keywords]) - page = render_template( - "page.html", + template_string = open(f"{blog_path}/templates/{self.template}", "r").read() + + page = render_template_string( + template_string, title = self.title, date = self.date, abstract = self.abstract, @@ -54,7 +59,7 @@ def homepage(): abstract = "Blog and portfolio of Will Greenwood.", keywords = [], date = "", - template = "home.html" + template = "page.html" ) return page.make() @@ -173,7 +178,7 @@ def generate_page(title): @app.route("//") def get_media(title, filename): - return send_from_directory(f"./media/{title}", filename) + return send_from_directory(f"{blog_path}/media/{title}", filename) if __name__ == "__main__": app.run(host='127.0.0.1', port=5000, debug=True) diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..3c614d6 --- /dev/null +++ b/setup.py @@ -0,0 +1,5 @@ +from distutils.core import setup +setup(name='blog', + version='1.0', + py_modules=['blog'], + ) -- 2.39.2