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): validate Markdown code fence closers (#291)
XuJianc76601770ac8
diff --git a/book_to_skill/utils.py b/book_to_skill/utils.pyindex 1836e70..4ed93c1 100644--- a/book_to_skill/utils.py+++ b/book_to_skill/utils.py@@ -533,19 +533,25 @@ def _closed_fence_line_numbers(lines: list[str]) -> set[int]:Counting a handful of code lines as prose is a far cheaper mistake thanlosing most of a book's structure.- The closing fence must use the SAME character as its opener, per CommonMark,- so a "```" block is no longer terminated by an unrelated "~~~" line.+ Per CommonMark, a closer must use the same character, be at least as long+ as its opener, and have no trailing info string. A longer fence can then+ safely contain examples of shorter fences without exposing sample headings."""inside: set[int] = set()opener: tuple[str, int] | None = Nonefor index, line in enumerate(lines):- match = _CODE_FENCE.match(line.strip())+ stripped = line.strip()+ match = _CODE_FENCE.match(stripped)if not match:continuemarker = match.group(1)if opener is None:- opener = (marker[0], index)- elif marker[0] == opener[0]:+ opener = (marker, index)+ elif (+ marker[0] == opener[0][0]+ and len(marker) >= len(opener[0])+ and not stripped[match.end():]+ ):# Include both fence marker lines themselves.inside.update(range(opener[1], index + 1))opener = Nonediff --git a/tests/test_code_fence_closers.py b/tests/test_code_fence_closers.pynew file mode 100644index 0000000..ff46702--- /dev/null+++ b/tests/test_code_fence_closers.py@@ -0,0 +1,97 @@+"""Sample headings stay inside longer Markdown fences until a valid closer."""++import json+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 _closed_fence_line_numbers, detect_structure, main++[email protected]("marker", ["`", "~"])+def test_shorter_marker_does_not_close_outer_fence(marker):+ lines = [+ "before",+ marker * 4 + "markdown",+ marker * 3,+ "## Sample title",+ marker * 3,+ marker * 4,+ "after",+ ]++ assert _closed_fence_line_numbers(lines) == set(range(1, 6))++[email protected]("marker", ["`", "~"])+def test_marker_with_info_string_cannot_close_fence(marker):+ lines = [marker * 3, marker * 4 + "python", "## Sample title", marker * 3]++ assert _closed_fence_line_numbers(lines) == {0, 1, 2, 3}++[email protected]("marker", ["`", "~"])+def test_shorter_marker_without_valid_closer_keeps_unclosed_policy(marker):+ # The extractor deliberately leaves truly unclosed blocks reachable.+ assert _closed_fence_line_numbers([marker * 4, "text", marker * 3]) == set()++[email protected]("marker", ["`", "~"])+def test_longer_closer_with_whitespace_is_valid(marker):+ lines = [" " + marker * 3 + "python", "code", " " + marker * 5 + " \t"]++ assert _closed_fence_line_numbers(lines) == {0, 1, 2}+++def _handbook(sample):+ return (+ "# Handbook\n\n## Alpha\nFirst real section.\n\n"+ "````markdown\n```python\n"+ f"{sample}\n"+ "```\n````\n\n## Beta\nSecond real section.\n"+ )+++def test_structural_scan_ignores_headings_inside_nested_fence_example():+ structure = detect_structure(_handbook("## Sample title"))++ assert structure["chapters_detected"] == 2+ assert structure["chapters_method"] == "structural"+ assert structure["chapter_headings_sample"] == ["## Alpha", "## Beta"]+++def test_numeric_scan_ignores_chapter_examples_inside_nested_fence():+ structure = detect_structure(_handbook("Chapter 7 Example\nChapter 8 Example"))++ assert structure["chapters_detected"] == 2+ assert structure["chapters_method"] == "structural"+ assert structure["chapter_headings_sample"] == ["## Alpha", "## Beta"]+++def test_cli_records_real_chapters_without_removing_code_sample(tmp_path, monkeypatch):+ source = tmp_path / "handbook.md"+ text = _handbook("## Sample title")+ source.write_text(text, encoding="utf-8")+ output_dir = tmp_path / "output"++ monkeypatch.setattr("book_to_skill.utils.prepare_dependencies", lambda *args: None)+ monkeypatch.setattr("book_to_skill.utils.OUTPUT_DIR", output_dir)+ monkeypatch.setattr("book_to_skill.utils.OUTPUT_TEXT", output_dir / "full_text.txt")+ monkeypatch.setattr("book_to_skill.utils.OUTPUT_META", output_dir / "metadata.json")+ monkeypatch.setattr(+ sys, "argv", ["extract.py", str(source), "--install-missing", "no"]+ )++ main()++ corpus = (output_dir / "full_text.txt").read_text(encoding="utf-8")+ metadata = json.loads((output_dir / "metadata.json").read_text(encoding="utf-8"))+ assert corpus.endswith(text.rstrip())+ for record in [metadata, metadata["sources"][0]]:+ assert record["chapters_detected"] == 2+ assert record["chapters_method"] == "structural"+ assert record["chapter_headings_sample"] == ["## Alpha", "## Beta"]