Skip to content

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

fix(extractor): exclude source banners from chapter detection (#83)

Nathancc7d5ed6af83
diff --git a/CHANGELOG.md b/CHANGELOG.md
index cd53e99..38fe77b 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -51,6 +51,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
`PyPDF2` is end-of-life and no longer receives security fixes (#54).
### Fixed
+- Consolidated chapter detection now analyzes extracted source text without the generated
+ `SOURCE:` boundary banners, preventing those banners from becoming phantom setext headings
+ and collapsing `chapters_detected` to 2 for short source paths (#81).
- **`Chapter I.` — a chapter word followed by a Roman numeral — is now detected.** It
matched neither existing pattern (`_EXPLICIT_CHAPTER` required Arabic digits after the
chapter word; `_ROMAN_HEAD` required the numeral to start the line), so books using
diff --git a/book_to_skill/utils.py b/book_to_skill/utils.py
index 3e2b912..ca2fdc4 100644
--- a/book_to_skill/utils.py
+++ b/book_to_skill/utils.py
@@ -646,8 +646,11 @@ def main():
total_words = len(consolidated_text.split())
total_tokens = estimate_tokens(consolidated_text)
- # Detect structure on consolidated text
- consolidated_structure = detect_structure(consolidated_text)
+ # Detect structure from source content only. The generated SOURCE banners in
+ # full_text.txt use rows of "=", which can otherwise become phantom setext
+ # 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)
metadata = {
"source_file": "Consolidated from multiple sources" if len(extracted_sources) > 1 else extracted_sources[0]["source_file"],
diff --git a/tests/test_book_to_skill.py b/tests/test_book_to_skill.py
index 2491919..046ee3a 100644
--- a/tests/test_book_to_skill.py
+++ b/tests/test_book_to_skill.py
@@ -366,6 +366,49 @@ class TestBatchResilience:
meta = json.loads(out_meta.read_text(encoding="utf-8"))
assert meta["total_sources"] == 1
+ @pytest.mark.parametrize(
+ "reported_source",
+ ["/x/sample.md", "/deep/" + ("nested/" * 12) + "sample.md"],
+ )
+ def test_source_banner_does_not_change_structural_chapter_count(
+ self, tmp_path, monkeypatch, reported_source
+ ):
+ """The generated SOURCE banner must not become a phantom setext heading."""
+ source = _make_md_file(
+ tmp_path / "sample.md",
+ "# The Pragmatic Widget\n\n"
+ "## Foundations\n\nBody.\n\n"
+ "## Design Rules\n\nBody.\n\n"
+ "## Trade-offs\n\nBody.\n\n"
+ "## Operating Model\n\nBody.\n\n"
+ "## Closing\n\nBody.\n",
+ )
+ out_dir = tmp_path / "output"
+ out_text = out_dir / "full_text.txt"
+ out_meta = out_dir / "metadata.json"
+ real_extract = extract_single_file
+
+ def extract_with_reported_source(*args, **kwargs):
+ result = real_extract(*args, **kwargs)
+ result["source_file"] = reported_source
+ return result
+
+ monkeypatch.setattr("sys.argv", ["extract.py", str(source), "--install-missing", "no"])
+ monkeypatch.setattr("book_to_skill.utils.OUTPUT_DIR", out_dir)
+ monkeypatch.setattr("book_to_skill.utils.OUTPUT_TEXT", out_text)
+ monkeypatch.setattr("book_to_skill.utils.OUTPUT_META", out_meta)
+ monkeypatch.setattr("book_to_skill.utils.prepare_dependencies", lambda *a: None)
+ monkeypatch.setattr(
+ "book_to_skill.utils.extract_single_file", extract_with_reported_source
+ )
+
+ main()
+
+ metadata = json.loads(out_meta.read_text(encoding="utf-8"))
+ assert metadata["sources"][0]["chapters_detected"] == 5
+ assert metadata["chapters_detected"] == 5
+ assert "SOURCE: sample.md" in out_text.read_text(encoding="utf-8")
+
def test_extraction_error_is_not_system_exit(self):
"""ExtractionError should NOT be a subclass of SystemExit."""
assert not issubclass(ExtractionError, SystemExit)