Skip to content

Commit 4790fa8

Browse files
authored
Merge pull request #56 from ByteInternet/extract_write_doc_method
2 parents 9fa2edc + cdec98d commit 4790fa8

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed

bin/generate_redirects_from_source_links

100644100755
File mode changed.

hypernode/common/docs.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import os.path
22
from pathlib import Path
3-
from typing import List
3+
from typing import List, Optional
4+
5+
import mdformat
6+
import yaml
47

58
from hypernode.common.settings import DOCS_DIR
69

@@ -15,3 +18,14 @@ def get_all_docs() -> List[Path]:
1518
for file in markdown_files:
1619
result.append(Path(root) / file)
1720
return result
21+
22+
23+
def write_doc(path: Path, contents: str, frontmatter: Optional[dict]) -> None:
24+
if frontmatter:
25+
fm_yaml = yaml.dump(frontmatter, default_flow_style=False)
26+
contents = "---\n" + fm_yaml + "---\n\n" + contents
27+
28+
contents = mdformat.text(contents, extensions=["frontmatter", "myst"])
29+
30+
with open(path, mode="w", encoding="utf-8") as f:
31+
f.write(contents)

hypernode/redirect/generate_redirects_from_source_links.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33
from typing import Optional
44
from urllib.parse import urlparse
55

6-
import yaml
76
from frontmatter import Frontmatter
87

9-
from hypernode.common.docs import get_all_docs
8+
from hypernode.common.docs import get_all_docs, write_doc
109

1110
SOURCE_PATTERN = re.compile(r"^<!-- source: (.+) -->$")
1211

@@ -25,16 +24,13 @@ def set_source_path_redirect(doc: Path, source_path: str) -> None:
2524
fm = Frontmatter.read_file(doc)
2625
attributes = fm["attributes"] or {}
2726
attributes["redirect_from"] = [source_path]
28-
fm_yaml = yaml.dump(attributes, default_flow_style=False)
2927

3028
body = fm["body"]
3129
if not body:
3230
with open(doc, mode="r", encoding="utf-8") as f:
3331
body = f.read()
3432

35-
with open(doc, mode="w", encoding="utf-8") as f:
36-
contents = "---\n" + fm_yaml + "---\n\n" + body
37-
f.write(contents)
33+
write_doc(doc, body, attributes)
3834

3935

4036
def main():

0 commit comments

Comments
 (0)