Coverage for little_loops / frontmatter.py: 11%
114 statements
« prev ^ index » next coverage.py v7.12.0, created at 2026-06-16 13:08 -0500
« prev ^ index » next coverage.py v7.12.0, created at 2026-06-16 13:08 -0500
1"""Frontmatter read/write utilities for little-loops.
3Provides shared YAML-subset frontmatter parsing, stripping, and updating
4used by issue_parser, sync, and issue_history modules.
5"""
7from __future__ import annotations
9import logging
10import re
11import textwrap
12from typing import Any
14import yaml
16logger = logging.getLogger(__name__)
18STATUS_SYNONYMS: dict[str, str] = {
19 "complete": "done",
20 "completed": "done",
21 "finished": "done",
22 "closed": "done",
23 "in-progress": "in_progress",
24 "in progress": "in_progress",
25 "wip": "in_progress",
26}
29def parse_frontmatter(content: str, *, coerce_types: bool = False) -> dict[str, Any]:
30 """Extract YAML frontmatter from content.
32 Looks for content between opening and closing '---' markers.
33 Parses a subset of YAML: simple ``key: value`` pairs, YAML block
34 sequences (``key:`` followed by ``- item`` lines), and block scalars
35 (``key: |`` or ``key: >`` followed by indented lines). Nested
36 structures are not supported and will emit a ``logging.WARNING``.
37 Returns empty dict if no frontmatter found.
39 Args:
40 content: File content to parse
41 coerce_types: If True, coerce digit strings to int
43 Returns:
44 Dictionary of frontmatter fields, or empty dict
45 """
46 if not content or not content.startswith("---"):
47 return {}
49 end_match = re.search(r"\n---\s*\n", content[3:])
50 if not end_match:
51 return {}
53 frontmatter_text = content[4 : 3 + end_match.start()]
55 result: dict[str, Any] = {}
56 current_list_key: str | None = None
57 lines = frontmatter_text.split("\n")
58 i = 0
59 while i < len(lines):
60 line = lines[i].strip()
61 i += 1
62 if not line or line.startswith("#"):
63 continue
64 if line.startswith("- "):
65 if current_list_key is not None:
66 result[current_list_key].append(line[2:].strip())
67 else:
68 logger.warning("Unsupported YAML list syntax in frontmatter: %r", line)
69 continue
70 # Non-list line: finalize any in-progress empty list, then reset
71 if current_list_key is not None and result[current_list_key] == []:
72 result[current_list_key] = None
73 current_list_key = None
74 if ":" in line:
75 key, value = line.split(":", 1)
76 key = key.strip()
77 value = value.strip()
78 if value.startswith("|") or value.startswith(">"):
79 # Block scalar: collect indented continuation lines
80 block_type = value[0]
81 block_lines: list[str] = []
82 while i < len(lines):
83 next_line = lines[i]
84 if next_line and (next_line[0] == " " or next_line[0] == "\t"):
85 block_lines.append(next_line)
86 i += 1
87 else:
88 break
89 if block_lines:
90 dedented = textwrap.dedent("\n".join(block_lines))
91 if block_type == ">":
92 dedented = re.sub(r"\s+", " ", dedented).strip()
93 result[key] = dedented
94 else:
95 result[key] = ""
96 continue
97 if value.startswith("[") and value.endswith("]"):
98 inner = value[1:-1].strip()
99 result[key] = [item.strip() for item in inner.split(",")] if inner else []
100 continue
101 if value.lower() in ("null", "~", ""):
102 if value == "":
103 result[key] = []
104 current_list_key = key
105 else:
106 result[key] = None
107 elif coerce_types and value.isdigit():
108 result[key] = int(value)
109 else:
110 # Strip surrounding YAML string quotes (single or double)
111 if len(value) >= 2 and value[0] == value[-1] and value[0] in ("'", '"'):
112 value = value[1:-1]
113 result[key] = value
114 # Finalize any trailing empty list key
115 if current_list_key is not None and result[current_list_key] == []:
116 result[current_list_key] = None
117 if "status" in result and isinstance(result["status"], str):
118 result["status"] = STATUS_SYNONYMS.get(result["status"], result["status"])
119 return result
122def parse_skill_frontmatter(text: str) -> dict[str, str]:
123 """Extract flat key/value pairs from SKILL.md frontmatter.
125 Uses ``yaml.safe_load`` so YAML block scalars (e.g. ``description: |``)
126 are resolved to their string content instead of the indicator literal.
127 Non-string scalar values are stringified; nested structures are dropped.
129 If the frontmatter is not valid YAML (e.g. unquoted colons in values),
130 falls back to a permissive line-based scan — top-level ``key: value``
131 pairs only, block scalars are not resolved in that path.
133 This is the canonical SKILL.md frontmatter parser. Prefer it over the
134 general ``parse_frontmatter`` for SKILL.md files: ``parse_frontmatter``
135 deliberately drops block scalars (logs a warning, sets value to
136 ``None``) which loses the description body for skills that use
137 ``description: |``.
138 """
139 if not text.startswith("---"):
140 return {}
141 end = text.find("---", 3)
142 if end == -1:
143 return {}
144 fm_text = text[3:end]
145 try:
146 loaded = yaml.safe_load(fm_text)
147 except yaml.YAMLError:
148 loaded = None
149 if isinstance(loaded, dict):
150 fm: dict[str, str] = {}
151 for key, value in loaded.items():
152 if value is None:
153 fm[str(key)] = ""
154 elif isinstance(value, str):
155 fm[str(key)] = value
156 elif isinstance(value, bool | int | float):
157 fm[str(key)] = str(value).lower() if isinstance(value, bool) else str(value)
158 return fm
159 fm = {}
160 for line in fm_text.splitlines():
161 if line and not line.startswith(" ") and not line.startswith("\t") and ":" in line:
162 key, _, val = line.partition(":")
163 fm[key.strip()] = val.strip()
164 return fm
167def strip_frontmatter(content: str) -> str:
168 """Remove YAML frontmatter from content, returning the body.
170 Strips the ``---`` delimited frontmatter block (if present) and
171 returns everything after the closing delimiter.
173 Args:
174 content: File content possibly starting with frontmatter
176 Returns:
177 Content with frontmatter removed. Returns original content
178 unchanged if no valid frontmatter block is found.
179 """
180 if not content or not content.startswith("---"):
181 return content
183 end_match = re.search(r"\n---\s*\n", content[3:])
184 if not end_match:
185 return content
187 return content[3 + end_match.end() :]
190def update_frontmatter(content: str, updates: dict[str, Any]) -> str:
191 """Update or add frontmatter fields in content.
193 Merges ``updates`` into an existing ``---`` delimited YAML frontmatter
194 block, preserving other fields and their order. If no frontmatter block
195 exists, a new one is prepended. Existing keys are overwritten with the
196 new values.
198 Args:
199 content: Full file content, possibly with existing frontmatter
200 updates: Fields to add/update in frontmatter; values may be nested dicts
202 Returns:
203 Content with updated frontmatter block
204 """
205 fm_match = re.match(r"^---\n(.*?)\n---", content, re.DOTALL)
206 if not fm_match:
207 fm_text = yaml.dump(dict(updates), default_flow_style=False, sort_keys=False).strip()
208 return f"---\n{fm_text}\n---\n{content}"
210 existing: dict[str, Any] = yaml.safe_load(fm_match.group(1)) or {}
211 existing.update(updates)
212 fm_text = yaml.dump(existing, default_flow_style=False, sort_keys=False).strip()
213 return f"---\n{fm_text}\n---{content[fm_match.end() :]}"