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): reject punctuation-only setext titles, matching the ATX branch (#180)
Hotragn Pettugani1ee330183a36
diff --git a/book_to_skill/utils.py b/book_to_skill/utils.pyindex e0674cf..f0eb504 100644--- a/book_to_skill/utils.py+++ b/book_to_skill/utils.py@@ -388,6 +388,13 @@ def _structural_chapter_count(text: str) -> int:and prevand not _SETEXT_UNDERLINE.match(prev)and len(s) >= len(prev)+ # A title made only of punctuation is never a chapter. Two thematic+ # breaks in a row ("***" over "---"), an ASCII box rule, a row of+ # dots, or a table border sitting above an underline all reach this+ # point. The ATX branch below already rejects them with the same+ # test; the setext branch had no equivalent, so the identical string+ # counted as a heading here and not there.+ and re.search(r"\w", prev)):depth = 1 if s[0] == "=" else 2levels.setdefault(depth, set()).add(prev.lower())diff --git a/tests/test_setext_punctuation_guard.py b/tests/test_setext_punctuation_guard.pynew file mode 100644index 0000000..2bb3156--- /dev/null+++ b/tests/test_setext_punctuation_guard.py@@ -0,0 +1,131 @@+r"""A setext title made only of punctuation is not a chapter heading.++`_structural_chapter_count` has two heading branches. The ATX branch rejects a+title with no word character — that is what keeps a `=====` table border or a+`***` thematic break from being counted:++ if title and re.search(r"\w", title):++The setext branch had no equivalent. So the *identical string* was rejected as+`## ***` and accepted as `***` sitting above a row of `-`. Two thematic breaks+in a row, an ASCII box rule, a row of dots, or a punctuation table border above+an underline all minted a phantom heading.++The function's own docstring claims "thematic breaks, table borders, and+front-matter `---` do not match", which was only true while the underline was+shorter than the line above it.+"""++import sys+from pathlib import Path++import pytest++ROOT_DIR = Path(__file__).resolve().parent.parent+sys.path.insert(0, str(ROOT_DIR))++from book_to_skill.utils import _structural_chapter_count++# Two real sections in every fixture below, so any count above 2 is a phantom.+# `___` is deliberately NOT here: "_" is a word character to `\w`, so it is kept+# by both branches. See TestSharedUnderscoreGap.+PUNCTUATION_TITLES = ["***", "+-----+", ".......", "|||||", "* * *", "###", "-- --"]+++def _with_setext(title: str) -> str:+ """A book whose middle holds `title` underlined by an equally long rule."""+ return (+ "# Book\n\n## Alpha\na\n\n"+ f"{title}\n{'-' * max(3, len(title))}\n\n"+ "## Beta\nb\n"+ )+++def _with_atx(title: str) -> str:+ return f"# Book\n\n## Alpha\na\n\n## {title}\n\n## Beta\nb\n"+++class TestPunctuationOnlySetextTitleRejected:+ @pytest.mark.parametrize("title", PUNCTUATION_TITLES)+ def test_no_phantom_heading(self, title):+ assert _structural_chapter_count(_with_setext(title)) == 2++ @pytest.mark.parametrize("title", PUNCTUATION_TITLES)+ def test_matches_the_atx_branch(self, title):+ """The same string must be judged the same way by both branches."""+ assert _structural_chapter_count(_with_setext(title)) == (+ _structural_chapter_count(_with_atx(title))+ )++ def test_two_thematic_breaks_in_a_row(self):+ """`***` then `---` are both valid thematic breaks, not a heading."""+ text = "# Book\n\n## Alpha\na\n\n***\n---\n\n## Beta\nb\n"++ assert _structural_chapter_count(text) == 2++ def test_underline_longer_than_the_punctuation_run(self):+ """The length guard does not help when the rule is the longer line."""+ text = "# Book\n\n## Alpha\na\n\n***\n" + "-" * 40 + "\n\n## Beta\nb\n"++ assert _structural_chapter_count(text) == 2+++class TestRealSetextHeadingsStillCounted:+ """The branch must keep doing its job."""++ def test_word_titles_still_counted(self):+ text = "Alpha\n=====\n\ntext\n\nBeta\n====\n\ntext\n"++ assert _structural_chapter_count(text) == 2++ def test_mixed_punctuation_and_words_is_kept(self):+ """A word character anywhere is enough — titles carry punctuation."""+ text = (+ "Chapter One -- Beginnings\n-------------------------\n\ntext\n\n"+ "Chapter Two -- Endings\n----------------------\n\ntext\n"+ )++ assert _structural_chapter_count(text) == 2++ def test_title_with_digits_and_punctuation(self):+ text = "1.2 Scope\n---------\n\ntext\n\n1.3 Limits\n----------\n\ntext\n"++ assert _structural_chapter_count(text) == 2++ def test_cjk_setext_title_counted(self):+ r"""`\w` is Unicode-aware, so a CJK title is not punctuation."""+ text = "\u7b2c\u4e00\u7ae0\n====\n\ntext\n\n\u7b2c\u4e8c\u7ae0\n====\n\ntext\n"++ assert _structural_chapter_count(text) == 2++ def test_snake_case_title_kept(self):+ assert _structural_chapter_count(_with_setext("snake_case_title")) == 3+++class TestSharedUnderscoreGap:+ """`___` is a thematic break but `_` is a word character, so both branches+ still count it. Pre-existing and identical on either side — pinned here so+ the parity this PR establishes is visible, and so closing the gap later is a+ deliberate change to BOTH branches rather than a silent divergence.+ """++ def test_underscore_rule_still_counted_by_both_branches(self):+ setext = _structural_chapter_count(_with_setext("___"))+ atx = _structural_chapter_count(_with_atx("___"))++ assert setext == atx == 3+++class TestUnaffectedBehaviour:+ def test_document_with_no_headings(self):+ assert _structural_chapter_count("just prose\nmore prose\n") == 0++ def test_setext_still_needs_a_long_enough_underline(self):+ text = "# Book\n\n## Alpha\na\n\nA long paragraph line here.\n---\n\n## Beta\nb\n"++ assert _structural_chapter_count(text) == 2++ def test_blank_line_above_underline_is_not_a_heading(self):+ text = "# Book\n\n## Alpha\na\n\n\n-------\n\n## Beta\nb\n"++ assert _structural_chapter_count(text) == 2