xref: /petsc/doc/update_htmlmap_links.py (revision f97672e55eacc8688507b9471cd7ec2664d7f203)
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
12
13        This converts source files (.md) to the locations of the resulting .html pages
14        or directories (which a web server will redirect to the index.html inside).
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