Created
June 27, 2023 19:46
-
-
Save MohitGupta121/6a63c096dd2a6919af7dc0287ec9757a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
def validate_toc(file_path): | |
with open(file_path, 'r') as file: | |
content = file.read() | |
# Extract the TOC section from the file content | |
match = re.search(r'## Table of contents(.+?)\n\n', content, re.DOTALL) | |
if not match: | |
print("Error: Table of contents section not found.") | |
return False | |
toc_content = match.group(1) | |
# Extract the expected headings from the TOC section | |
expected_headings = re.findall(r'\* \[(.*?)\]', toc_content) | |
# Extract the actual headings from the content | |
actual_headings = re.findall(r'(?<=^## ).+', content, re.MULTILINE) | |
# Remove "Table of contents" from the expected and actual headings | |
if len(expected_headings) > 0 and expected_headings[0] == "Table of contents": | |
expected_headings = expected_headings[1:] | |
if len(actual_headings) > 0 and actual_headings[0] == "Table of contents": | |
actual_headings = actual_headings[1:] | |
# Remove subheadings from the actual headings if they are present in the TOC | |
actual_headings = [heading for heading in actual_headings if heading not in expected_headings] | |
# Compare the expected and actual headings | |
if expected_headings == actual_headings: | |
print("Table of contents is valid.") | |
return True | |
else: | |
print("Error: Invalid headings.") | |
print("Expected headings: {}".format(expected_headings)) | |
print("Actual headings: {}".format(actual_headings)) | |
return False | |
file_path = '/Users/mohitgupta/Documents/scriptWiki/End-to-End-Tests.md' | |
validate_toc(file_path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment