diff --git a/generateChangemarks/generateTOC.py b/generateChangemarks/generateTOC.py index 3129a8a8e9c0ec792a78a83488cafe7b7a4daa2d..1081e91a823d0de86509679d4c4d316ea699b6f5 100644 --- a/generateChangemarks/generateTOC.py +++ b/generateChangemarks/generateTOC.py @@ -56,6 +56,9 @@ def processDocument(args:argparse.Namespace) -> None: .replace('(', '%28')\ .replace(')', '%29')\ .replace('>', '%3E')\ + .replace('[', '')\ + .replace('\[', '')\ + .replace(']', '')\ .replace(':', '%3A') @@ -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. with open(args.document, 'r', encoding='utf-8', errors='replace') as f: document = f.readlines() + + with open(args.document, 'w', encoding='utf-8', errors='replace') as f: for line in document: _l = line.strip() if _l.startswith('#'): @@ -76,12 +81,21 @@ def processDocument(args:argparse.Namespace) -> None: # Skip the Contents headline if necessary if (headline := _l.lstrip('#').strip()) == 'Contents' and not args.contents: 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 toc = '# Contents\n\n' 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 += '\n'