Skip to content
Snippets Groups Projects
Commit 0346db4d authored by Miguel Angel Reina Ortega's avatar Miguel Angel Reina Ortega
Browse files

Fix links for ToC

parent 4299b861
No related branches found
No related tags found
No related merge requests found
Pipeline #1393 passed
...@@ -56,6 +56,9 @@ def processDocument(args:argparse.Namespace) -> None: ...@@ -56,6 +56,9 @@ def processDocument(args:argparse.Namespace) -> None:
.replace('(', '%28')\ .replace('(', '%28')\
.replace(')', '%29')\ .replace(')', '%29')\
.replace('>', '%3E')\ .replace('>', '%3E')\
.replace('[', '')\
.replace('\[', '')\
.replace(']', '')\
.replace(':', '%3A') .replace(':', '%3A')
...@@ -64,6 +67,8 @@ def processDocument(args:argparse.Namespace) -> None: ...@@ -64,6 +67,8 @@ def processDocument(args:argparse.Namespace) -> None:
# Note: We use utf-8 and replace errors to avoid problems with special or unknown characters. # Note: We use utf-8 and replace errors to avoid problems with special or unknown characters.
with open(args.document, 'r', encoding='utf-8', errors='replace') as f: with open(args.document, 'r', encoding='utf-8', errors='replace') as f:
document = f.readlines() document = f.readlines()
with open(args.document, 'w', encoding='utf-8', errors='replace') as f:
for line in document: for line in document:
_l = line.strip() _l = line.strip()
if _l.startswith('#'): if _l.startswith('#'):
...@@ -76,12 +81,21 @@ def processDocument(args:argparse.Namespace) -> None: ...@@ -76,12 +81,21 @@ def processDocument(args:argparse.Namespace) -> None:
# Skip the Contents headline if necessary # Skip the Contents headline if necessary
if (headline := _l.lstrip('#').strip()) == 'Contents' and not args.contents: if (headline := _l.lstrip('#').strip()) == 'Contents' and not args.contents:
continue continue
headers.append((headline, level)) heading_link=prepareTOClink(headline)
headers.append((headline, level, heading_link ))
heading_with_anchor = line + "{#" + f'{heading_link}'+ "}"
f.write(heading_with_anchor)
continue
f.write(line)
# Prepare and Print the table of contents # Prepare and Print the table of contents
toc = '# Contents\n\n' toc = '# Contents\n\n'
for h in headers: for h in headers:
toc += ' ' * (h[1] * args.indent) + f'[{h[0]}](#{prepareTOClink(h[0])}) \n' # toc += ' ' * (h[1] * args.indent) + f'[{h[0]}](#{prepareTOClink(h[0])}) \n'
toc += ' ' * (h[1] * args.indent) + f'[{h[0]}](#{h[2]}) \n'
toc = re.sub('<[^<]+?>', '', toc) toc = re.sub('<[^<]+?>', '', toc)
toc += '\n' toc += '\n'
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment