mirror of
https://github.com/HKUDS/OpenSpace.git
synced 2026-09-11 22:51:05 +00:00
fix(skill_engine): parse block scalars without requiring PyYAML
This commit is contained in:
parent
87e7dcdd73
commit
9d32b4db9c
2 changed files with 31 additions and 6 deletions
|
|
@ -113,15 +113,30 @@ def get_frontmatter_field(content: str, field_name: str) -> Optional[str]:
|
|||
"""Extract a single field value from YAML frontmatter.
|
||||
|
||||
Returns ``None`` if the field is absent or content has no frontmatter.
|
||||
Uses :func:`parse_frontmatter` so block scalars (``|`` / ``>``) round-trip.
|
||||
Block scalars (``|`` / ``>``) include following indented lines.
|
||||
"""
|
||||
fm = parse_frontmatter(content)
|
||||
if field_name not in fm:
|
||||
if not content.startswith("---"):
|
||||
return None
|
||||
value = fm[field_name]
|
||||
if value is None:
|
||||
match = _FRONTMATTER_RE.match(content)
|
||||
if not match:
|
||||
return None
|
||||
return value if isinstance(value, str) else str(value)
|
||||
lines = match.group(1).split("\n")
|
||||
for i, line in enumerate(lines):
|
||||
if ":" not in line:
|
||||
continue
|
||||
key, value = line.split(":", 1)
|
||||
if key.strip() != field_name:
|
||||
continue
|
||||
rest = value.strip()
|
||||
if re.fullmatch(r"[|>][+-]?", rest):
|
||||
collected: List[str] = []
|
||||
j = i + 1
|
||||
while j < len(lines) and lines[j].startswith((" ", "\t")):
|
||||
collected.append(re.sub(r"^[ \t]+", "", lines[j], count=1))
|
||||
j += 1
|
||||
return "\n".join(collected)
|
||||
return _yaml_unquote(rest)
|
||||
return None
|
||||
|
||||
|
||||
def set_frontmatter_field(content: str, field_name: str, value: str) -> str:
|
||||
|
|
|
|||
|
|
@ -10,6 +10,16 @@ def test_get_frontmatter_field_reads_block_scalar_description():
|
|||
assert get_frontmatter_field(src, "description") == "line1\nline2"
|
||||
|
||||
|
||||
def test_get_frontmatter_field_reads_folded_scalar():
|
||||
src = "---\nname: x\ndescription: >\n line1\n line2\n---\n"
|
||||
assert get_frontmatter_field(src, "description") == "line1\nline2"
|
||||
|
||||
|
||||
def test_get_frontmatter_field_missing_returns_none():
|
||||
src = "---\nname: x\n---\n"
|
||||
assert get_frontmatter_field(src, "description") is None
|
||||
|
||||
|
||||
def test_get_frontmatter_field_keeps_plain_scalar_text():
|
||||
src = "---\nenabled: true\n---\n"
|
||||
assert get_frontmatter_field(src, "enabled") == "true"
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue