-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread_toc.py
30 lines (26 loc) · 1012 Bytes
/
read_toc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import yaml
import os
def read_toc(file_path):
# Check if the file exists
if not os.path.exists(file_path):
print(f"File '{file_path}' does not exist.")
return
# Load the TOC data from the YAML file
with open(file_path, 'r') as file:
toc_data = yaml.safe_load(file)
# Print the overview of the book structure
print("Overview of the book structure:")
chapters = toc_data.get('chapters', [])
for chapter in chapters:
print(f"Chapter: {chapter.get('title', chapter.get('file'))}")
sections = chapter.get('sections', [])
for section in sections:
print(f" - Section: {section['title']}")
subsections = section.get('subsections', [])
for subsection in subsections:
print(f" - Subsection: {subsection['title']}")
if __name__ == '__main__':
# Specify the path to the TOC file (YAML)
toc_file_path = '_toc.yaml'
# Call the read_toc function
read_toc(toc_file_path)