From: Max Value Date: Mon, 16 Dec 2024 19:53:49 +0000 (+0000) Subject: Tweaks X-Git-Url: https://git.ozva.co.uk/?a=commitdiff_plain;h=3005b74cce1dc855e26afe4eae0a2ecb7fdedf05;p=blog Tweaks - Added justification on text body - Changed RWD width - Added latest posts on the sidebar - Added sidebar md document - Removed test page - Added bayeux audio file --- diff --git a/blog.py b/blog.py index 94e603b..27b5b07 100755 --- a/blog.py +++ b/blog.py @@ -3,6 +3,7 @@ from flask import Flask, Response, request, render_template_string, redirect, send_from_directory import markdown from os import listdir, environ +from os.path import getmtime from markdown.extensions.toc import TocExtension import re import mmap @@ -43,6 +44,10 @@ class Page(): template_string = open(f"{blog_path}/templates/{self.template}", "r").read() + recent = get_recent(10) + with open(f"{blog_path}/templates/sidebar.md", "r") as f: + sidebar = md.convert(f.read()) + page = render_template_string( template_string, title = self.title, @@ -50,11 +55,60 @@ class Page(): abstract = self.abstract, keywords = format_keywords, content = self.content, - filename = self.filename + filename = self.filename, + recent = recent, + sidebar = sidebar ) return Response(page, mimetype="text/html") +def get_plaintext(file_name: str): + page = open(f"{blog_path}/pages/{file_name}", "r").read() + return re.sub( + r"((?[\S ]*)|(``*)", + "", + page) + +def get_metadata(file_name: str): + title = "Untitled post" + abstract = "..." + keywords = ["Undefined"] + + with open(f"{blog_path}/pages/{file_name}", "r") as f: + while keywords == ["Undefined"]: + line = f.readline().split(":") # Split metdata into [key, value] + if len(line) == 2: # len != 2 when metadata block is over + key, value = line + match key: + case "title": + title = value.strip().title() + case "abstract": + abstract = value.strip() + case "keywords": + keywords = [x.strip() for x in value.split(",")] + else: + break + + return title, abstract, keywords + +def get_recent(n): + modification_times = {file_name: getmtime(f"{blog_path}/pages/{file_name}") for file_name in listdir(f"{blog_path}/pages")} + pages = sorted(modification_times.items(), key=lambda x: x[1])[:n] + + content = "" + + return content + @app.route("/") def homepage(): page = Page( @@ -66,41 +120,36 @@ def homepage(): ) return page.make() +@app.route("/search") def get_search(): - names = [re.sub('[\W_]+', '', name) for name in request.args.get("name").split()] - - name_string = "' '".join(names) - content = f"

Search '{name_string}'

" + search = request.args.get("name") + + results = 0 + content = f"

Search '{search}'

No results found

" + else: + content += f"

{results} results found

" page = Page( title = "Post search", @@ -119,37 +168,25 @@ def get_categories(): return redirect("/categories") for file_name in listdir(f"{blog_path}/pages"): - keywords = ["Undefined"] - title = "Untitled post" - abstract = "..." - with open(f"{blog_path}/pages/{file_name}", "r") as f: - while keywords == ["Undefined"]: - line = f.readline().split(":") - if len(line) == 2: - key, value = line - match key: - case "title": - title = value.strip().title() - case "abstract": - abstract = value.strip() - case "keywords": - keywords = [x.strip() for x in value.split(",")] - else: - break + title, abstract, keywords = get_metadata(file_name) entry = {"title": title, "abstract": abstract, "path": f"/{file_name[:-3]}"} for key in keywords: if key in index: index[key] += [entry] - elif key == name or name is None: + + elif name is None: + index.update({key: [entry]}) + + elif key.lower() == name.lower(): index.update({key: [entry]}) content = "" for c in index: - content += f"

{c}