-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathdocs.py
More file actions
47 lines (36 loc) · 1.27 KB
/
docs.py
File metadata and controls
47 lines (36 loc) · 1.27 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import os.path
from pathlib import Path
from typing import List, Optional, Tuple
import mdformat
import yaml
from frontmatter import Frontmatter
from hypernode.common.settings import DOCS_DIR
def get_all_docs() -> List[Path]:
result = []
for root, dirs, files in os.walk(DOCS_DIR):
if "/_build" in root or "/_res" in root:
continue
markdown_files = filter(lambda f: f.endswith(".md"), files)
for file in markdown_files:
result.append(Path(root) / file)
return result
def read_doc(path: Path) -> Tuple[dict, str, str]:
with open(path, mode="r") as f:
contents = f.read()
fm = Frontmatter.read(contents)
if not fm["attributes"]:
fm["attributes"] = {}
if not fm["body"]:
fm["body"] = contents
return (
fm["attributes"],
fm["body"],
contents,
)
def write_doc(path: Path, contents: str, frontmatter: Optional[dict]) -> None:
if frontmatter:
fm_yaml = yaml.dump(frontmatter, default_flow_style=False)
contents = "---\n" + fm_yaml + "---\n\n" + contents
contents = mdformat.text(contents, extensions=["frontmatter", "myst"])
with open(path, mode="w", encoding="utf-8") as f:
f.write(contents)