Skip to content

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

feat(extractor): detect Russian (Глава) chapter headings (#199)

Shubham Srivastava411706451cd0
diff --git a/book_to_skill/utils.py b/book_to_skill/utils.py
index 89442d4..ef8c914 100644
--- a/book_to_skill/utils.py
+++ b/book_to_skill/utils.py
@@ -198,6 +198,15 @@ _BN_CHAPTER = re.compile(
rf"^\s*(?:#{{1,6}}\s+)?অধ্যায়\s*([0-9{_BN_DIGITS}]+)\b"
)
+# Russian (Cyrillic) chapter headings: "Глава 1", "ГЛАВА 12", "## Глава 2".
+# "Глава" ("chapter") + a number. Cyrillic uses ordinary Arabic digits, so —
+# unlike the Devanagari/Bengali blocks above — no digit remap is needed. A
+# dedicated matcher (rather than adding the word to _EXPLICIT_CHAPTER) is used
+# because that alternation is Latin-only and its number would still be read
+# there. Requiring whitespace then a number keeps prose that merely uses an
+# inflected form ("В этой главе…", "Главная страница") from matching.
+_RU_CHAPTER = re.compile(r"^\s*(?:#{1,6}\s+)?глава\s+([0-9]+)\b", re.IGNORECASE)
+
# Korean chapter headings: "제1장 총칙", "## 제4장 근로시간과 휴식", "제6장의2 …".
# 제 + Arabic numeral + a classifier (장 chapter / 편 part / 절 section / 관
# subsection), with an optional "의N" branch suffix that Korean statutes use for
@@ -563,6 +572,9 @@ def _match_chapter_number(line: str) -> int | None:
bm = _BN_CHAPTER.match(s)
if bm:
return int(bm.group(1).translate(_BN_DIGIT_MAP))
+ rum = _RU_CHAPTER.match(s)
+ if rum:
+ return int(rum.group(1))
km = _KO_CHAPTER.match(s)
if km:
@@ -583,6 +595,7 @@ def _chapter_number(line: str) -> int | None:
Chinese ("第三章 …", "## 一 · …", "## 第一讲"), Thai ("บทที่ 3",
"## บทที่ ๑"), Hindi ("अध्याय 1", "अध्याय १", "## अध्याय 2"),
Bengali ("অধ্যায় 1", "অধ্যায় ১", "## অধ্যায় 2"),
+ Russian ("Глава 1", "ГЛАВА 12", "## Глава 2"),
Korean ("제1장 총칙", "## 제4장 근로시간과 휴식"), and
Persian ("فصل ۱", "فصل اول", "فصل بیست و یکم", "بخش ۲: مفاهیم",
"## فصل ۱: مقدمه", PDF-glued "فصل سی و چهارمخداحافظ…") heading styles — each
diff --git a/tests/test_book_to_skill.py b/tests/test_book_to_skill.py
index 86add88..b910441 100644
--- a/tests/test_book_to_skill.py
+++ b/tests/test_book_to_skill.py
@@ -668,6 +668,27 @@ class TestDetectStructure:
assert _chapter_number("এই অধ্যায়ে আমরা আলোচনা করব") is None
assert _chapter_number("অধ্যায়") is None
+ def test_detects_russian_chapters(self):
+ """Russian headings: `Глава N`, case-insensitive, with Arabic digits."""
+ text = (
+ "Глава 1 Введение\nсодержание\n"
+ "ГЛАВА 2 Методы\nсодержание\n"
+ "Глава 3 Результаты\nсодержание"
+ )
+ assert detect_structure(text)["chapters_detected"] == 3
+
+ def test_russian_markdown_prefix(self):
+ text = "## Глава 1 Первая\nсодержание\n## Глава 2 Вторая\nсодержание"
+ assert detect_structure(text)["chapters_detected"] == 2
+
+ def test_russian_prose_is_not_a_chapter_heading(self):
+ """An inflected form or a different word (Главная) is not a heading."""
+ from book_to_skill.utils import _chapter_number
+
+ assert _chapter_number("В этой главе мы обсудим") is None
+ assert _chapter_number("Главная страница") is None
+ assert _chapter_number("Глава") is None
+
# ── Korean chapter headings ────────────────────────────────────────────
def test_korean_je_n_jang(self):