|
| 1 | +import os |
| 2 | +import re |
| 3 | +import logging |
| 4 | + |
| 5 | +from typing import Any, Dict |
| 6 | + |
| 7 | +enabled = os.getenv("LINKBACKWARD", "0") == "1" or os.getenv("FULL", "0") == "true" |
| 8 | +logger = logging.getLogger("mkdocs.hooks.linkbackward") |
| 9 | + |
| 10 | +if enabled: |
| 11 | + logger.info("hook - linkbackward is loaded and enabled") |
| 12 | +else: |
| 13 | + logger.info("hook - linkbackward is disabled") |
| 14 | + |
| 15 | +WAIT_TIME = 0 |
| 16 | +REDIRS = [ |
| 17 | + ("/ctf/steg/", "/ctf/misc/steg/"), |
| 18 | + ("/ctf/steg/image/", "/ctf/misc/steg/image/"), |
| 19 | + ("/ctf/steg/audio/", "/ctf/misc/steg/audio/"), |
| 20 | + ("/ctf/escapes/", "/ctf/misc/escapes/"), |
| 21 | + ("/ctf/escapes/pysandbox/", "/ctf/misc/escapes/pysandbox/"), |
| 22 | + ("/ctf/forensics/", "/ctf/misc/forensics/"), |
| 23 | + ("/ctf/forensics/mem/", "/ctf/misc/forensics/mem/"), |
| 24 | + ("/ctf/coding/", "/ctf/misc/coding/"), |
| 25 | + ("/ctf/qrcode/", "/ctf/misc/qrcode/"), |
| 26 | + ("/ctf/esolang/", "/ctf/misc/esolang/"), |
| 27 | + ("/cs/pl/c_cpp/", "/cs/pl/c_cpp/c/"), |
| 28 | + ("/hpc/", "/cs/hpc/hpc101/"), |
| 29 | + ("/hpc/hpc101/vectorized/", "/cs/hpc/hpc101/vectorized/"), |
| 30 | + ("/hpc/hpc101/gpu/", "/cs/hpc/hpc101/gpu/"), |
| 31 | + ("/hpc/hpc101/openmp/", "/cs/hpc/hpc101/openmp/"), |
| 32 | + ("/hpc/hpc101/mpi/", "/cs/hpc/hpc101/mpi/"), |
| 33 | + ("/hpc/hpc101/ml/", "/cs/hpc/hpc101/ml/"), |
| 34 | +] |
| 35 | + |
| 36 | + |
| 37 | +redirs = [] |
| 38 | +for src, dst in REDIRS: |
| 39 | + if not src.startswith("/"): |
| 40 | + src = "/" + src[1:] |
| 41 | + if not dst.startswith("/"): |
| 42 | + dst = "/" + dst[1:] |
| 43 | + if src.endswith("/"): |
| 44 | + src += "index.html" |
| 45 | + if dst.endswith("/"): |
| 46 | + dst += "index.html" |
| 47 | + if not src.endswith(".htm") and not src.endswith(".html"): |
| 48 | + src += "/index.html" |
| 49 | + if not dst.endswith(".htm") and not dst.endswith(".html"): |
| 50 | + dst += "/index.html" |
| 51 | + redirs.append((src, dst)) |
| 52 | + logger.debug(f"Redirect: `{src}` -> `{dst}`") |
| 53 | + |
| 54 | + |
| 55 | +def on_post_build(config: Dict[str, Any], **kwargs) -> None: |
| 56 | + if not enabled: |
| 57 | + return |
| 58 | + site_dir = config["site_dir"] |
| 59 | + template_file_path = os.path.join(site_dir, "redirection.html") |
| 60 | + with open(template_file_path, "r", encoding="utf-8") as f: |
| 61 | + template = f.read() |
| 62 | + for src, dst in redirs: |
| 63 | + if os.path.exists(os.path.join(site_dir, src[1:])): |
| 64 | + logger.warning( |
| 65 | + f"Skip creating redirection file `{src}` because it already exists" |
| 66 | + ) |
| 67 | + continue |
| 68 | + if not os.path.exists(os.path.join(site_dir, dst[1:])): |
| 69 | + logger.warning( |
| 70 | + f"Skip creating redirection file `{src}` because the dest `{dst}` does not exist" |
| 71 | + ) |
| 72 | + continue |
| 73 | + logger.debug(f"Creating redirection file `{src}` -> `{dst}`") |
| 74 | + src_file = re.sub( |
| 75 | + r"\./", |
| 76 | + "../" * src.count("/"), |
| 77 | + template, |
| 78 | + ) |
| 79 | + src_file = re.sub( |
| 80 | + r"//old//", |
| 81 | + src.replace("index.html", ""), |
| 82 | + src_file, |
| 83 | + ) |
| 84 | + src_file = re.sub( |
| 85 | + r"//new//", |
| 86 | + dst.replace("index.html", ""), |
| 87 | + src_file, |
| 88 | + ) |
| 89 | + src_file = re.sub( |
| 90 | + r"//wait_time//", |
| 91 | + str(WAIT_TIME), |
| 92 | + src_file, |
| 93 | + ) |
| 94 | + if WAIT_TIME == 0: |
| 95 | + src_file = re.sub( |
| 96 | + "<script>", |
| 97 | + f"<script>window.location='{dst.replace('index.html', '')}';", |
| 98 | + src_file, |
| 99 | + ) |
| 100 | + os.makedirs(os.path.dirname(os.path.join(site_dir, src[1:])), exist_ok=True) |
| 101 | + with open(os.path.join(site_dir, src[1:]), "w", encoding="utf-8") as f: |
| 102 | + f.write(src_file) |
0 commit comments