Skip to content

Commit 9e8a387

Browse files
committed
Refactor find_tags_categories.py to use string parsing instead of YAML parsing
So installing YAML parsing library is not required.
1 parent 57a0a98 commit 9e8a387

1 file changed

Lines changed: 14 additions & 11 deletions

File tree

docs/find_tags_categories.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,31 @@
44
Looks for both 'tags' and 'categories' keys (common variations).
55
"""
66

7-
import os
8-
import yaml
97
from pathlib import Path
108
from typing import Set
119

1210
DOCS_DIR = Path(__file__).parent
1311
EXTENSIONS = (".md", ".markdown", ".mkd")
1412

1513
def extract_frontmatter(file_path: Path) -> dict:
16-
"""Extract YAML front matter if present."""
14+
"""Extract tags and categories from YAML front matter using string parsing."""
1715
content = file_path.read_text(encoding="utf-8")
1816
if not content.startswith("---"):
1917
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}")
18+
parts = content.split("---", 2)
19+
if len(parts) < 3:
2820
return {}
21+
result: dict[str, list[str]] = {"tags": [], "categories": []}
22+
current_key = None
23+
for line in parts[1].splitlines():
24+
stripped = line.strip()
25+
if stripped in ("tags:", "categories:"):
26+
current_key = stripped[:-1]
27+
elif current_key and stripped.startswith("- "):
28+
result[current_key].append(stripped[2:].strip())
29+
else:
30+
current_key = None
31+
return result
2932

3033
def collect_tags() -> tuple[Set[str], Set[str]]:
3134
all_tags: Set[str] = set()

0 commit comments

Comments
 (0)