From c36ce7dd2055e23781b3e5c830f80f87e7ad0552 Mon Sep 17 00:00:00 2001 From: veclav talica Date: Fri, 16 Feb 2024 18:29:50 +0500 Subject: [PATCH] tag listings --- compile.sh | 7 +++++ tools/article_utils.py | 2 +- tools/article_wrapper.py | 8 +++-- tools/page_shares.py | 1 + tools/tag_listing_generator.py | 54 ++++++++++++++++++++++++++++++++++ 5 files changed, 68 insertions(+), 4 deletions(-) create mode 100755 tools/tag_listing_generator.py diff --git a/compile.sh b/compile.sh index 20fb9ed..25ebe00 100755 --- a/compile.sh +++ b/compile.sh @@ -28,4 +28,11 @@ for d in ./articles/*/; do fi done +mkdir -p "./html/tags/" +./tools/tag_listing_generator.py ./articles/ ./html/ | ./tools/mmd/build/multimarkdown > "./html/tags.html" + +for f in ./html/tags/*.html; do + echo $(cat "$f" | ./tools/mmd/build/multimarkdown) > "$f" +done + ./tools/feed_generator.py ./articles/ $URL > ./html/feed.xml diff --git a/tools/article_utils.py b/tools/article_utils.py index 86d4e41..f00b20a 100644 --- a/tools/article_utils.py +++ b/tools/article_utils.py @@ -18,7 +18,7 @@ def parse_metadata(filepath: str) -> {}: if key == "Date": result["Date"] = time.gmtime(int(val)) elif key == "Tags": - result["Tags"] = val.split(",") + result["Tags"] = [v.strip() for v in val.split(",")] else: result[key] = val result["Last Edit"] = time.gmtime(int(subprocess.getoutput(r"stat -c %Y " + filepath))) diff --git a/tools/article_wrapper.py b/tools/article_wrapper.py index 758f0c8..093aef9 100755 --- a/tools/article_wrapper.py +++ b/tools/article_wrapper.py @@ -24,7 +24,7 @@ with open(argv[1], "r") as f: metadata = parse_metadata(argv[1]) directory = path.split(path.dirname(path.abspath(argv[1])))[-1] -title = metadata.get("Title", "Oopsie, somebody forgot to name the article!") +title = metadata["Title"] article_head = "\n# " + title + "\n" brief = metadata.get("Brief") @@ -41,10 +41,12 @@ if not last_edit is None: last_edit.tm_mday != date.tm_mday or last_edit.tm_year != date.tm_year: article_head += f"-- Edited: *{MONTHS[last_edit.tm_mon]} {last_edit.tm_mday}, {last_edit.tm_year} UTC*\n\n" -# todo: Hyperlinks to appropriate tag pages. tags = metadata.get("Tags") if tags: - article_head += f"""-- Tags: *{",".join(tags)}*\n\n""" + tag_links = [] + for tag in tags: + tag_links.append(f"[{tag}](/tags/{urllib.parse.quote(tag.lower())}.html)") + article_head += f"""-- Tags: *{", ".join(tag_links)}*\n\n""" article_head += "---\n\n" diff --git a/tools/page_shares.py b/tools/page_shares.py index d2e14a8..c651876 100644 --- a/tools/page_shares.py +++ b/tools/page_shares.py @@ -10,6 +10,7 @@ HEAD_EMBED = """
  • about
  • tracks
  • mastodon
  • +
  • tags
  • diff --git a/tools/tag_listing_generator.py b/tools/tag_listing_generator.py new file mode 100755 index 0000000..82d1cbe --- /dev/null +++ b/tools/tag_listing_generator.py @@ -0,0 +1,54 @@ +#!/usr/bin/env python3 + +# todo: Problems might arise with casing if there are overlaps such as 'Tag' and 'tag'. + +from sys import argv, exit +import time, urllib.parse, re +from os import walk +import os.path as path + +from article_utils import the_line_after_metadata, parse_metadata +from page_shares import wrap_page + +tag_listing_header = "CSS: /style.css\n\n" +main_listing_header = "CSS: /style.css\n\n" + +if len(argv) <= 1: + print("No article directory was supplied") + exit(-1) + +if len(argv) <= 2: + print("No tag listing output directory was supplied") + exit(-1) + +tag_to_tag_page = {} +tag_to_articles = {} +tag_counts = {} +article_to_title = {} + +for root, dirs, _ in walk(argv[1]): + for d in dirs: + metadata = parse_metadata(path.abspath(root + '/' + d + "/page.mmd")) + article = "/articles/" + urllib.parse.quote(d) + ".html" + for tag in metadata.get('Tags', []): + tag_to_articles[tag] = tag_to_articles.get(tag, []) + [article] + tag_counts[tag] = tag_counts.get(tag, 0) + 1 + article_to_title[article] = metadata['Title'] + break + +for tag in tag_to_articles: + tag_page = f"/tags/{urllib.parse.quote(tag.lower())}.html" + tag_to_tag_page[tag] = tag_page + + with open(argv[2] + tag_page, 'w') as f: + tagged_article_listing = f"\n# Tagged {tag} #\n---\n" + \ + '\n'.join(f"- [{article_to_title[article]}]({article})" \ + for article in tag_to_articles[tag]) + f.write(tag_listing_header + wrap_page(tagged_article_listing)) + +main_listing = "\n# Tag Listing #\n---\n" + \ + ', '.join(f"[{tag}]({tag_to_tag_page[tag]}) ({tag_counts[tag]})" \ + for tag in sorted(tag_counts.keys(), key=lambda x: tag_counts[x], reverse=True)) + \ + '\n\n' + +print(main_listing_header + wrap_page(main_listing))