Skip to content

Commit

Permalink
🐛 Fix bug in von-part name parsing (#423)
Browse files Browse the repository at this point in the history
  • Loading branch information
mlutze authored Nov 24, 2023
1 parent ed46b28 commit d670c18
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
14 changes: 10 additions & 4 deletions bibtexparser/middlewares/names.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,11 +424,17 @@ def parse_single_name_into_parts(name, strict=True):
else:
lcases = cases[0]

#
def rindex(l, x, default):
"""Returns the index of the rightmost occurence of x in l."""
for i in range(len(l) - 1, -1, -1):
if l[i] == x:
return i
return default

# Check if at least one of the words is lowercase
if 0 in lcases:
split = len(lcases) - lcases[::-1].index(0)
if split == len(lcases):
split = 0 # Last cannot be empty.
# Excluding the last word, find the index of the last lower word
split = rindex(lcases[:-1], 0, -1) + 1
parts.von = sections[0][:split]
parts.last = sections[0][split:]

Expand Down
21 changes: 21 additions & 0 deletions tests/middleware_tests/test_names.py
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,27 @@ def test_name_splitting_commas_at_higher_brace_level(strict: bool):
r"Brand\~{a}o, F",
{"first": ["F"], "von": [], "last": ["Brand\\", "{a}o"], "jr": []},
),
###############################################################################
#
# Group 2 examples from Tame the BeaST
#
###############################################################################
(
r"de la fontaine, Jean",
{"first": ["Jean"], "von": ["de", "la"], "last": ["fontaine"], "jr": []},
),
(
r"De La Fontaine, Jean",
{"first": ["Jean"], "von": [], "last": ["De", "La", "Fontaine"], "jr": []},
),
(
r"De la Fontaine, Jean",
{"first": ["Jean"], "von": ["De", "la"], "last": ["Fontaine"], "jr": []},
),
(
r"de La Fontaine, Jean",
{"first": ["Jean"], "von": ["de"], "last": ["La", "Fontaine"], "jr": []},
),
)


Expand Down

0 comments on commit d670c18

Please sign in to comment.