Skip to content

Commit 6422e9c

Browse files
author
Mark Matchynski
committed
Issue 168 - Fix parsing references with trailing periods after book names (#168)
Fixes examples reported in #168 such as 'Micah. 2Ch. 34:20', 'Psalm. 46', 'Psalms. 74', and '1Peter. 1:22'. The parser now strips punctuation after the matched book name before interpreting chapter and verse values, avoiding empty-string int conversion errors.
1 parent bc751b1 commit 6422e9c

3 files changed

Lines changed: 84 additions & 3 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Fixed
11+
12+
- [Issue #168](https://github.com/avendesora/pythonbible/issues/168) - Parse references when book names are followed by periods.
13+
1014
## [0.15.5] - 2026-01-24
1115

1216
### Fixed

‎pythonbible/pythonbible/parser.py‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,12 +158,19 @@ def _process_sub_references(
158158
start_chapter: int | None = None
159159

160160
for sub_reference in reference.split(COMMA):
161-
if (not sub_reference or sub_reference in {DASH, PERIOD}) and not references:
161+
normalized_sub_reference = sub_reference.strip().lstrip(" .,:;-")
162+
163+
if (
164+
not normalized_sub_reference
165+
or normalized_sub_reference in {DASH, PERIOD}
166+
) and not references:
162167
references.append(NormalizedReference(book, None, None, None, None, book))
163168
continue
164169

165170
start_chapter, start_verse, end_chapter, end_verse = _process_sub_reference(
166-
sub_reference[:-1] if sub_reference.endswith(DASH) else sub_reference,
171+
normalized_sub_reference[:-1]
172+
if normalized_sub_reference.endswith(DASH)
173+
else normalized_sub_reference,
167174
book,
168175
start_chapter,
169176
)
@@ -195,7 +202,8 @@ def _process_sub_reference(
195202
end_verse: int | None = None
196203
no_verses: bool = False
197204

198-
clean_sub_reference: str = sub_reference.replace(PERIOD, COLON)
205+
clean_sub_reference: str = sub_reference.strip().lstrip(" .,:;-")
206+
clean_sub_reference = clean_sub_reference.replace(PERIOD, COLON)
199207
chapter_and_verse_range: list[str] = clean_sub_reference.split(DASH)
200208
min_chapter_and_verse: list[str] = chapter_and_verse_range[0].strip().split(COLON)
201209

‎pythonbible/tests/parser/parser_test.py‎

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,75 @@ def test_normalize_reference_range_without_verse_numbers(
116116
)
117117

118118

119+
@pytest.mark.parametrize(
120+
("text", "expected"),
121+
[
122+
(
123+
"Micah. 2",
124+
[
125+
bible.NormalizedReference(
126+
bible.Book.MICAH,
127+
2,
128+
None,
129+
2,
130+
None,
131+
bible.Book.MICAH,
132+
)
133+
],
134+
),
135+
(
136+
"Psalm. 46",
137+
[
138+
bible.NormalizedReference(
139+
bible.Book.PSALMS,
140+
46,
141+
None,
142+
46,
143+
None,
144+
bible.Book.PSALMS,
145+
)
146+
],
147+
),
148+
(
149+
"Psalms. 74",
150+
[
151+
bible.NormalizedReference(
152+
bible.Book.PSALMS,
153+
74,
154+
None,
155+
74,
156+
None,
157+
bible.Book.PSALMS,
158+
)
159+
],
160+
),
161+
(
162+
"1Peter. 1:22",
163+
[
164+
bible.NormalizedReference(
165+
bible.Book.PETER_1,
166+
1,
167+
22,
168+
1,
169+
22,
170+
bible.Book.PETER_1,
171+
)
172+
],
173+
),
174+
],
175+
)
176+
def test_get_references_book_name_followed_by_period(
177+
text: str,
178+
expected: list[bible.NormalizedReference],
179+
) -> None:
180+
# Given a reference where a book name is immediately followed by a period
181+
# When it is parsed
182+
references: list[bible.NormalizedReference] = bible.get_references(text)
183+
184+
# Then the reference is returned without crashing or discarding the chapter/verse
185+
assert references == expected
186+
187+
119188
def test_get_references_roman_numerals(
120189
roman_numeral_references: str,
121190
normalized_references_complex: list[bible.NormalizedReference],

0 commit comments

Comments
 (0)