Resync all links and fix issues with auto-relink

This commit is contained in:
Griatch 2020-06-18 00:44:36 +02:00
parent 20a1741f4c
commit fab769e0d0
107 changed files with 887 additions and 877 deletions

View file

@ -38,7 +38,10 @@ def create_toctree():
"accept doc-files with the same name, even in different folders.")
docref_map[fname] = url
# normal reference-links [txt](urls)
ref_regex = re.compile(r"\[(?P<txt>[\w -\[\]]+?)\]\((?P<url>.+?)\)", re.I + re.S + re.U)
# in document references
ref_doc_regex = re.compile(r"\[(?P<txt>[\w -]+?)\]:\s+?(?P<url>.+?)(?=$|\n)", re.I + re.S + re.U)
def _sub(match):
grpdict = match.groupdict()
@ -49,17 +52,34 @@ def create_toctree():
fname, *anchor = url.rsplit("#", 1)
if fname in docref_map:
urlout = docref_map[fname] + ('#' + anchor[0] if anchor else '')
print(f" Remapped link [{txt}]({url}) -> [{txt}]({urlout})")
if urlout != url:
print(f" Remapped link [{txt}]({url}) -> [{txt}]({urlout})")
else:
urlout = url
return f"[{txt}]({urlout})"
def _sub_doc(match):
grpdict = match.groupdict()
txt, url = grpdict['txt'], grpdict['url']
fname, *part = url.rsplit("/", 1)
fname = part[0] if part else fname
fname = fname.rsplit(".", 1)[0]
fname, *anchor = url.rsplit("#", 1)
if fname in docref_map:
urlout = docref_map[fname] + ('#' + anchor[0] if anchor else '')
if urlout != url:
print(f" Remapped link [{txt}]: {url} -> [{txt}]: {urlout}")
else:
urlout = url
return f"[{txt}]: {urlout}"
# replace / correct links in all files
count = 0
for path in Path(_SOURCE_DIR).rglob("*.md"):
with open(path, 'r') as fil:
intxt = fil.read()
outtxt = ref_regex.sub(_sub, intxt)
outtxt = ref_doc_regex.sub(_sub_doc, outtxt)
if intxt != outtxt:
with open(path, 'w') as fil:
fil.write(outtxt)
@ -73,13 +93,16 @@ def create_toctree():
with open(_TOC_FILE, "w") as fil:
fil.write("# Toc\n")
for ref in sorted(docref_map.values()):
for ref in sorted(docref_map.values()):
if ref == "toc":
continue
linkname = ref.replace("-", " ")
fil.write(f"\n- [{linkname}]({ref}.md)")
fil.write(f"\n- [{linkname}]({ref})")
# we add a self-reference so the toc itself is also a part of a toctree
fil.write("\n\n```toctree::\n :hidden:\n\n toc\n```")
if __name__ == "__main__":
create_toctree()