Turn any technical book PDF into a Claude Code skill — ready to study, reference, and use while you work.
Python agent-skillsai-agentsbook-to-skillcontext-engineeringdocument-processingedtechknowledge-baseknowledge-managementllmpdf-to-markdownragself-studystudy-tools
fix(extractor): count structural chapters per source (#273)
Ryan1568377f37f8
diff --git a/book_to_skill/utils.py b/book_to_skill/utils.pyindex 1d4c61a..f5c99c7 100644--- a/book_to_skill/utils.py+++ b/book_to_skill/utils.py@@ -1505,6 +1505,28 @@ 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)+ structural_sources = [+ src for src in extracted_sources+ if src["chapters_method"] == "structural" and src["chapters_detected"] >= 2+ ]+ if (+ len(extracted_sources) > 1+ and structural_sources+ and all(src["chapters_method"] != "numeric" for src in extracted_sources)+ ):+ # Each source has already selected its own chapter depth. Re-scanning+ # the joined corpus selects the H1 source titles instead when there are+ # multiple books with H2 chapters (issue #272). A lone heading in a+ # source may only be its title, so it is not added to the chapter count.+ consolidated_structure["chapters_detected"] = sum(+ src["chapters_detected"] for src in structural_sources+ )+ consolidated_structure["chapter_headings_sample"] = [+ heading+ for src in structural_sources+ for heading in src["chapter_headings_sample"]+ ][:10]+ consolidated_structure["chapters_method"] = "structural"# 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 frontdiff --git a/tests/test_multi_source_structural_chapters.py b/tests/test_multi_source_structural_chapters.pynew file mode 100644index 0000000..9989cbc--- /dev/null+++ b/tests/test_multi_source_structural_chapters.py@@ -0,0 +1,63 @@+"""Structural chapter depth is selected within each source, not across books."""++import json+import os+import subprocess+import sys+from pathlib import Path++import pytest+++ROOT_DIR = Path(__file__).resolve().parent.parent++[email protected]("source_count,include_plain", [(1, False), (3, False), (3, True)])+def test_cli_counts_structural_chapters_below_source_titles(+ tmp_path, source_count, include_plain+):+ paths = []+ expected_headings = []+ for source_number in range(1, source_count + 1):+ path = tmp_path / f"book{source_number}.md"+ headings = [+ f"## Topic {source_number}.{chapter_number}"+ for chapter_number in range(1, 4)+ ]+ path.write_text(+ f"# Book {source_number}\n\n"+ + "\n\n".join(f"{heading}\nBody text." for heading in headings)+ + "\n",+ encoding="utf-8",+ )+ paths.append(path)+ expected_headings.extend(headings)+ if include_plain:+ plain = tmp_path / "notes.md"+ plain.write_text("Supporting prose without chapter headings.\n", encoding="utf-8")+ paths.append(plain)++ workdir = tmp_path / "output"+ result = subprocess.run(+ [+ sys.executable,+ str(ROOT_DIR / "scripts" / "extract.py"),+ *map(str, paths),+ "--mode",+ "text",+ "--install-missing",+ "no",+ ],+ env=dict(os.environ, BOOK_SKILL_WORKDIR=str(workdir)),+ capture_output=True,+ text=True,+ timeout=30,+ )+ assert result.returncode == 0, result.stdout + result.stderr+ metadata = json.loads((workdir / "metadata.json").read_text(encoding="utf-8"))+ assert metadata["chapters_detected"] == 3 * source_count+ assert metadata["chapters_method"] == "structural"+ assert metadata["chapter_headings_sample"] == expected_headings[:10]+ assert [source["chapters_detected"] for source in metadata["sources"]] == (+ [3] * source_count + ([0] if include_plain else [])+ )