Skip to content

Turn any technical book PDF into a Claude Code skill — ready to study, reference, and use while you work.

fix(extract): detect a table of contents in any source, not just the first (#114)

Hotragn Pettuganiea9a71568020
diff --git a/book_to_skill/utils.py b/book_to_skill/utils.py
index 4021101..918da86 100644
--- a/book_to_skill/utils.py
+++ b/book_to_skill/utils.py
@@ -770,6 +770,16 @@ def main():
# headings and make the result depend on the source-path length.
structure_text = "\n\n".join(src["text"] for src in extracted_sources)
consolidated_structure = detect_structure(structure_text)
+ # has_toc is a per-source property, so it has to be combined per source
+ # rather than re-derived from the corpus. detect_structure only scans the
+ # first ~30k chars, because a table of contents sits in a book's front
+ # matter -- but on a consolidated corpus that window covers only the FIRST
+ # source, so a ToC in any later book was invisible and the answer flipped on
+ # input order alone. Each per-source result already scanned its own front
+ # matter, so OR them.
+ consolidated_structure["has_toc"] = any(
+ src["has_toc"] for src in extracted_sources
+ )
metadata = {
"source_file": "Consolidated from multiple sources" if len(extracted_sources) > 1 else extracted_sources[0]["source_file"],
diff --git a/tests/test_multi_source_toc.py b/tests/test_multi_source_toc.py
new file mode 100644
index 0000000..7c5470b
--- /dev/null
+++ b/tests/test_multi_source_toc.py
@@ -0,0 +1,94 @@
+"""Consolidated `has_toc` must not depend on the order of the input files.
+
+`detect_structure` only scans the first ~30,000 characters for a table-of-contents
+header, which is right for one book: a ToC lives in the front matter. But the
+consolidated value was derived by re-running that scan over the *joined* corpus,
+so the window only ever covered the first source. A ToC in any later book was
+invisible, and feeding the same two books in the other order produced the
+opposite answer.
+
+`main()` prints a "No table of contents detected" warning off this value and
+writes it to metadata.json, where Step 3 uses it to decide whether chapter
+mapping can trust a ToC or must fall back to a heading scan alone.
+"""
+
+import json
+import sys
+from pathlib import Path
+
+ROOT_DIR = Path(__file__).resolve().parent.parent
+sys.path.insert(0, str(ROOT_DIR))
+
+from book_to_skill.utils import main
+
+# Long enough that anything after it falls outside the 30k scan window.
+FILLER = "Filler prose about distributed systems. " * 1200
+NO_TOC_BOOK = "Chapter 1\n" + FILLER + "\n"
+TOC_BOOK = "Table of Contents\n\nChapter 2\nBody of the second book.\n"
+
+
+def _run(tmp_path, monkeypatch, *sources):
+ tmp_path.mkdir(parents=True, exist_ok=True)
+ paths = []
+ for index, text in enumerate(sources, start=1):
+ path = tmp_path / f"book{index}.md"
+ path.write_text(text, encoding="utf-8")
+ paths.append(str(path))
+
+ out_dir = tmp_path / "out"
+ out_meta = out_dir / "metadata.json"
+ monkeypatch.setenv("BOOK_SKILL_WORKDIR", str(out_dir))
+ monkeypatch.setattr("book_to_skill.utils.OUTPUT_DIR", out_dir)
+ monkeypatch.setattr("book_to_skill.utils.OUTPUT_TEXT", out_dir / "full_text.txt")
+ monkeypatch.setattr("book_to_skill.utils.OUTPUT_META", out_meta)
+ monkeypatch.setattr("book_to_skill.utils.prepare_dependencies", lambda *a: None)
+ monkeypatch.setattr(
+ "sys.argv", ["extract.py", *paths, "--install-missing", "no"]
+ )
+
+ main()
+ return json.loads(out_meta.read_text(encoding="utf-8"))
+
+
+class TestMultiSourceTocDetection:
+ def test_filler_book_pushes_second_book_past_the_window(self):
+ """Guards the premise: without this, the test would prove nothing."""
+ assert len(NO_TOC_BOOK) > 30000
+
+ def test_toc_in_second_source_is_detected(self, tmp_path, monkeypatch):
+ meta = _run(tmp_path, monkeypatch, NO_TOC_BOOK, TOC_BOOK)
+
+ assert meta["total_sources"] == 2
+ assert meta["has_toc"] is True
+
+ def test_result_is_independent_of_input_order(self, tmp_path, monkeypatch):
+ first = _run(tmp_path / "a", monkeypatch, NO_TOC_BOOK, TOC_BOOK)
+ second = _run(tmp_path / "b", monkeypatch, TOC_BOOK, NO_TOC_BOOK)
+
+ assert first["has_toc"] == second["has_toc"] is True
+
+ def test_agrees_with_the_per_source_records(self, tmp_path, monkeypatch):
+ meta = _run(tmp_path, monkeypatch, NO_TOC_BOOK, TOC_BOOK)
+
+ per_source = [src["has_toc"] for src in meta["sources"]]
+ assert per_source == [False, True]
+ assert meta["has_toc"] is any(per_source)
+
+ def test_no_toc_anywhere_stays_false(self, tmp_path, monkeypatch):
+ meta = _run(tmp_path, monkeypatch, NO_TOC_BOOK, "Chapter 2\nPlain body.\n")
+
+ assert meta["has_toc"] is False
+
+ def test_single_source_behaviour_unchanged(self, tmp_path, monkeypatch):
+ with_toc = _run(tmp_path / "a", monkeypatch, TOC_BOOK)
+ without = _run(tmp_path / "b", monkeypatch, NO_TOC_BOOK)
+
+ assert with_toc["has_toc"] is True
+ assert without["has_toc"] is False
+
+ def test_chapter_count_still_spans_all_sources(self, tmp_path, monkeypatch):
+ """The #83 fix must keep working alongside this one."""
+ meta = _run(tmp_path, monkeypatch, NO_TOC_BOOK, TOC_BOOK)
+
+ # Chapter 1 from the first book, Chapter 2 from the second.
+ assert meta["chapters_detected"] == 2