2021-01-07 18:23:59 +00:00
|
|
|
"""
|
|
|
|
|
Github publishes the markdown documentation with jekyll enabled.
|
|
|
|
|
This extension does not publish any folder starting with `_`.
|
|
|
|
|
These folders need to be renamed.
|
|
|
|
|
"""
|
2024-03-13 17:00:32 +00:00
|
|
|
|
2021-01-07 18:23:59 +00:00
|
|
|
import os
|
|
|
|
|
import re
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def rename_folder(root):
|
|
|
|
|
"""
|
|
|
|
|
Renames all folder starting with `_`.
|
|
|
|
|
Returns the list of renamed folders.
|
|
|
|
|
"""
|
|
|
|
|
found = []
|
2023-03-24 22:29:03 +00:00
|
|
|
for r, dirs, _files in os.walk(root):
|
2021-01-07 18:23:59 +00:00
|
|
|
for name in dirs:
|
2022-04-26 16:35:16 +00:00
|
|
|
if name.startswith("_"):
|
2023-07-25 22:38:22 +00:00
|
|
|
found.append((r, name))
|
2021-01-07 18:23:59 +00:00
|
|
|
renamed = []
|
|
|
|
|
for r, name in found:
|
2022-04-26 16:35:16 +00:00
|
|
|
into = name.lstrip("_")
|
2021-01-07 18:23:59 +00:00
|
|
|
renamed.append((r, name, into))
|
|
|
|
|
full_src = os.path.join(r, name)
|
|
|
|
|
full_into = os.path.join(r, into)
|
2021-02-05 00:10:11 +00:00
|
|
|
if os.path.exists(full_into):
|
|
|
|
|
raise RuntimeError("%r already exists, previous documentation should be removed.")
|
2024-07-24 18:50:11 +00:00
|
|
|
print(f"rename {full_src!r}")
|
2021-01-07 18:23:59 +00:00
|
|
|
os.rename(full_src, full_into)
|
|
|
|
|
|
|
|
|
|
return renamed
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def replace_files(root, renamed):
|
|
|
|
|
subs = {r[1]: r[2] for r in renamed}
|
2023-04-17 17:11:44 +00:00
|
|
|
reg = re.compile('(\\"[a-zA-Z0-9\\.\\/\\?\\:@\\-_=#]+\\.([a-zA-Z]){2,6}([a-zA-Z0-9\\.\\&\\/\\?\\:@\\-_=#])*\\")')
|
2021-01-07 18:23:59 +00:00
|
|
|
|
2023-03-24 22:29:03 +00:00
|
|
|
for r, _dirs, files in os.walk(root):
|
2021-01-07 18:23:59 +00:00
|
|
|
for name in files:
|
2022-04-26 16:35:16 +00:00
|
|
|
if os.path.splitext(name)[-1] != ".html":
|
2021-01-07 18:23:59 +00:00
|
|
|
continue
|
|
|
|
|
full = os.path.join(r, name)
|
2023-03-24 22:29:03 +00:00
|
|
|
with open(full, encoding="utf-8") as f:
|
2021-01-07 18:23:59 +00:00
|
|
|
content = f.read()
|
|
|
|
|
find = reg.findall(content)
|
|
|
|
|
repl = []
|
|
|
|
|
for f in find:
|
|
|
|
|
if f[0].startswith("http"):
|
|
|
|
|
continue
|
|
|
|
|
for k, v in subs.items():
|
|
|
|
|
if k == v:
|
2023-03-24 22:29:03 +00:00
|
|
|
raise ValueError(f"{k!r} == {v!r}")
|
2024-07-24 18:50:11 +00:00
|
|
|
if (f'"{k}') in f[0]:
|
|
|
|
|
repl.append((f[0], f[0].replace(f'"{k}', f'"{v}')))
|
|
|
|
|
if (f"/{k}") in f[0]:
|
|
|
|
|
repl.append((f[0], f[0].replace(f"/{k}", f"/{v}")))
|
2021-01-07 18:23:59 +00:00
|
|
|
if len(repl) == 0:
|
|
|
|
|
continue
|
2024-07-24 18:50:11 +00:00
|
|
|
print(f"update {full!r}")
|
2021-01-07 18:23:59 +00:00
|
|
|
for k, v in repl:
|
|
|
|
|
content = content.replace(k, v)
|
|
|
|
|
with open(full, "w", encoding="utf-8") as f:
|
|
|
|
|
f.write(content)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
import sys
|
2022-04-26 16:35:16 +00:00
|
|
|
|
2021-01-07 18:23:59 +00:00
|
|
|
if len(sys.argv) > 1:
|
|
|
|
|
root = sys.argv[-1]
|
|
|
|
|
else:
|
|
|
|
|
root = "../../build/docs/html"
|
2024-07-24 18:50:11 +00:00
|
|
|
print(f"look into {root!r}")
|
2021-01-07 18:23:59 +00:00
|
|
|
ren = rename_folder(root)
|
|
|
|
|
if len(ren) == 0:
|
2022-04-26 16:35:16 +00:00
|
|
|
ren = [
|
|
|
|
|
("", "_static", "static"),
|
|
|
|
|
("", "_images", "images"),
|
|
|
|
|
("", "_downloads", "downloads"),
|
|
|
|
|
("", "_sources", "sources"),
|
|
|
|
|
("", "_modules", "modules"),
|
|
|
|
|
]
|
2021-01-07 18:23:59 +00:00
|
|
|
replace_files(root, ren)
|
2022-04-26 16:35:16 +00:00
|
|
|
print("done.")
|