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