diff --git a/README.md b/README.md index 3fcca455..21619ac9 100644 --- a/README.md +++ b/README.md @@ -97,9 +97,11 @@ These scripts will: authors: - chris categories: - - Category Name + - CategoryOne + - CategoryTwo tags: - - relevant-tag + - relevant-tag-one + - relevant-tag-two --- ``` diff --git a/create_post.ps1 b/create_post.ps1 index 32455617..30448a9b 100644 --- a/create_post.ps1 +++ b/create_post.ps1 @@ -43,6 +43,14 @@ title: "$title" date: $date authors: - chris | norm | omar +categories: + # use existing categories when possible, in YAML list format. + - CATEGORY_ONE + - CATEGORY_TWO +tags: + # use existing tags when possible, in YAML list format. + - TAG_ONE + - TAG_TWO --- TOPIC_INTRODUCTION_HERE @@ -55,4 +63,6 @@ Everyone and anyone are welcome to [join](https://weeklydevchat.com/join/) as lo # Write the content to the file Set-Content -Path $filePath -Value $yamlContent -Write-Output "Blog post template created at $filePath" \ No newline at end of file +Write-Output "Blog post template created at $filePath" +Write-Output "" +Write-Output "Reminder: Use existing categories and tags when possible." \ No newline at end of file diff --git a/create_post.sh b/create_post.sh index 596ee4ac..a0777946 100644 --- a/create_post.sh +++ b/create_post.sh @@ -1,17 +1,21 @@ #!/bin/bash # Calculate the next Tuesday (or today if it's Tuesday) -# In Linux date, weekday: 0=Sunday, 1=Monday, ..., 6=Saturday current_weekday=$(date +%w) # 0=Sun ... 6=Sat days_to_tuesday=$(( (2 - current_weekday + 7) % 7 )) -# If today is Tuesday, days_to_tuesday will be 0 → perfect -# Add the calculated days -tuesday=$(date -d "+${days_to_tuesday} days" +%Y-%m-%d) +# Add the calculated days (compatible with both macOS and Linux) +if date -v +0d &>/dev/null; then + # macOS (BSD date) + tuesday=$(date -v "+${days_to_tuesday}d" +%Y-%m-%d) +else + # Linux (GNU date) + tuesday=$(date -d "+${days_to_tuesday} days" +%Y-%m-%d) +fi -year=$(date -d "$tuesday" +%Y) -month=$(date -d "$tuesday" +%02m) -day=$(date -d "$tuesday" +%02d) +year=${tuesday%%-*} # 2026 +month=${tuesday#*-}; month=${month%-*} # 02 +day=${tuesday##*-} # 24 # Define paths folder_path="docs/posts/$year/$month/$day" @@ -27,6 +31,14 @@ title: "Your Blog Post Title" date: $tuesday authors: - chris | norm | omar +categories: + # use existing categories when possible, in YAML list format. + - CATEGORY_ONE + - CATEGORY_TWO +tags: + # use existing tags when possible, in YAML list format. + - TAG_ONE + - TAG_TWO --- TOPIC_INTRODUCTION_HERE @@ -36,4 +48,8 @@ Everyone and anyone are welcome to [join](https://weeklydevchat.com/join/) as lo ![alt text](${tuesday}_image_filename.webp) EOF -echo "Blog post template created at $file_path" \ No newline at end of file +echo "Blog post template created at $file_path" +echo "" +echo "Reminder: Use existing categories and tags when possible." + +python "./docs/find_tags_categories.py" diff --git a/docs/find_tags_categories.py b/docs/find_tags_categories.py new file mode 100644 index 00000000..fdf3cb22 --- /dev/null +++ b/docs/find_tags_categories.py @@ -0,0 +1,77 @@ +#!/usr/bin/env python3 +""" +Lists all unique tags and categories found in YAML front matter of .md files. +Looks for both 'tags' and 'categories' keys (common variations). +""" + +import os +import yaml +from pathlib import Path +from typing import Set + +DOCS_DIR = Path(__file__).parent +EXTENSIONS = (".md", ".markdown", ".mkd") + +def extract_frontmatter(file_path: Path) -> dict: + """Extract YAML front matter if present.""" + content = file_path.read_text(encoding="utf-8") + if not content.startswith("---"): + return {} + try: + parts = content.split("---", 2) + if len(parts) < 3: + return {} + fm = yaml.safe_load(parts[1]) or {} + return fm if isinstance(fm, dict) else {} + except yaml.YAMLError: + print(f"Warning: Invalid YAML in {file_path}") + return {} + +def collect_tags() -> tuple[Set[str], Set[str]]: + all_tags: Set[str] = set() + all_categories: Set[str] = set() + + docs_path = Path(DOCS_DIR) + if not docs_path.is_dir(): + print(f"Error: Directory not found: {docs_path}") + return all_tags, all_categories + + for file_path in docs_path.rglob("*"): + if not file_path.is_file() or not file_path.suffix.lower() in EXTENSIONS: + continue + + fm = extract_frontmatter(file_path) + + # Handle 'tags' + tags = fm.get("tags", []) + if isinstance(tags, str): + tags = [t.strip() for t in tags.split(",") if t.strip()] + if isinstance(tags, list): + all_tags.update(str(t).strip() for t in tags if t) + + # Handle 'categories' (sometimes used instead / in addition) + cats = fm.get("categories", []) + if isinstance(cats, str): + cats = [c.strip() for c in cats.split(",") if c.strip()] + if isinstance(cats, list): + all_categories.update(str(c).strip() for c in cats if c) + + all_tags.discard('TAG_ONE') + all_tags.discard('TAG_TWO') + all_categories.discard('CATEGORY_ONE') + all_categories.discard('CATEGORY_TWO') + + return all_tags, all_categories + + +def main(): + tags, categories = collect_tags() + + print(f"\nExisting categories: {', '.join(sorted(categories))}", ) + print(f"\nExisting tags: {', '.join(sorted(tags))}", ) + + +if __name__ == "__main__": + main() + +