Find the start and end indices (inclusive, exclusive) of a chapter. title_path is like "Chapter 1/Section 2"
(lines: List[str], title_path: str)
| 193 | |
| 194 | |
| 195 | def _find_chapter_range(lines: List[str], title_path: str) -> Tuple[int, int]: |
| 196 | """ |
| 197 | Find the start and end indices (inclusive, exclusive) of a chapter. |
| 198 | title_path is like "Chapter 1/Section 2" |
| 199 | """ |
| 200 | titles = [t.strip() for t in title_path.split("/")] |
| 201 | current_level_idx = 0 |
| 202 | start_idx = -1 |
| 203 | |
| 204 | # We need to find the sequence of headers |
| 205 | search_start = 0 |
| 206 | |
| 207 | for i, target_title in enumerate(titles): |
| 208 | found = False |
| 209 | |
| 210 | for idx in range(search_start, len(lines)): |
| 211 | level, text = _parse_header(lines[idx]) |
| 212 | if level > 0 and text == target_title: |
| 213 | # Found the current segment |
| 214 | search_start = idx + 1 |
| 215 | found = True |
| 216 | if i == len(titles) - 1: |
| 217 | start_idx = idx |
| 218 | current_level_idx = level |
| 219 | break |
| 220 | |
| 221 | if not found: |
| 222 | return -1, -1 |
| 223 | |
| 224 | if start_idx == -1: |
| 225 | return -1, -1 |
| 226 | |
| 227 | # Find end: next header of same or lower level (higher importance, smaller integer) |
| 228 | end_idx = len(lines) |
| 229 | for idx in range(start_idx + 1, len(lines)): |
| 230 | level, _ = _parse_header(lines[idx]) |
| 231 | if level > 0 and level <= current_level_idx: |
| 232 | end_idx = idx |
| 233 | break |
| 234 | |
| 235 | return start_idx, end_idx |
| 236 | |
| 237 | |
| 238 | def report_read( |
no test coverage detected