Skip to content

Commit ccadbd3

Browse files
authored
Merge pull request #51 from ByteInternet/generate_redirects_from_source_links
2 parents 1196d63 + 75542ba commit ccadbd3

File tree

6 files changed

+77
-1
lines changed

6 files changed

+77
-1
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env python3
2+
from hypernode.redirect.generate_redirects_from_source_links import main
3+
4+
if __name__ == "__main__":
5+
main()

hypernode/common/docs.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import os.path
2+
from pathlib import Path
3+
from typing import List
4+
5+
from hypernode.common.settings import DOCS_DIR
6+
7+
8+
def get_all_docs() -> List[Path]:
9+
result = []
10+
for root, dirs, files in os.walk(DOCS_DIR):
11+
if "/_build" in root or "/_res" in root:
12+
continue
13+
14+
markdown_files = filter(lambda f: f.endswith(".md"), files)
15+
for file in markdown_files:
16+
result.append(Path(root) / file)
17+
return result

hypernode/common/settings.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import os
2+
from pathlib import Path
3+
4+
BASE_DIR = Path(os.path.realpath(os.path.dirname(__file__) + "/../.."))
5+
6+
DOCS_DIR = BASE_DIR / "docs"

hypernode/redirect/__init__.py

Whitespace-only changes.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import re
2+
from pathlib import Path
3+
from typing import Optional
4+
from urllib.parse import urlparse
5+
6+
import yaml
7+
from frontmatter import Frontmatter
8+
9+
from hypernode.common.docs import get_all_docs
10+
11+
SOURCE_PATTERN = re.compile(r"^<!-- source: (.+) -->$")
12+
13+
14+
def get_source_url(doc: Path) -> Optional[str]:
15+
result = None
16+
with open(doc, mode="r", encoding="utf-8") as f:
17+
for line in f.readlines():
18+
match = SOURCE_PATTERN.match(line)
19+
if match:
20+
result = match[1]
21+
return result
22+
23+
24+
def set_source_path_redirect(doc: Path, source_path: str) -> None:
25+
fm = Frontmatter.read_file(doc)
26+
attributes = fm["attributes"] or {}
27+
attributes["redirect_from"] = [source_path]
28+
fm_yaml = yaml.dump(attributes, default_flow_style=False)
29+
30+
body = fm["body"]
31+
if not body:
32+
with open(doc, mode="r", encoding="utf-8") as f:
33+
body = f.read()
34+
35+
with open(doc, mode="w", encoding="utf-8") as f:
36+
contents = "---\n" + fm_yaml + "---\n\n" + body
37+
f.write(contents)
38+
39+
40+
def main():
41+
for doc in get_all_docs():
42+
source_url = get_source_url(doc)
43+
if not source_url:
44+
continue
45+
46+
path = urlparse(source_url).path
47+
set_source_path_redirect(doc, path)

requirements/base.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ sphinx-notfound-page==0.8.3
1313
beautifulsoup4==4.11.1
1414
markdownify==0.11.2
1515
python-slugify==6.1.2
16-
pyyaml==6.0
16+
pyyaml==5.1
17+
frontmatter==3.0.7

0 commit comments

Comments
 (0)