Skip to content

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

fix(discovery_tax): reuse the extractor's multilingual ToC detection (#138)

Shubham Srivastavad5c9792c286b
diff --git a/book_to_skill/utils.py b/book_to_skill/utils.py
index 56d355f..34f1698 100644
--- a/book_to_skill/utils.py
+++ b/book_to_skill/utils.py
@@ -170,6 +170,7 @@ _KO_CHAPTER = re.compile(
# line (^\s*X\s*$) so an inline "the contents of this chapter" never matches.
_TOC_HEADERS = (
"table of contents", "contents", "índice", "sumário", # EN / ES / PT
+ "sumario", # PT (no accent — OCR / accent-stripped, like indice below)
"table des matières", # French
"inhaltsverzeichnis", # German
"indice", "sommario", # Italian (no accent — distinct from índice above)
diff --git a/tests/test_book_to_skill.py b/tests/test_book_to_skill.py
index 724898b..99e4463 100644
--- a/tests/test_book_to_skill.py
+++ b/tests/test_book_to_skill.py
@@ -712,6 +712,10 @@ class TestDetectStructure:
def test_toc_spanish_accented(self):
assert detect_structure("Índice\n1 Introducción")["has_toc"] is True
+ def test_toc_portuguese_unaccented(self):
+ # OCR / accent-stripped Brazilian PDFs leave "Sumario" without the accent.
+ assert detect_structure("Sumario\n1 Introdução")["has_toc"] is True
+
def test_toc_traditional_chinese(self):
assert detect_structure("目錄\n第一章")["has_toc"] is True
diff --git a/tests/test_discovery_tax.py b/tests/test_discovery_tax.py
index cfbcca1..1adcf3e 100644
--- a/tests/test_discovery_tax.py
+++ b/tests/test_discovery_tax.py
@@ -120,3 +120,12 @@ class TestDiscoveryTaxOrdering:
assert skill < d_best < dump, (skill, d_best, dump)
assert d_best <= d_loop, (d_best, d_loop)
assert skill < d_loop
+
+
+def test_extract_toc_detects_non_english_toc():
+ # German ToC: the stale local regex missed it and returned the whole front
+ # matter; reusing the extractor's _TOC_PATTERN slices from the ToC heading.
+ front = "Cover junk\nlots of preamble\n\nInhaltsverzeichnis\nKapitel 1 .. 5\nKapitel 2 .. 9\n"
+ toc = dt.extract_toc(front)
+ assert toc.lstrip().startswith("Inhaltsverzeichnis")
+ assert len(toc) < len(front) # not the whole-front-matter fallback
diff --git a/tools/discovery_tax.py b/tools/discovery_tax.py
index 3e2fff5..941bb30 100644
--- a/tools/discovery_tax.py
+++ b/tools/discovery_tax.py
@@ -39,10 +39,13 @@ from pathlib import Path
# discovery_tax and the pipeline always agree on what a chapter is (Arabic +
# Roman headings, prose/cross-reference rejection, list-item rejection).
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
-from book_to_skill.utils import _chapter_number as chapter_number # noqa: E402
-
-TOC_RE = re.compile(r"^\s*(?:sum[áa]rio|table of contents|contents|[íi]ndice)\s*$",
- re.IGNORECASE | re.MULTILINE)
+# Reuse the extractor's ToC detection too (multilingual + CJK), so discovery_tax
+# and the pipeline agree on what a ToC is — the stale local regex here only
+# matched EN/ES/PT and silently missed FR/DE/IT/NL/CJK books.
+from book_to_skill.utils import ( # noqa: E402
+ _chapter_number as chapter_number,
+ _TOC_PATTERN as toc_pattern,
+)
def count_tokens(text: str) -> int:
@@ -90,7 +93,7 @@ def best_chapter(chapters: list[tuple[int | None, str, str]], n: int,
def extract_toc(front_matter: str) -> str:
"""Best-effort slice of the ToC block from the front matter."""
- m = TOC_RE.search(front_matter)
+ m = toc_pattern.search(front_matter)
if not m:
# No explicit ToC: assume the agent skims the whole front matter.
return front_matter