xref: /linux/tools/docs/gen-redirects.py (revision 6093a688a07da07808f0122f9aa2a3eed250d853)
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
7"""Generate HTML redirects for renamed Documentation/**.rst files using
8the output of tools/docs/gen-renames.py.
9
10Example:
11
12    tools/docs/gen-redirects.py --output Documentation/output/ < Documentation/.renames.txt
13"""
14
15import argparse
16import os
17import sys
18
19parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
20parser.add_argument('-o', '--output', help='output directory')
21
22args = parser.parse_args()
23
24for line in sys.stdin:
25    line = line.rstrip('\n')
26
27    old_name, new_name = line.split(' ', 2)
28
29    old_html_path = os.path.join(args.output, old_name + '.html')
30    new_html_path = os.path.join(args.output, new_name + '.html')
31
32    if not os.path.exists(new_html_path):
33        print(f"warning: target does not exist: {new_html_path} (redirect from {old_html_path})")
34        continue
35
36    old_html_dir = os.path.dirname(old_html_path)
37    if not os.path.exists(old_html_dir):
38        os.makedirs(old_html_dir)
39
40    relpath = os.path.relpath(new_name, os.path.dirname(old_name)) + '.html'
41
42    with open(old_html_path, 'w') as f:
43        print(f"""\
44<!DOCTYPE html>
45
46<html lang="en">
47<head>
48    <title>This page has moved</title>
49    <meta http-equiv="refresh" content="0; url={relpath}">
50</head>
51<body>
52<p>This page has moved to <a href="{relpath}">{new_name}</a>.</p>
53</body>
54</html>""", file=f)
55