-
-
Notifications
You must be signed in to change notification settings - Fork 54
/
fix-converted-myst.py
56 lines (42 loc) · 1.53 KB
/
fix-converted-myst.py
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
import logging
import os
import re
logging.basicConfig()
logger = logging.getLogger("fix converted MyST documentation")
logger.setLevel(logging.INFO)
logger.info("Fix some MyST / markdown stuff.")
count_files = {
"modified": 0,
"unmodified": 0,
}
def replace_label_underscore(data):
"""Fix 'examples' reference in docstrings.
:Example: :ref:`portal_get_tool_example`
->
:Example: :ref:`portal-get-tool-example`
"""
regex_label_underscore = "(?<=:ref:`)(.*)(?=`)"
def _replace(mobj):
return mobj.group(0).replace("_", "-")
data = re.sub(regex_label_underscore, _replace, data)
return data
def remove_github_warning(data):
"""Remove 'GitHub-only' warning"""
regex_github_warning = r":::{admonition} GitHub-only([\S\s]*?):::\n\n"
data = re.sub(regex_github_warning, "", data, flags=re.DOTALL)
return data
for root, dirs, files in (*os.walk("./src"), *os.walk("./docs")):
for name in files:
if name.endswith(".py") or name.endswith(".md"):
filename = os.path.join(root, name)
with open(filename, "r+") as f:
data = f.read()
data = replace_label_underscore(data)
data = remove_github_warning(data)
f.seek(0)
f.write(data)
f.truncate()
count_files["modified"] += 1
logger.info(f"{filename} modified.")
logger.info(f'MyST modified for {count_files["modified"]} files.')
logger.info(f'{count_files["unmodified"]} files unmodified.')