-
Notifications
You must be signed in to change notification settings - Fork 0
Add categories and tags placeholders in create post scripts #90
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
c1cc945
602c2f0
5a5618e
e768f37
7700ed3
6073e57
57a0a98
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Uh oh, my editor "cleaned up" the whitespace. I only added the last two lines. I restored the file, and re-added my changes. |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mrbiggred not sure if it's bad form to add a file to a PR. This script mines the existing documents to pull out the tags and categories, so they can be displayed in the "create_post" scripts. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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() | ||
|
|
||
|
|
Uh oh!
There was an error while loading. Please reload this page.