xref: /petsc/doc/fix_man_page_edit_links.py (revision 2a28d964fe6fe359fc6e4247643cfa1e8e64ec79)
1#!/usr/bin/env python
2""" A brittle approach to making "Edit this Page" links work on man pages """
3
4import os
5import re
6import fileinput
7
8EDIT_URL_PATTERN = re.compile(
9    r'^<p><a.*href="(.*)">Edit on GitLab</a></p>')  # very brittle
10
11
12def _get_edit_url(filename):
13    """ Get the edit URL for the source code for a manual page that was added by lib/petsc/bin/maint/wwwindex.py"""
14    with open(filename, "r") as f:
15        for line in f:
16            m = re.match(EDIT_URL_PATTERN, line)
17            if m:
18                return m.group(1)
19    return None
20
21
22def _check_edit_link(filename):
23    """ Return true if the file has an edit link to be updated """
24    found = False
25    with open(filename, "r") as f:
26        for line in f:
27            if line.lstrip().startswith("<div") and "editthispage" in line:
28                return True
29        return False
30
31
32def _update_edit_link(filename, url):
33    """ Change the URL for editthispage that Sphinx generates to the URL for GitLab repository location of the file
34        Remove the Edit on GitLab line added by lib/petsc/bin/maint/wwwindex.py since it is now a duplicate"""
35    with fileinput.FileInput(filename, inplace=True) as f:
36        replace_link_line = False
37        done = False
38        for line in f:
39            if done:
40                print(line, end="")  # prints to file
41            else:
42                if line.lstrip().startswith("<div") and "editthispage" in line:
43                    replace_link_line = True
44                if replace_link_line and line.lstrip().startswith("<a"):
45                    print("<a href=%s>" % url)  # prints to file
46                    done = True
47                elif not 'Edit on GitLab' in line:
48                    print(line, end="")  # prints to file
49
50
51def fix_man_page_edit_links(root):
52    base = os.path.join(root, "manualpages")
53    for root, dirs, filenames in os.walk(base):
54        for filename in filenames:
55            if filename.endswith(".html"):
56                filename_full = os.path.join(root, filename)
57                url = _get_edit_url(filename_full)
58                if url is not None and _check_edit_link(filename_full):
59                    _update_edit_link(filename_full, url)
60