Commit 0346db4d authored by Miguel Angel Reina Ortega's avatar Miguel Angel Reina Ortega
Browse files

Fix links for ToC

parent 4299b861
Loading
Loading
Loading
Loading
+16 −2
Original line number Diff line number Diff line
@@ -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'