Skip to content

Commit

Permalink
Add script to build redirects from root to current stable version
Browse files Browse the repository at this point in the history
  • Loading branch information
CAM-Gerlach committed Aug 26, 2020
1 parent 143736e commit 7bc745a
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 9 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ docs: ## generate Sphinx HTML documentation for the current branch
multidocs: ## generate Sphinx HTML documentation for the multiple versions available
$(MAKE) -C doc clean
sphinx-multiversion doc doc/_build/html
cp ci/index_redirect.html doc/_build/html/index.html
@python -c "import shutil; shutil.copytree('doc/_build/html/master/', 'doc/_build/html/stable/')"
@python ci/generateredirects.py "stable" -v --base-path "doc/_build/html" --base-url "https://docs.spyder-ide.org"

linkcheck: ## check that links are still valid
$(MAKE) -C doc linkcheck
Expand Down
119 changes: 119 additions & 0 deletions ci/generateredirects.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
#!/usr/bin/env python3
"""
Recursively build redirects for the HTML in a given dir to an output dir.
"""

# Standard library imports
import argparse
import os.path
from pathlib import Path


REDIRECT_TEXT = """<!DOCTYPE html>
<html>
<head>
<title>Redirecting to master branch</title>
<meta charset="utf-8">
<meta http-equiv="refresh" content="0; url={redirect_url}">
<link rel="canonical" href="{base_url}{canonical_url}">
</head>
</html>
"""


def generate_redirect(
canonical_path,
canonical_dir,
redirect_dir="",
base_path="",
base_url="/"
):
base_path = Path(base_path).resolve()
canonical_dir = base_path / canonical_dir
redirect_dir = base_path / redirect_dir
if base_url[-1] != "/":
base_url += "/"

redirect_output_path = (
redirect_dir / canonical_path.relative_to(canonical_dir))
redirect_url = Path(os.path.relpath(
canonical_path, start=redirect_output_path.parent))
canonical_url = canonical_path.relative_to(base_path)

output_text = REDIRECT_TEXT.format(
redirect_url=redirect_url.as_posix(),
base_url=base_url,
canonical_url=canonical_url.as_posix(),
)
with open(redirect_output_path, mode="w",
encoding="utf-8", newline="\n") as redirect_file:
redirect_file.write(output_text)

return redirect_output_path


def generate_redirects(
canonical_dir,
base_path="",
match_glob="**/*.html",
verbose=False,
**redirect_kwargs):
base_path = Path(base_path).resolve()

redirect_paths = []
for canonical_path in (base_path / canonical_dir).glob(match_glob):
redirect_path = generate_redirect(
canonical_path=canonical_path,
canonical_dir=canonical_dir,
base_path=base_path,
**redirect_kwargs)
redirect_paths.append(redirect_path)
if verbose:
print("Generated redirect from "
f"{redirect_path.relative_to(base_path).as_posix()} "
f"--> {canonical_path.relative_to(base_path).as_posix()}")

return redirect_paths


def generate_arg_parser():
arg_parser = argparse.ArgumentParser(
description="Generate redirects given a directory of HTML files",
argument_default=argparse.SUPPRESS)

arg_parser.add_argument(
"canonical_dir", help=(
"The directory of pages to build redirects to, "
"relative to base_dir."))
arg_parser.add_argument(
"--redirect-dir", help=(
"The dir that should be redirected from and "
"the redirect files should be written to, "
"relative to base_path. By default, base_path."))
arg_parser.add_argument(
"--base-path", help=(
"The path that canonical_dir and redirect_dir "
"are relative to; defaults to the working dir."))
arg_parser.add_argument(
"--base-url", help=(
"Full URL for the site, for use with the canonical tag."))
arg_parser.add_argument(
"--match-glob", help=(
"The glob pattern to match files to redirect to "
"in canonical_dir. By default, '**/*.html'."))
arg_parser.add_argument(
"-v", "--verbose", action="store_true", help=(
"If passed, prints each redirect generated."))

return arg_parser


def main(argv=None):
arg_parser = generate_arg_parser()
parsed_args = arg_parser.parse_args(argv)
redirect_paths = generate_redirects(**vars(parsed_args))
return redirect_paths


if __name__ == "__main__":
main()
8 changes: 0 additions & 8 deletions ci/index_redirect.html

This file was deleted.

0 comments on commit 7bc745a

Please sign in to comment.