nomadro/scripts/fix-ebook-read-paths.py
eric 0fc608ea26 Fix ebook /read links to /book/read and add compat redirects.
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-28 18:54:03 -05:00

50 lines
1.3 KiB
Python

#!/usr/bin/env python3
"""Prefix leftover /read paths in ebook to /book/read (avoid double prefix)."""
from __future__ import annotations
import pathlib
import re
ROOT = pathlib.Path(__file__).resolve().parents[1]
TARGETS = [
ROOT / "apps" / "ebook" / "src",
ROOT / "app" / "book",
]
PATTERNS = [
(re.compile(r"(?<!/book)/read#"), "/book/read#"),
(re.compile(r"(?<!/book)/read/"), "/book/read/"),
(re.compile(r"(?<!/book)/read(?=[`\"'\s\?]|$)"), "/book/read"),
]
def fix_text(text: str) -> str:
out = text
for pat, repl in PATTERNS:
out = pat.sub(repl, out)
out = out.replace("/book/book/read", "/book/read")
return out
def main() -> None:
n = 0
for base in TARGETS:
if not base.exists():
continue
for path in base.rglob("*"):
if path.suffix not in {".ts", ".tsx", ".js", ".jsx"}:
continue
if "node_modules" in path.parts:
continue
raw = path.read_text(encoding="utf-8")
fixed = fix_text(raw)
if fixed != raw:
path.write_text(fixed, encoding="utf-8")
n += 1
print("fixed", path.relative_to(ROOT))
print(f"updated {n} files")
if __name__ == "__main__":
main()