From 0398828c01d20b5d1abfb1f5fa78badc2aea7ed7 Mon Sep 17 00:00:00 2001 From: veclav talica Date: Sun, 21 May 2023 19:24:54 +0500 Subject: [PATCH] init --- .gitmodules | 3 + compile.py | 14 ++++ prepare.sh | 9 +++ .../__pycache__/article_utils.cpython-38.pyc | Bin 0 -> 1165 bytes tools/article_utils.py | 41 ++++++++++ tools/article_wrapper.py | 72 ++++++++++++++++++ tools/main_page_generator.py | 44 +++++++++++ tools/mmd | 1 + 8 files changed, 184 insertions(+) create mode 100644 .gitmodules create mode 100755 compile.py create mode 100755 prepare.sh create mode 100644 tools/__pycache__/article_utils.cpython-38.pyc create mode 100644 tools/article_utils.py create mode 100755 tools/article_wrapper.py create mode 100755 tools/main_page_generator.py create mode 160000 tools/mmd 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 0000000000000000000000000000000000000000..cd254c636b7d0bcd37d53f87f4b7a68f6f6733a8 GIT binary patch literal 1165 zcmZuw&5ImG6tAlOo|)a9Y&Nb55*GY`29r6dD2o`fhy-?m*@)U8Lv6p>+0OQKcdDva zMruy0-bDX^ATvk*50Cl})Ya46^^%jXdW=M{n)j>s(eG8iSHJ#jYpX|ag}0u-?;at4 zIOFBDg~mrH>Jb7Epe4yRJIyFC@W6)vLWm%S1bWbi0c^qcvq8qDM0l|CjKL7Ded}kw z2nqtbXkUjtv_qgLB)zf5x@4S14|vsbInUyc#i{JH;F!z&JPYnMvM4j}n9no+L0buj z0nLqq&0JZoszZhAaD>mh#bTY z5(jQ9>%M~lLdtAV7L`yoltQ<%%3ZM+%hhypA4T0p0J@-t7C&akNvRE#mPL~?xsA4^#V92?hObFgSa_fbMkwvtKO$7c0li%xL z8S2QyS9C>joWUWf-oRLhv7b%6j3Mg$G5QB4Hq=DRWJM0iCQtX4eMk)1CoB3-h29}~ z`fhg}{jsI;IsKr!kIX;fT38=Pg-ms6Y;JURa=*2^&sl-R>2Y#%piN0=KgMT1eYd?P(+HTqu<# zQ=#itH?2++dBde6>r`86OV6?>uPZGoEw6QT-Emm}qAX_CyAX5hUGlQy&jzl$x3(KD zmH3weucA-AhOnVPOn2E3iSQPU=^o0E4d`tqQ;esPbbOYtBiQix;rQ_M*sKuGu#=5e*?Bm8nplb literal 0 HcmV?d00001 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