From: Max Value Date: Mon, 16 Dec 2024 13:07:33 +0000 (+0000) Subject: Renamed blog.py and wrote setup script X-Git-Url: https://git.ozva.co.uk/?a=commitdiff_plain;h=6ac8a7ffa5a585159f322f2016d97fe86ba1da7e;p=blog Renamed blog.py and wrote setup script --- diff --git a/blog.py b/blog.py new file mode 100755 index 0000000..16ad6e5 --- /dev/null +++ b/blog.py @@ -0,0 +1,184 @@ +#! .venv/bin/python3 + +from flask import Flask, Response, request, render_template_string, redirect, send_from_directory +import markdown +from os import listdir, environ +from markdown.extensions.toc import TocExtension +import re +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, + title: str = "Unamed page", + abstract: str = "", + keywords: list = ["Undefined"], + date: str = "n.d.", + content: str = "", + template: str = "page.html" + ): + + self.title = title + self.abstract = abstract + 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() + if "abstract" in metadata: self.abstract = metadata["abstract"] + if "keywords" in metadata: self.keywords = [x.strip().title() for x in metadata["keywords"].split(",")] + if "date" in metadata: self.date = metadata["date"] + + def make(self): + format_keywords = " - ".join([f"{key}" for key in self.keywords]) + + 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, + keywords = format_keywords, + content = self.content + ) + + return Response(page, mimetype="text/html") + +@app.route("/") +def homepage(): + page = Page( + title = "Home", + abstract = "Blog and portfolio of Will Greenwood.", + keywords = [], + date = "", + template = "page.html" + ) + return page.make() + +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}'

" + + page = Page( + title = "Post search", + date = "", + keywords = [], + content = content + ) + return page.make() + +@app.route("/categories") +def get_categories(): + name = request.args.get("name") + index = {} + + if name == "all": + return redirect("/categories") + + for file_name in listdir("./pages"): + keywords = ["Undefined"] + title = "Untitled post" + abstract = "..." + with open(f"./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 + + 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: + index.update({key: [entry]}) + + content = "" + for c in index: + content += f"

{c}

" + + page = Page( + title = "Keyword search", + abstract = "Click on a keyword to see all pages tagged with that keyword.", + date = "", + keywords = [c.title() for c in index] + ["All"], + content = content + ) + return page.make() + +@app.route("/") +def generate_page(title): + title = title.replace("/", "") + try: + with open(f"pages/{title}.md", "r") as f: + html = md.convert(f.read()) + metadata = {k: v[0] for k, v in md.Meta.items()} + md.reset() + + page = Page(content = html) + page.from_metadata(metadata) + return page.make() + + except FileNotFoundError: + page = Page(title = "404", abstract = "Page not found!") + return page.make() + +@app.route("//") +def get_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/index.py b/index.py deleted file mode 100755 index 8625e37..0000000 --- a/index.py +++ /dev/null @@ -1,179 +0,0 @@ -#! .venv/bin/python3 - -from flask import Flask, Response, request, render_template, redirect, send_from_directory -import markdown -from os import listdir -from markdown.extensions.toc import TocExtension -import re -import mmap - -app = Flask(__name__) -md = markdown.Markdown(extensions = ['meta', 'tables', 'footnotes', TocExtension(title = "Table of contents")]) - -class Page(): - def __init__( - self, - title: str = "Unamed page", - abstract: str = "", - keywords: list = ["Undefined"], - date: str = "n.d.", - content: str = "", - template: str = "page.html" - ): - - self.title = title - self.abstract = abstract - self.keywords = keywords - self.date = date - self.content = content - - def from_metadata(self, metadata): - if "title" in metadata: self.title = metadata["title"].title() - if "abstract" in metadata: self.abstract = metadata["abstract"] - if "keywords" in metadata: self.keywords = [x.strip().title() for x in metadata["keywords"].split(",")] - if "date" in metadata: self.date = metadata["date"] - - def make(self): - format_keywords = " - ".join([f"{key}" for key in self.keywords]) - - page = render_template( - "page.html", - title = self.title, - date = self.date, - abstract = self.abstract, - keywords = format_keywords, - content = self.content - ) - - return Response(page, mimetype="text/html") - -@app.route("/") -def homepage(): - page = Page( - title = "Home", - abstract = "Blog and portfolio of Will Greenwood.", - keywords = [], - date = "", - template = "home.html" - ) - return page.make() - -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}'

" - - page = Page( - title = "Post search", - date = "", - keywords = [], - content = content - ) - return page.make() - -@app.route("/categories") -def get_categories(): - name = request.args.get("name") - index = {} - - if name == "all": - return redirect("/categories") - - for file_name in listdir("./pages"): - keywords = ["Undefined"] - title = "Untitled post" - abstract = "..." - with open(f"./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 - - 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: - index.update({key: [entry]}) - - content = "" - for c in index: - content += f"

{c}

" - - page = Page( - title = "Keyword search", - abstract = "Click on a keyword to see all pages tagged with that keyword.", - date = "", - keywords = [c.title() for c in index] + ["All"], - content = content - ) - return page.make() - -@app.route("/") -def generate_page(title): - title = title.replace("/", "") - try: - with open(f"pages/{title}.md", "r") as f: - html = md.convert(f.read()) - metadata = {k: v[0] for k, v in md.Meta.items()} - md.reset() - - page = Page(content = html) - page.from_metadata(metadata) - return page.make() - - except FileNotFoundError: - page = Page(title = "404", abstract = "Page not found!") - return page.make() - -@app.route("//") -def get_media(title, filename): - return send_from_directory(f"./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'], + )