utils.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. """Shared utilities for skill-creator scripts."""
  2. from pathlib import Path
  3. def parse_skill_md(skill_path: Path) -> tuple[str, str, str]:
  4. """Parse a SKILL.md file, returning (name, description, full_content)."""
  5. content = (skill_path / "SKILL.md").read_text()
  6. lines = content.split("\n")
  7. if lines[0].strip() != "---":
  8. raise ValueError("SKILL.md missing frontmatter (no opening ---)")
  9. end_idx = None
  10. for i, line in enumerate(lines[1:], start=1):
  11. if line.strip() == "---":
  12. end_idx = i
  13. break
  14. if end_idx is None:
  15. raise ValueError("SKILL.md missing frontmatter (no closing ---)")
  16. name = ""
  17. description = ""
  18. frontmatter_lines = lines[1:end_idx]
  19. i = 0
  20. while i < len(frontmatter_lines):
  21. line = frontmatter_lines[i]
  22. if line.startswith("name:"):
  23. name = line[len("name:"):].strip().strip('"').strip("'")
  24. elif line.startswith("description:"):
  25. value = line[len("description:"):].strip()
  26. # Handle YAML multiline indicators (>, |, >-, |-)
  27. if value in (">", "|", ">-", "|-"):
  28. continuation_lines: list[str] = []
  29. i += 1
  30. while i < len(frontmatter_lines) and (frontmatter_lines[i].startswith(" ") or frontmatter_lines[i].startswith("\t")):
  31. continuation_lines.append(frontmatter_lines[i].strip())
  32. i += 1
  33. description = " ".join(continuation_lines)
  34. continue
  35. else:
  36. description = value.strip('"').strip("'")
  37. i += 1
  38. return name, description, content