Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions bibtexparser/middlewares/names.py
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,10 @@ def split_multiple_persons_names(names: str) -> list[str]:

# Escaped character.
if char == "\\":
if step == NEXT_WORD:
spans[-1].append(possible_end)
spans.append([pos - 1])
step = START_WHITESPACE
try:
next(namesiter)
# If we're at the end of the string, then the \ is just a \.
Expand Down
30 changes: 30 additions & 0 deletions tests/middleware_tests/test_names.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,36 @@ def test_split_coauthors_consistent_with_bibtex(field_value: str, expected: list
assert split_multiple_persons_names(field_value) == expected


@pytest.mark.parametrize(
"names, expected",
[
(r"John Smith and \'{E}lodie Dupont", ["John Smith", r"\'{E}lodie Dupont"]),
(r"John Smith and \AA berg", ["John Smith", r"\AA berg"]),
(
r"John Smith and \'{E}lodie Dupont and \AA berg",
["John Smith", r"\'{E}lodie Dupont", r"\AA berg"],
),
(r"John Smith a\'nd Jane Doe", [r"John Smith a\'nd Jane Doe"]),
],
)
def test_split_coauthors_preserves_escapes(names: str, expected: list[str]):
"""Escapes belong to names and cannot form part of an `and` delimiter."""
assert split_multiple_persons_names(names) == expected


def test_escaped_coauthors_survive_split_and_merge():
"""Splitting and merging coauthors must preserve their LaTeX accent commands."""
authors = r"John Smith and \'{E}lodie Dupont and \AA berg"
library = bibtexparser.parse_string(
"@article{example, author={" + authors + "}}",
append_middleware=[SeparateCoAuthors()],
)
written = bibtexparser.write_string(library, prepend_middleware=[MergeCoAuthors()])
reparsed = bibtexparser.parse_string(written)

assert reparsed.entries[0]["author"] == authors


@pytest.mark.parametrize(
"name",
[
Expand Down
Loading