-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfrontmatter.py
More file actions
33 lines (26 loc) · 908 Bytes
/
Copy pathfrontmatter.py
File metadata and controls
33 lines (26 loc) · 908 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
"""Parse YAML front matter from a Markdown document."""
from __future__ import annotations
from typing import Any
import yaml
_DELIM = "---"
def split_frontmatter(text: str) -> tuple[dict[str, Any], str]:
"""Split a document into ``(frontmatter_dict, body)``.
Returns an empty dict if the document has no front matter. Never raises on a
missing block; raises ``yaml.YAMLError`` only when a present block is invalid.
"""
if not text.startswith(_DELIM):
return {}, text
lines = text.splitlines()
end = None
for i in range(1, len(lines)):
if lines[i].strip() == _DELIM:
end = i
break
if end is None:
return {}, text
raw = "\n".join(lines[1:end])
data = yaml.safe_load(raw) or {}
body = "\n".join(lines[end + 1 :]).lstrip("\n")
if not isinstance(data, dict):
return {}, text
return data, body