Skip to content

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

fix(extractor): count chapters within part headings (#279)

Huanyi Xieaeb77ba6b218
diff --git a/book_to_skill/utils.py b/book_to_skill/utils.py
index f5c99c7..f375d34 100644
--- a/book_to_skill/utils.py
+++ b/book_to_skill/utils.py
@@ -457,7 +457,8 @@ _BACK_MATTER_HEADERS = (
"about the authors",
)
_NON_CHAPTER_HEADERS = frozenset((*_FRONT_MATTER_HEADERS, *_BACK_MATTER_HEADERS))
-_NON_CHAPTER_PREFIX = re.compile(r"^(?:appendix|appendices|part)\b", re.IGNORECASE)
+_NON_CHAPTER_PREFIX = re.compile(r"^(?:appendix|appendices)\b", re.IGNORECASE)
+_PART_HEADER = re.compile(r"^part\s+(?:\d+|[ivxlcdm]+)\b", re.IGNORECASE)
def _is_non_chapter_section(title: str) -> bool:
@@ -467,6 +468,7 @@ def _is_non_chapter_section(title: str) -> bool:
normalized.casefold() in _NON_CHAPTER_HEADERS
or bool(_TOC_PATTERN.fullmatch(normalized))
or bool(_NON_CHAPTER_PREFIX.match(normalized))
+ or bool(_PART_HEADER.match(normalized))
)
# ATX-style heading: "# Title", "## Section", AsciiDoc "= Title", "== Section".
@@ -870,6 +872,8 @@ def detect_structure(text: str) -> dict:
headings = []
numbers = set()
+ part_headings = []
+ part_numbers = set()
prev = "" # previous non-blank line; cleared by blanks and fences
for index, line in enumerate(lines):
if index in fenced:
@@ -881,8 +885,13 @@ def detect_structure(text: str) -> dict:
continue
num = _chapter_number(line, prev)
if num is not None:
- numbers.add(num)
- headings.append(s)
+ title = _MD_HEADING_PREFIX.sub("", s)
+ if _PART_HEADER.match(title):
+ part_numbers.add(num)
+ part_headings.append(s)
+ else:
+ numbers.add(num)
+ headings.append(s)
prev = s
numeric_count = len(numbers)
# Fall back to structural (Markdown/AsciiDoc) headings only when no numeric
@@ -905,6 +914,12 @@ def detect_structure(text: str) -> dict:
# books with "### Unit N" headings still get counted via max().
structural_headings = _structural_chapter_headings(text)
structural_count = len(structural_headings)
+ # Parts group chapters and must not win the numeric branch ahead of
+ # the structural chapters inside them. Retain the old Part-only
+ # fallback when the extraction contains no other chapter evidence.
+ if not numeric_count and not structural_count:
+ numeric_count = len(part_numbers)
+ headings = part_headings
chapters_detected = max(numeric_count, structural_count)
chapters_method = (
"structural" if structural_count > numeric_count
diff --git a/tests/test_part_chapter_count.py b/tests/test_part_chapter_count.py
new file mode 100644
index 0000000..493f65c
--- /dev/null
+++ b/tests/test_part_chapter_count.py
@@ -0,0 +1,60 @@
+"""Part labels must not hide the chapters they group (issue #271)."""
+
+import pytest
+
+from book_to_skill.utils import detect_structure
+
+
[email protected]("marker", ["#", "="])
[email protected]("labels", [("I", "II"), ("1", "2")])
+def test_part_labels_do_not_suppress_structural_chapters(marker, labels):
+ text = "\n\n".join(
+ f"{marker} Part {label}: Group\n\n" + "\n\n".join(
+ f"{marker * 2} Topic {n}\nSynthetic chapter body."
+ for n in range(start, start + 5)
+ )
+ for label, start in zip(labels, (1, 6))
+ )
+
+ result = detect_structure(text)
+
+ assert result["chapters_detected"] == 10
+ assert result["chapters_method"] == "structural"
+ assert result["chapter_headings_sample"] == [
+ f"{marker * 2} Topic {n}" for n in range(1, 11)
+ ]
+
+
+def test_part_numbers_do_not_inflate_real_numbered_chapters():
+ text = "# Part I\n\n# Part II\n\n" + "\n\n".join(
+ f"## Chapter {n}: Topic\nSynthetic chapter body."
+ for n in (3, 4, 5)
+ )
+
+ result = detect_structure(text)
+
+ assert result["chapters_detected"] == 3
+ assert result["chapters_method"] == "numeric"
+ assert result["chapter_headings_sample"] == [
+ f"## Chapter {n}: Topic" for n in (3, 4, 5)
+ ]
+
+
+def test_part_only_plain_text_book_keeps_numeric_fallback():
+ result = detect_structure("Part I: Foundations\nBody.\n\nPart II: Practice\nBody.")
+
+ assert result["chapters_detected"] == 2
+ assert result["chapters_method"] == "numeric"
+ assert result["chapter_headings_sample"] == [
+ "Part I: Foundations", "Part II: Practice"
+ ]
+
+
+def test_instructional_titles_beginning_with_part_are_chapters():
+ titles = ["Part of Speech", "Part of a Whole", "Partnership Models"]
+ text = "\n\n".join(f"## {title}\nSynthetic chapter body." for title in titles)
+
+ result = detect_structure(text)
+
+ assert result["chapters_detected"] == 3
+ assert result["chapter_headings_sample"] == [f"## {title}" for title in titles]