diff --git a/bin/generate_redirects_from_source_links b/bin/generate_redirects_from_source_links new file mode 100644 index 00000000..0f417dac --- /dev/null +++ b/bin/generate_redirects_from_source_links @@ -0,0 +1,5 @@ +#!/usr/bin/env python3 +from hypernode.redirect.generate_redirects_from_source_links import main + +if __name__ == "__main__": + main() diff --git a/hypernode/common/docs.py b/hypernode/common/docs.py new file mode 100644 index 00000000..0c340190 --- /dev/null +++ b/hypernode/common/docs.py @@ -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 diff --git a/hypernode/common/settings.py b/hypernode/common/settings.py new file mode 100644 index 00000000..d3c3408d --- /dev/null +++ b/hypernode/common/settings.py @@ -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" diff --git a/hypernode/redirect/__init__.py b/hypernode/redirect/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/hypernode/redirect/generate_redirects_from_source_links.py b/hypernode/redirect/generate_redirects_from_source_links.py new file mode 100644 index 00000000..2d8204cf --- /dev/null +++ b/hypernode/redirect/generate_redirects_from_source_links.py @@ -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"^$") + + +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) diff --git a/requirements/base.txt b/requirements/base.txt index 03f0d4e2..2870e8f9 100644 --- a/requirements/base.txt +++ b/requirements/base.txt @@ -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