#!/usr/bin/env python3
"""List all unique tags across Hugo content, sorted alphabetically."""

import re
from pathlib import Path

content_dir = Path(__file__).resolve().parent.parent / "content"
tags: set[str] = set()

for md in content_dir.rglob("*.md"):
    text = md.read_text(encoding="utf-8")
    fm = re.match(r"^---\s*\n(.*?)\n---\s*\n", text, re.DOTALL)
    if not fm:
        continue
    inline = re.search(r"^tags:\s*\[([^\]]*)\]", fm.group(1), re.MULTILINE)
    if inline and inline.group(1).strip():
        for t in inline.group(1).split(","):
            t = t.strip().strip("\"'").lower()
            if t:
                tags.add(t)
    else:
        lm = re.search(
            r"^tags:\s*\n((?:\s+-\s+.+\n?)+)", fm.group(1), re.MULTILINE
        )
        if lm:
            for t in re.findall(r"^\s+-\s+(.*)", lm.group(1), re.MULTILINE):
                t = t.strip().strip("\"'").lower()
                if t:
                    tags.add(t)

for t in sorted(tags):
    print(t)
