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