commit 0398828c01d20b5d1abfb1f5fa78badc2aea7ed7 Author: veclav talica Date: Sun May 21 19:24:54 2023 +0500 init diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..6a957d5 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "tools/mmd"] + path = tools/mmd + url = https://github.com/fletcher/MultiMarkdown-6 diff --git a/compile.py b/compile.py new file mode 100755 index 0000000..10172d3 --- /dev/null +++ b/compile.py @@ -0,0 +1,14 @@ +#!/usr/bin/python3 + +from os import walk +import subprocess + +subprocess.run("./tools/main_page_generator.py ./articles | ./tools/mmd/build/multimarkdown > ./html/index.html", shell=True) +subprocess.run("mkdir -p ./html/articles", shell=True) + +for root, dirs, _ in walk("./articles"): + for d in dirs: + subprocess.run( + f"""./tools/article_wrapper.py ./articles/{d}/page.mmd | ./tools/mmd/build/multimarkdown > ./html/articles/{d}.html""", + shell=True + ) diff --git a/prepare.sh b/prepare.sh new file mode 100755 index 0000000..3ebf3f9 --- /dev/null +++ b/prepare.sh @@ -0,0 +1,9 @@ +#!/bin/sh + +git submodule init +git submodule update +cd tools/mmd +make release +cd build +make +cd ../.. diff --git a/tools/__pycache__/article_utils.cpython-38.pyc b/tools/__pycache__/article_utils.cpython-38.pyc new file mode 100644 index 0000000..cd254c6 Binary files /dev/null and b/tools/__pycache__/article_utils.cpython-38.pyc differ diff --git a/tools/article_utils.py b/tools/article_utils.py new file mode 100644 index 0000000..9903852 --- /dev/null +++ b/tools/article_utils.py @@ -0,0 +1,41 @@ +import time, subprocess + +MONTHS = { + 1: "January", + 2: "February", + 3: "March", + 4: "April", + 5: "May", + 6: "June", + 7: "July", + 8: "August", + 9: "September", + 10: "October", + 11: "November", + 12: "December" +} + +def the_line_after_metadata(lines: []) -> int: + i = 0 + while lines[i].strip(): + i += 1 + return i + +def parse_metadata(filepath: str) -> {}: + result = {} + with open(filepath, "r") as f: + content = f.readlines() + i = the_line_after_metadata(content) + + for line in content[:i]: + delim = line.find(":") + key, val = (line[:delim].strip(), line[delim+1:].strip()) + if key == "Date": + result["date"] = time.gmtime(int(val)) + elif key == "Tags": + result["tags"] = val.split(",") + else: + result[key.lower()] = val + result["last_edit"] = time.gmtime(int(subprocess.getoutput(r"stat -c %Y " + filepath))) + + return result diff --git a/tools/article_wrapper.py b/tools/article_wrapper.py new file mode 100755 index 0000000..f431f12 --- /dev/null +++ b/tools/article_wrapper.py @@ -0,0 +1,72 @@ +#!/usr/bin/python3 + +# todo: Show related git history of a file? + +from sys import argv, exit +import time + +from article_utils import the_line_after_metadata, parse_metadata, MONTHS + +if len(argv) <= 1: + print("No file was supplied") + exit(-1) + +with open(argv[1], "r") as f: + content = f.readlines() + i = the_line_after_metadata(content) + +metadata = parse_metadata(argv[1]) + +HEAD_EMBED = """ +
+ +
+ +## mjestečko ## + + + +
+ +
+""" + +TAIL_EMBED = """ +
+""" + +title = metadata.get("title", "Oopsie, somebody forgot to name the article!") +article_head = "\n# " + title + "\n" if title else \ + "# Oopsie, somebody forgot to give it a title!\n" + +brief = metadata.get("brief") +if not brief is None: + article_head += f""" +*{brief}* +""" + +date = metadata.get("date") +if not date is None: + article_head += f""" +-- Date: *{MONTHS[date.tm_mon]} {date.tm_mday}, {date.tm_year}* +""" + +last_edit = metadata.get("last_edit") +if not last_edit is None: + if date is None or last_edit.tm_mon != date.tm_mon or \ + last_edit.tm_mday != date.tm_mday or last_edit.tm_year != date.tm_year: + article_head += f""" +-- Edit: *{MONTHS[last_edit.tm_mon]} {last_edit.tm_mday}, {last_edit.tm_year}* +""" + +# todo: Hyperlinks to appropriate tag pages. +tags = metadata.get("tags") +if tags: + article_head += f""" +-- Tags: *{",".join(tags)}* +""" + +print(''.join(content[:i]) + HEAD_EMBED + article_head + ''.join(content[i:]) + TAIL_EMBED) diff --git a/tools/main_page_generator.py b/tools/main_page_generator.py new file mode 100755 index 0000000..cb5cdaf --- /dev/null +++ b/tools/main_page_generator.py @@ -0,0 +1,44 @@ +#!/usr/bin/python3 + +from sys import argv, exit +from os import walk, path +import time, urllib.parse + +from article_utils import parse_metadata, MONTHS + +if len(argv) <= 1: + print("No directory was supplied") + exit(-1) + +page = """Title: mjestečko +CSS: /style.css + +
+ +# mjestečko # + +Personal blog of one Veclav Talica. + +### Articles ### + +""" + +# todo: Sort by date first. +for root, dirs, _ in walk(argv[1]): + for d in dirs: + metadata = parse_metadata(path.abspath(root + '/' + d + "/page.mmd")) + article = urllib.parse.quote(d) + page += f"""#### [{metadata.get("title", "No title given! What a clusterfuck!")}](/articles/{article}.html) ####\n""" + page += f"""{metadata.get("brief", "")}\n\n""" + if "tags" in metadata: + page += f"""*{','.join(metadata["tags"])}*\n\n""" + +curtime = time.gmtime(int(time.time())) + +page += f""" +Last compiled: *{MONTHS[curtime.tm_mon]} {curtime.tm_mday}, {curtime.tm_year} {curtime.tm_hour}:{curtime.tm_min}* + +
+""" + +print(page) diff --git a/tools/mmd b/tools/mmd new file mode 160000 index 0000000..2818278 --- /dev/null +++ b/tools/mmd @@ -0,0 +1 @@ +Subproject commit 281827895db0190d41bda3c1d29a012b63dfd09b