Skip to content

Commit 6073e57

Browse files
committed
Add a script to harvest tags and categories. Initially by Grok, then fine-tuned.
1 parent 7700ed3 commit 6073e57

2 files changed

Lines changed: 88 additions & 7 deletions

File tree

create_post.sh

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#!/bin/bash
22

33
# Calculate the next Tuesday (or today if it's Tuesday)
4-
current_weekday=$(date +%w) # 0=Sun ... 6=Sat
5-
days_to_tuesday=$(( (2 - current_weekday + 7) % 7 ))
4+
current_weekday=$(date +%w) # 0=Sun ... 6=Sat
5+
days_to_tuesday=$(((2 - current_weekday + 7) % 7))
66

77
# Add the calculated days (compatible with both macOS and Linux)
88
if date -v +0d &>/dev/null; then
@@ -13,9 +13,10 @@ else
1313
tuesday=$(date -d "+${days_to_tuesday} days" +%Y-%m-%d)
1414
fi
1515

16-
year=${tuesday%%-*} # 2026
17-
month=${tuesday#*-}; month=${month%-*} # 02
18-
day=${tuesday##*-} # 24
16+
year=${tuesday%%-*} # 2026
17+
month=${tuesday#*-}
18+
month=${month%-*} # 02
19+
day=${tuesday##*-} # 24
1920

2021
# Define paths
2122
folder_path="docs/posts/$year/$month/$day"
@@ -25,7 +26,7 @@ file_path="$folder_path/index.md"
2526
mkdir -p "$folder_path"
2627

2728
# YAML frontmatter
28-
cat << EOF > "$file_path"
29+
cat <<EOF >"$file_path"
2930
---
3031
title: "Your Blog Post Title"
3132
date: $tuesday
@@ -50,4 +51,7 @@ EOF
5051

5152
echo "Blog post template created at $file_path"
5253
echo ""
53-
echo "Reminder: Use existing categories and tags when possible."
54+
echo "Reminder: Use existing categories and tags when possible."
55+
56+
python "./docs/find_tags_categories.py"
57+

docs/find_tags_categories.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Lists all unique tags and categories found in YAML front matter of .md files.
4+
Looks for both 'tags' and 'categories' keys (common variations).
5+
"""
6+
7+
import os
8+
import yaml
9+
from pathlib import Path
10+
from typing import Set
11+
12+
DOCS_DIR = Path(__file__).parent
13+
EXTENSIONS = (".md", ".markdown", ".mkd")
14+
15+
def extract_frontmatter(file_path: Path) -> dict:
16+
"""Extract YAML front matter if present."""
17+
content = file_path.read_text(encoding="utf-8")
18+
if not content.startswith("---"):
19+
return {}
20+
try:
21+
parts = content.split("---", 2)
22+
if len(parts) < 3:
23+
return {}
24+
fm = yaml.safe_load(parts[1]) or {}
25+
return fm if isinstance(fm, dict) else {}
26+
except yaml.YAMLError:
27+
print(f"Warning: Invalid YAML in {file_path}")
28+
return {}
29+
30+
def collect_tags() -> tuple[Set[str], Set[str]]:
31+
all_tags: Set[str] = set()
32+
all_categories: Set[str] = set()
33+
34+
docs_path = Path(DOCS_DIR)
35+
if not docs_path.is_dir():
36+
print(f"Error: Directory not found: {docs_path}")
37+
return all_tags, all_categories
38+
39+
for file_path in docs_path.rglob("*"):
40+
if not file_path.is_file() or not file_path.suffix.lower() in EXTENSIONS:
41+
continue
42+
43+
fm = extract_frontmatter(file_path)
44+
45+
# Handle 'tags'
46+
tags = fm.get("tags", [])
47+
if isinstance(tags, str):
48+
tags = [t.strip() for t in tags.split(",") if t.strip()]
49+
if isinstance(tags, list):
50+
all_tags.update(str(t).strip() for t in tags if t)
51+
52+
# Handle 'categories' (sometimes used instead / in addition)
53+
cats = fm.get("categories", [])
54+
if isinstance(cats, str):
55+
cats = [c.strip() for c in cats.split(",") if c.strip()]
56+
if isinstance(cats, list):
57+
all_categories.update(str(c).strip() for c in cats if c)
58+
59+
all_tags.discard('TAG_ONE')
60+
all_tags.discard('TAG_TWO')
61+
all_categories.discard('CATEGORY_ONE')
62+
all_categories.discard('CATEGORY_TWO')
63+
64+
return all_tags, all_categories
65+
66+
67+
def main():
68+
tags, categories = collect_tags()
69+
70+
print(f"\nExisting categories: {', '.join(sorted(categories))}", )
71+
print(f"\nExisting tags: {', '.join(sorted(tags))}", )
72+
73+
74+
if __name__ == "__main__":
75+
main()
76+
77+

0 commit comments

Comments
 (0)