|
4 | 4 | Looks for both 'tags' and 'categories' keys (common variations). |
5 | 5 | """ |
6 | 6 |
|
7 | | -import os |
8 | | -import yaml |
9 | 7 | from pathlib import Path |
10 | 8 | from typing import Set |
11 | 9 |
|
12 | 10 | DOCS_DIR = Path(__file__).parent |
13 | 11 | EXTENSIONS = (".md", ".markdown", ".mkd") |
14 | 12 |
|
15 | 13 | 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.""" |
17 | 15 | content = file_path.read_text(encoding="utf-8") |
18 | 16 | if not content.startswith("---"): |
19 | 17 | 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: |
28 | 20 | 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 |
29 | 32 |
|
30 | 33 | def collect_tags() -> tuple[Set[str], Set[str]]: |
31 | 34 | all_tags: Set[str] = set() |
|
0 commit comments