From ead4c9168f474ebf617d0d42b63ba457ef8c0f90 Mon Sep 17 00:00:00 2001 From: will Date: Tue, 22 Oct 2024 14:10:03 +0100 Subject: [PATCH] inital --- .gitignore | 1 + index.py | 148 ++++++++++++++++++++++++++++++++++++++++++++++++++ pages/test.md | 22 ++++++++ script.js | 23 ++++++++ style.css | 0 5 files changed, 194 insertions(+) create mode 100644 .gitignore create mode 100755 index.py create mode 100644 pages/test.md create mode 100644 script.js create mode 100644 style.css diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..21d0b89 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.venv/ diff --git a/index.py b/index.py new file mode 100755 index 0000000..32d1af6 --- /dev/null +++ b/index.py @@ -0,0 +1,148 @@ +#! .venv/bin/python3 + +from flask import Flask, Response, request +import markdown +from os import listdir +from markdown.extensions.toc import TocExtension + +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 = "" + ): + + 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"] + 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 = f""" + + + + {self.title} + + + + +

{self.title}

+

{self.date}

+

Abstract: {self.abstract}

+

Keywords: {format_keywords}

+
+
+ {self.content} +
+

+ © William Greenwood 2024 +

+ + + """ + + return Response(page, mimetype="text/html") + +@app.route("/") +def homepage(): + return "Hello, World!" + +@app.route("/.") +def get_static(title, extension): + title = title.replace("/", "") + + match extension: + case "css": path, mime = "/", "text/css" + case "js": path, mime = "/", "text/javascript" + case "png": path, mime = "/media/", "image/png" + case _: path = mime = "" + + try: + with open(f"{path}/{title}.{extension}", "r") as f: + return Response(f.read, minetype=mime) + + except FileNotFoundError: + return "" + +@app.route("/categories") +def get_categories(): + name = request.args.get('name') + index = {} + + 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 "categories": + 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 = "Keywords", + keywords = [c.title() for c in index], + 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() + +if __name__ == "__main__": + app.run(host='127.0.0.1', port=5000) diff --git a/pages/test.md b/pages/test.md new file mode 100644 index 0000000..8ccb136 --- /dev/null +++ b/pages/test.md @@ -0,0 +1,22 @@ +title: this is the title of the file +abstract: this is a subtitle +keywords: testing,page,structural +date: 22/10/2024 + +[TOC] + +# This is some test markdown! + +Body text! + +* this +* is +* a +* list[^1] + + +## This is some test markdown! + +# This is some test markdown! + +[^1]: And this is a footnote! diff --git a/script.js b/script.js new file mode 100644 index 0000000..a1c241e --- /dev/null +++ b/script.js @@ -0,0 +1,23 @@ +const links = document.getElementsByTagName('a'); + +for (anchor of links) { + anchor.addEventListener('mouseover', event => { + const href = event.target.href; + + const link = document.createElement('link'); + link.rel = 'prefetch'; + link.href = href; + + document.head.appendChild(link); + }); + + anchor.addEventListener('touchstart', event => { + const href = event.target.href; + + const link = document.createElement('link'); + link.rel = 'prefetch'; + link.href = href; + + document.head.appendChild(link); + }); +} diff --git a/style.css b/style.css new file mode 100644 index 0000000..e69de29 -- 2.39.2