From 6a7a5f091ceca47fef7b1e5ea552296e59f9c0e1 Mon Sep 17 00:00:00 2001 From: veclav talica Date: Sat, 24 Feb 2024 11:33:57 +0500 Subject: [PATCH] article listing in plaintext --- compile.sh | 2 ++ tools/article_utils.py | 17 +++++++++++++++++ tools/main_page_generator.py | 17 ++++------------- tools/plaintext_article_listing_generator.py | 15 +++++++++++++++ 4 files changed, 38 insertions(+), 13 deletions(-) create mode 100755 tools/plaintext_article_listing_generator.py diff --git a/compile.sh b/compile.sh index 3a9df33..7065d38 100755 --- a/compile.sh +++ b/compile.sh @@ -37,6 +37,8 @@ done ./tools/feed_generator.py ./articles/ $URL > ./html/feed.xml +./tools/plaintext_article_listing_generator.py ./articles/ > ./html/articles.txt + mkdir -p "./html/markdown/" for d in ./articles/*/; do if [ -d "$d" ]; then diff --git a/tools/article_utils.py b/tools/article_utils.py index f2c730b..9213076 100644 --- a/tools/article_utils.py +++ b/tools/article_utils.py @@ -1,4 +1,6 @@ import time, subprocess +from os import walk, path +import urllib.parse def the_line_after_metadata(lines: []) -> int: i = 0 @@ -24,3 +26,18 @@ def parse_metadata(filepath: str) -> {}: result["Last Edit"] = time.gmtime(int(subprocess.getoutput(r"stat -c %Y " + filepath))) return result + +def parse_article_directory(directory: str) -> {}: + articles = {} + for root, dirs, _ in walk(directory): + for d in dirs: + metadata = parse_metadata(path.abspath(root + '/' + d + "/page.mmd")) + article = urllib.parse.quote(d) + articles[article] = { + "metadata": metadata + } + break + return articles + +def sort_titles_by_date(articles: {}) -> []: + return sorted(articles.keys(), key=lambda a: articles[a]["metadata"].get("Date", time.gmtime(0)), reverse=True) diff --git a/tools/main_page_generator.py b/tools/main_page_generator.py index 6cf1b51..e165dc2 100755 --- a/tools/main_page_generator.py +++ b/tools/main_page_generator.py @@ -1,11 +1,10 @@ #!/usr/bin/python3 from sys import argv, exit -from os import walk, path from random import choice, seed -import time, urllib.parse +import time -from article_utils import parse_metadata +from article_utils import parse_article_directory, sort_titles_by_date from page_shares import wrap_page, ADJECTIVES, MONTHS if len(argv) <= 1: @@ -28,17 +27,9 @@ Personal blog of one {choice(ADJECTIVES)} Veclav Talica. """ -artciles = {} -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) - artciles[article] = { - "metadata": metadata - } - break +artciles = parse_article_directory(argv[1]) -for title in sorted(artciles.keys(), key=lambda a: artciles[a]["metadata"].get("Date", time.gmtime(0)), reverse=True): +for title in sort_titles_by_date(artciles): article = artciles[title] metadata = article["metadata"] page += ( diff --git a/tools/plaintext_article_listing_generator.py b/tools/plaintext_article_listing_generator.py new file mode 100755 index 0000000..c6eb98b --- /dev/null +++ b/tools/plaintext_article_listing_generator.py @@ -0,0 +1,15 @@ +#!/usr/bin/env python3 + +from sys import argv, exit + +from article_utils import parse_article_directory, sort_titles_by_date + +if len(argv) <= 1: + print("No directory was supplied") + exit(-1) + +articles = parse_article_directory(argv[1]) + +result = '\n'.join(sort_titles_by_date(articles)) + +print(result)