]> OzVa Git service - blog/commitdiff
Renamed blog.py and wrote setup script
authorMax Value <greenwoodw50@gmail.com>
Mon, 16 Dec 2024 13:07:33 +0000 (13:07 +0000)
committerMax Value <greenwoodw50@gmail.com>
Mon, 16 Dec 2024 13:07:33 +0000 (13:07 +0000)
blog.py [new file with mode: 0755]
index.py [deleted file]
setup.py [new file with mode: 0644]

diff --git a/blog.py b/blog.py
new file mode 100755 (executable)
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"<a href='/categories?name={key.lower()}'>{key}</a>" 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 = "</em>' '<em>".join(names)
+       content = f"<h1>Search '<em>{name_string}</em>'</h1><ul>"
+
+       for file_name in listdir("./pages"):
+               with open(f"./pages/{file_name}", "r") as f:
+                       s = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ)
+                       for name in names:
+                               i = s.find(str.encode(name))
+                               if i != -1:
+                                       post_content = f.read()
+
+                                       post_content = re.sub('<.*>', ' ', post_content)
+                                       post_content = re.sub('.[^\s\d\w]', ' ', post_content)
+                                       post_content = re.sub('  ', ' ', post_content)
+
+                                       i = post_content.find(name)
+                                       excerpt = "'..." + post_content[i-50:i] + "</em><b>" + post_content[i:i+len(name)] + "</b><em>" + post_content[i+len(name):i+50] + "...'"
+
+                                       lines = post_content.split("\n")
+                                       title = "Unamed page"
+                                       for line in lines:
+                                               line = line.split()
+                                               print(line)
+                                               if len(line) == 2 and line[0] == "title":
+                                                       title = line[1].strip().title()
+                                               elif len(line) == 2: pass
+                                               else: break
+
+                                       content += f"<li><a href='/{file_name[:-3]}'>{title}</a> - <em>{excerpt}</em></li>"
+                                       break
+
+       content += "</ul>"
+
+       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"<h1>{c}</h1><ul>"
+
+               for post in index[c]:
+                       content += f"<li><a href='{post['path']}'>{post['title']}</a> - <em>{abstract}</em></li>"
+
+               content += "</ul>"
+
+       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("/<string:title>")
+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("/<string:title>/<string:filename>")
+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 (executable)
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"<a href='/categories?name={key.lower()}'>{key}</a>" 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 = "</em>' '<em>".join(names)
-       content = f"<h1>Search '<em>{name_string}</em>'</h1><ul>"
-
-       for file_name in listdir("./pages"):
-               with open(f"./pages/{file_name}", "r") as f:
-                       s = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ)
-                       for name in names:
-                               i = s.find(str.encode(name))
-                               if i != -1:
-                                       post_content = f.read()
-
-                                       post_content = re.sub('<.*>', ' ', post_content)
-                                       post_content = re.sub('.[^\s\d\w]', ' ', post_content)
-                                       post_content = re.sub('  ', ' ', post_content)
-
-                                       i = post_content.find(name)
-                                       excerpt = "'..." + post_content[i-50:i] + "</em><b>" + post_content[i:i+len(name)] + "</b><em>" + post_content[i+len(name):i+50] + "...'"
-
-                                       lines = post_content.split("\n")
-                                       title = "Unamed page"
-                                       for line in lines:
-                                               line = line.split()
-                                               print(line)
-                                               if len(line) == 2 and line[0] == "title":
-                                                       title = line[1].strip().title()
-                                               elif len(line) == 2: pass
-                                               else: break
-
-                                       content += f"<li><a href='/{file_name[:-3]}'>{title}</a> - <em>{excerpt}</em></li>"
-                                       break
-
-       content += "</ul>"
-
-       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"<h1>{c}</h1><ul>"
-
-               for post in index[c]:
-                       content += f"<li><a href='{post['path']}'>{post['title']}</a> - <em>{abstract}</em></li>"
-
-               content += "</ul>"
-
-       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("/<string:title>")
-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("/<string:title>/<string:filename>")
-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 (file)
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'],
+      )