MCPcopy Index your code
hub / github.com/karpathy/reader3 / parse_toc_recursive

Function parse_toc_recursive

reader3.py:96–132  ·  view source on GitHub ↗

Recursively parses the TOC structure from ebooklib.

(toc_list, depth=0)

Source from the content-addressed store, hash-verified

94
95
96def parse_toc_recursive(toc_list, depth=0) -> List[TOCEntry]:
97 """
98 Recursively parses the TOC structure from ebooklib.
99 """
100 result = []
101
102 for item in toc_list:
103 # ebooklib TOC items are either `Link` objects or tuples (Section, [Children])
104 if isinstance(item, tuple):
105 section, children = item
106 entry = TOCEntry(
107 title=section.title,
108 href=section.href,
109 file_href=section.href.split('#')[0],
110 anchor=section.href.split('#')[1] if '#' in section.href else "",
111 children=parse_toc_recursive(children, depth + 1)
112 )
113 result.append(entry)
114 elif isinstance(item, epub.Link):
115 entry = TOCEntry(
116 title=item.title,
117 href=item.href,
118 file_href=item.href.split('#')[0],
119 anchor=item.href.split('#')[1] if '#' in item.href else ""
120 )
121 result.append(entry)
122 # Note: ebooklib sometimes returns direct Section objects without children
123 elif isinstance(item, epub.Section):
124 entry = TOCEntry(
125 title=item.title,
126 href=item.href,
127 file_href=item.href.split('#')[0],
128 anchor=item.href.split('#')[1] if '#' in item.href else ""
129 )
130 result.append(entry)
131
132 return result
133
134
135def get_fallback_toc(book_obj) -> List[TOCEntry]:

Callers 1

process_epubFunction · 0.85

Calls 1

TOCEntryClass · 0.85

Tested by

no test coverage detected