summaryrefslogtreecommitdiff
path: root/scripts/parse_frontmatter.py
blob: 2d5c60e58747e59104f9a5bb1db32d37cd7728c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import yaml

def parse_frontmatter(filepath):
    with open(filepath, 'r', encoding='utf-8') as f:
        lines = f.readlines()

    if not lines or lines[0].strip() != '---':
        return {}

    frontmatter_lines = []
    for line in lines[1:]:
        if line.strip() == '---':
            break
        frontmatter_lines.append(line)

    frontmatter_str = ''.join(frontmatter_lines)
    frontmatter = yaml.safe_load(frontmatter_str) or {}

    return frontmatter