Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions bin/generate_redirects_from_source_links
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env python3
from hypernode.redirect.generate_redirects_from_source_links import main

if __name__ == "__main__":
main()
17 changes: 17 additions & 0 deletions hypernode/common/docs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import os.path
from pathlib import Path
from typing import List

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
6 changes: 6 additions & 0 deletions hypernode/common/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import os
from pathlib import Path

BASE_DIR = Path(os.path.realpath(os.path.dirname(__file__) + "/../.."))

DOCS_DIR = BASE_DIR / "docs"
Empty file added hypernode/redirect/__init__.py
Empty file.
47 changes: 47 additions & 0 deletions hypernode/redirect/generate_redirects_from_source_links.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import re
from pathlib import Path
from typing import Optional
from urllib.parse import urlparse

import yaml
from frontmatter import Frontmatter

from hypernode.common.docs import get_all_docs

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


def get_source_url(doc: Path) -> Optional[str]:
result = None
with open(doc, mode="r", encoding="utf-8") as f:
for line in f.readlines():
match = SOURCE_PATTERN.match(line)
if match:
result = match[1]
return result


def set_source_path_redirect(doc: Path, source_path: str) -> None:
fm = Frontmatter.read_file(doc)
attributes = fm["attributes"] or {}
attributes["redirect_from"] = [source_path]
fm_yaml = yaml.dump(attributes, default_flow_style=False)

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

with open(doc, mode="w", encoding="utf-8") as f:
contents = "---\n" + fm_yaml + "---\n\n" + body
f.write(contents)


def main():
for doc in get_all_docs():
source_url = get_source_url(doc)
if not source_url:
continue

path = urlparse(source_url).path
set_source_path_redirect(doc, path)
3 changes: 2 additions & 1 deletion requirements/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ sphinx-notfound-page==0.8.3
beautifulsoup4==4.11.1
markdownify==0.11.2
python-slugify==6.1.2
pyyaml==6.0
pyyaml==5.1
frontmatter==3.0.7