-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathreplace-strings-in-doc.py
More file actions
executable file
·58 lines (50 loc) · 1.65 KB
/
replace-strings-in-doc.py
File metadata and controls
executable file
·58 lines (50 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import argparse
import re
from glob import glob
BOLD_TEXT = "\033[1m"
YELLOW = "\033[93m"
END_COLOUR = "\033[0m"
parser = argparse.ArgumentParser(
prog="replace-strings-in-doc.py", description="Replaces strings in html"
)
parser.add_argument(
"-c",
"--check-unused",
help="check whether all of the specified replacements are actually used",
action="store_true",
)
args = parser.parse_args()
replacements = {
r'(?<=<span class="sig-prename descclassname"><span class="pre">)_?libsemigroups_pybind11\.': ""
}
html_glob = "docs/_build/html/**/*.html"
files = glob(html_glob, recursive=True)
actually_replaced = set()
print(BOLD_TEXT + "Making post-build string replacements..." + END_COLOUR)
for file in sorted(files):
with open(file) as f:
content = f.read()
num_matches = 0
for expr, replacement in replacements.items():
content, n = re.subn(expr, replacement, content)
if n > 0:
num_matches += n
actually_replaced.add(expr)
if num_matches > 0:
print(f"String replacements made in {file}")
with open(file, "w") as f:
f.write(content)
if args.check_unused:
number_unused = 0
print(BOLD_TEXT + "Checking for unused string replacements..." + END_COLOUR, end=" ")
for bad_string, good_string in replacements.items():
if bad_string not in actually_replaced:
number_unused += 1
print(
YELLOW
+ f'WARNING: "{bad_string}" -> "{good_string}" [unused-replacement]'
+ END_COLOUR
)
if number_unused > 0:
print(f"Please correct this in {__file__}")
print("done")