Skip to content

Commit 6fe2c4d

Browse files
committed
Require exact special command types
Treat custom entry types that extend comment, preamble, or string names as ordinary entries.
1 parent 08e370a commit 6fe2c4d

2 files changed

Lines changed: 23 additions & 3 deletions

File tree

‎bibtexparser/splitter.py‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ def split(self, library: Library | None = None) -> Library:
300300
m_val = m.group(0).lower()
301301

302302
if m_val.startswith("@"):
303+
block_type = m_val[1:].strip()
303304
# Clean up previous block implicit_comment
304305
implicit_comment = self._end_implicit_comment(m.start())
305306
if implicit_comment is not None:
@@ -309,11 +310,11 @@ def split(self, library: Library | None = None) -> Library:
309310
start_line = self._current_line
310311
try:
311312
# Start new block parsing
312-
if m_val.startswith("@comment"):
313+
if block_type == "comment":
313314
library.add(self._handle_explicit_comment())
314-
elif m_val.startswith("@preamble"):
315+
elif block_type == "preamble":
315316
library.add(self._handle_preamble())
316-
elif m_val.startswith("@string"):
317+
elif block_type == "string":
317318
library.add(self._handle_string(m), fail_on_duplicate_key=False)
318319
else:
319320
library.add(self._handle_entry(m, m_val), fail_on_duplicate_key=False)

‎tests/splitter_tests/test_splitter_block_start_detection.py‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,25 @@ def test_mixed_blocks_same_line(
154154
assert len(library.comments) == expected_comments
155155

156156

157+
@pytest.mark.parametrize(
158+
"entry_type",
159+
[
160+
pytest.param("commentary", id="comment-prefix"),
161+
pytest.param("preamble_study", id="preamble-prefix"),
162+
pytest.param("string_theory", id="string-prefix"),
163+
],
164+
)
165+
def test_entry_type_extending_special_command_name_is_entry(entry_type: str):
166+
"""Special commands require an exact type match; longer custom types remain entries."""
167+
library = Splitter(f"@{entry_type}{{key, title = {{Custom entry type}}}}").split()
168+
169+
assert len(library.failed_blocks) == 0
170+
assert len(library.comments) == 0
171+
assert len(library.preambles) == 0
172+
assert len(library.strings) == 0
173+
assert [(entry.entry_type, entry.key) for entry in library.entries] == [(entry_type, "key")]
174+
175+
157176
# =============================================================================
158177
# Test: Error recovery when new block starts at line start
159178
# =============================================================================

0 commit comments

Comments
 (0)