Metadata-Version: 2.4
Name: anytocs
Version: 0.1.0
Summary: Extract a Table of Contents from any PDF (including scanned/image-only PDFs via OCR) and render it to Markdown with the document body following.
License-Expression: MIT
Requires-Python: >=3.9
Description-Content-Type: text/markdown
Requires-Dist: pymupdf>=1.24
Requires-Dist: pytesseract>=0.3.13
Requires-Dist: Pillow>=10.0
Provides-Extra: dev
Requires-Dist: pytest; extra == "dev"

<div align="center">
  <img width="677" height="369" alt="b43tdib43tdib43t-removebg-preview" src="https://github.com/user-attachments/assets/a5bc4c5a-6490-4231-afeb-2ab080d6cd3d" />
</div>




Extract a **Table of Contents (ToC) from any PDF** and render it to **Markdown**,
with the rest of the document following. Works for:

* PDFs with an embedded / keyword ToC bookmark layer,
* PDFs with a machine-readable text layer but no bookmark layer,
* **scanned / image-only PDFs** via OCR (Tesseract).

## Install

```bash
pip install anytocs
```

Requires Python >= 3.9 and the system Tesseract binary for OCR
(`pip install pytesseract` for the Python binding).

## CLI

```bash
anytocs my_gazette.pdf                    # -> my_gazette.md
anytocs jort_1964_047.pdf -o out.md
python -m anytocs my_gazette.pdf --help
```

Rules:

* The output file is named after the input PDF: `jort_1964_047.pdf` → `jort_1964_047.md`.
* The file starts with `# Table of Contents`, followed by `# Document Body`.
* If the PDF has **no structured ToC** (scanned gazette), the tool:
  1. writes `*(no structured ToC in source PDF)*`,
  2. re-reads the body and summarizes it into a ToC,
  3. marks the result `*(TOC generated from md file)*`,
  4. keeps the ToC at the top and the body beneath it.

## Library usage

```python
from anytocs.pdf_toc_md import build_full_markdown, enrich_markdown
from anytocs.pdf_toc import extract_toc_from_pdf, build_toc_nodes

# Flat ToC entries from a PDF (bookmark layer -> text layer -> OCR)
entries = extract_toc_from_pdf("jort_1964_047.pdf")
toc = build_toc_nodes(entries)

# Full Markdown: ToC at top, then the body
md = build_full_markdown("jort_1964_047.pdf")
print(md[:500])
```

## Layout

```
anytocs/
  __init__.py      public API
  __main__.py      `python -m anytocs`
  pdf_toc.py       ToC extraction (bookmark / text / OCR)
  pdf_toc_md.py    Markdown rendering + CLI (build_full_markdown)
  toc.py           TocNode + rendering
```

## Test

```bash
pip install -e ".[dev]"
pytest
```
