xref: /petsc/doc/update_htmlmap_links.py (revision 1b37a2a7cc4a4fb30c3e967db1c694c0a1013f51)
1import shutil
2import re
3import os
4
5def update_htmlmap_links(builder,htmlmap_filename):
6    """ Update manualpage links in an htmlmap file for use with Sphinx HTML builders html and dirhtml
7
8        This converts file names in the htmlmap file (ending with .md) to the locations of the
9        file that Sphinx generates.
10    """
11
12    if builder.name == "dirhtml":
13        postfix = "/"
14    elif builder.name == "html":
15        postfix = ".html"
16    else:
17        raise Exception("Unsupported builder named %s" % builder.name)
18
19    with open(htmlmap_filename+'_modified', "w") as htmlmap_file_modified, open(htmlmap_filename, "r") as htmlmap_file:
20        pattern = re.compile(r".*\+\+\+\+man\+(.*)$")  # Match URL in group
21        for line in htmlmap_file.readlines():
22            match = re.match(pattern, line)
23            if match:
24                url = match.group(1)
25                if url.startswith("manualpages"):
26                    line = os.path.splitext(line)[0] + postfix + "\n"
27            htmlmap_file_modified.write(line)
28    os.rename(htmlmap_filename + '_modified',htmlmap_filename)
29