gen-redirects.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #! /usr/bin/env python3
  2. # SPDX-License-Identifier: GPL-2.0
  3. #
  4. # Copyright © 2025, Oracle and/or its affiliates.
  5. # Author: Vegard Nossum <vegard.nossum@oracle.com>
  6. """Generate HTML redirects for renamed Documentation/**.rst files using
  7. the output of tools/docs/gen-renames.py.
  8. Example:
  9. tools/docs/gen-redirects.py --output Documentation/output/ < Documentation/.renames.txt
  10. """
  11. import argparse
  12. import os
  13. import sys
  14. parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
  15. parser.add_argument('-o', '--output', help='output directory')
  16. args = parser.parse_args()
  17. for line in sys.stdin:
  18. line = line.rstrip('\n')
  19. old_name, new_name = line.split(' ', 2)
  20. old_html_path = os.path.join(args.output, old_name + '.html')
  21. new_html_path = os.path.join(args.output, new_name + '.html')
  22. if not os.path.exists(new_html_path):
  23. print(f"warning: target does not exist: {new_html_path} (redirect from {old_html_path})")
  24. continue
  25. old_html_dir = os.path.dirname(old_html_path)
  26. if not os.path.exists(old_html_dir):
  27. os.makedirs(old_html_dir)
  28. relpath = os.path.relpath(new_name, os.path.dirname(old_name)) + '.html'
  29. with open(old_html_path, 'w') as f:
  30. print(f"""\
  31. <!DOCTYPE html>
  32. <html lang="en">
  33. <head>
  34. <title>This page has moved</title>
  35. <meta http-equiv="refresh" content="0; url={relpath}">
  36. </head>
  37. <body>
  38. <p>This page has moved to <a href="{relpath}">{new_name}</a>.</p>
  39. </body>
  40. </html>""", file=f)