File size: 839 Bytes
c6d9de7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from pathlib import Path
from typing import Any

import yaml


def read_frontmatter_and_body(file_path: Path) -> tuple[dict[str, Any], str]:
    with file_path.open("r") as f:
        content = f.read()
    if content.startswith("---"):
        end_idx = content.find("---", 3)
        if end_idx != -1:
            frontmatter = content[3:end_idx].strip()
            return yaml.safe_load(frontmatter), content[end_idx:]
    raise ValueError(f"No frontmatter found in file: {file_path}")


def get_tag_idx(readme: str, tag: str):
    tag_start = f"<!-- START-{tag} -->"
    tag_end = f"<!-- END-{tag} -->"
    start_idx = readme.find(tag_start)
    end_idx = readme.find(tag_end)
    if end_idx != -1 and start_idx != -1 and start_idx < end_idx:
        return start_idx, end_idx
    raise ValueError(f"tag ({tag}) not found in readme")