| 1899 | |
| 1900 | |
| 1901 | def extract_pdf_sections(pdf_path: Path, max_pages: int | None = None) -> dict[str, str]: |
| 1902 | if fitz is None: |
| 1903 | return {} |
| 1904 | sections: dict[str, list[str]] = {"preamble": []} |
| 1905 | current = "preamble" |
| 1906 | doc = fitz.open(pdf_path) |
| 1907 | try: |
| 1908 | page_limit = len(doc) if max_pages is None else min(len(doc), max_pages) |
| 1909 | for page_index in range(page_limit): |
| 1910 | text = doc[page_index].get_text("text") |
| 1911 | reached_stop = False |
| 1912 | for raw_line in text.splitlines(): |
| 1913 | line = clean_pdf_line(raw_line) |
| 1914 | if not line: |
| 1915 | continue |
| 1916 | heading = match_section_heading(line) |
| 1917 | if heading == "stop": |
| 1918 | reached_stop = True |
| 1919 | break |
| 1920 | if heading: |
| 1921 | current = heading |
| 1922 | sections.setdefault(current, []) |
| 1923 | continue |
| 1924 | sections.setdefault(current, []).append(line) |
| 1925 | if reached_stop: |
| 1926 | break |
| 1927 | finally: |
| 1928 | doc.close() |
| 1929 | |
| 1930 | collapsed = {} |
| 1931 | for key, value in sections.items(): |
| 1932 | if not value: |
| 1933 | continue |
| 1934 | text = re.sub(r"\s+", " ", " ".join(value)).strip() |
| 1935 | if text: |
| 1936 | collapsed[key] = text |
| 1937 | return collapsed |
| 1938 | |
| 1939 | |
| 1940 | def extract_pdf_text(pdf_path: Path, max_pages: int | None = None) -> str: |