From 9a2f1dd07416ec582e5c39825ee5bbd9f8ebc529 Mon Sep 17 00:00:00 2001 From: Jay Hemnani <193022578+jayhemnani9910@users.noreply.github.com> Date: Thu, 29 Oct 2026 13:43:56 +0530 Subject: [PATCH] fix: format BibTeX names with Jr parts and braced "and" Authors were split on every " and " after braces were removed, so a braced corporate name such as {Food and Agriculture Organization} became two people, and "King, Jr., Martin Luther" came out as "Jr., Martin Luther King". Split names and name parts only outside braces, and order the three-part "Last, Jr, First" form as "First Last Jr". --- deeptutor/utils/bibtex_converter.py | 46 ++++++++++++++++++++++------ tests/utils/test_bibtex_converter.py | 14 +++++++++ 2 files changed, 51 insertions(+), 9 deletions(-) diff --git a/deeptutor/utils/bibtex_converter.py b/deeptutor/utils/bibtex_converter.py index 1727365a2..8d95aa6a2 100644 --- a/deeptutor/utils/bibtex_converter.py +++ b/deeptutor/utils/bibtex_converter.py @@ -8,6 +8,8 @@ import re _ENTRY_START_RE = re.compile(r"@([A-Za-z][A-Za-z0-9_+.-]*)\s*([({])") _IDENTIFIER_RE = re.compile(r"[A-Za-z][A-Za-z0-9_.+:-]*") _WHITESPACE_RE = re.compile(r"\s+") +_NAME_SEPARATOR_RE = re.compile(r"\s+and\s+", re.IGNORECASE) +_NAME_PART_SEPARATOR_RE = re.compile(r",") _DELIMITERS = {"{": "}", "(": ")"} _METADATA_ENTRY_TYPES = {"comment", "string", "preamble"} @@ -53,13 +55,39 @@ def _clean_value(value: str) -> str: return _WHITESPACE_RE.sub(" ", cleaned).strip() +def _split_top_level(value: str, separator: re.Pattern[str]) -> list[str]: + """Split on separator matches outside braces; braces protect a group.""" + parts: list[str] = [] + depth = 0 + start = index = 0 + while index < len(value): + char = value[index] + if char == "{": + depth += 1 + elif char == "}": + depth -= 1 + elif depth == 0: + match = separator.match(value, index) + if match is not None: + parts.append(value[start:index]) + start = index = match.end() + continue + index += 1 + parts.append(value[start:]) + return parts + + def _format_authors(value: str) -> str: names: list[str] = [] - for raw_name in _clean_value(value).split(" and "): - name = raw_name.strip() - if "," in name: - last, first = (part.strip() for part in name.split(",", 1)) - name = f"{first} {last}".strip() + for raw_name in _split_top_level(value, _NAME_SEPARATOR_RE): + parts = [part.strip() for part in _split_top_level(raw_name, _NAME_PART_SEPARATOR_RE)] + if len(parts) == 2: + # "Last, First" + parts = [parts[1], parts[0]] + elif len(parts) > 2: + # "Last, Jr, First" + parts = [", ".join(parts[2:]), parts[0], parts[1]] + name = _clean_value(" ".join(parts)) if name: names.append(name) return ", ".join(names) @@ -79,17 +107,16 @@ def _format_entry(entry: _Entry, index: int) -> str: entry_type = _TYPE_LABELS.get( entry.entry_type, entry.entry_type.replace("_", " ").title() or "Reference" ) - title = entry.fields.get("title") or "Untitled" + title = _clean_value(entry.fields.get("title", "")) or "Untitled" lines = [f"## {index}. {title}", "", f"**Type:** {entry_type}"] citation_key = entry.key.replace("`", "'") or "unknown" lines.append(f"**Citation key:** `{citation_key}`") for field in _DISPLAY_FIELDS: value = entry.fields.get(field, "") + value = _format_authors(value) if field == "author" else _clean_value(value) if not value: continue - if field == "author": - value = _format_authors(value) lines.append(f"**{_field_label(field)}:** {value}") lines.append("") return "\n".join(lines) @@ -178,7 +205,8 @@ def _read_fields(body: str) -> dict[str, str]: continue break - fields[name] = _clean_value("".join(parts)) + # Braces are kept here: in author lists they protect "and" and commas. + fields[name] = "".join(parts) index = _skip_whitespace(body, index) if index < len(body) and body[index] == ",": index += 1 diff --git a/tests/utils/test_bibtex_converter.py b/tests/utils/test_bibtex_converter.py index 63f9a0e2c..f3fe9c791 100644 --- a/tests/utils/test_bibtex_converter.py +++ b/tests/utils/test_bibtex_converter.py @@ -92,3 +92,17 @@ def test_bibtex_to_markdown_keeps_entry_with_accent_quote_in_braces() -> None: assert "## 1. Deep Learning" in result assert "mueller2020" in result assert "## 2. Second Paper" in result + + +def test_bibtex_to_markdown_formats_jr_and_braced_author_names() -> None: + text = r""" + @article{king1963, + author = {King, Jr., Martin Luther and {Food and Agriculture Organization}}, + title = {Letter}, + year = {1963} + } + """ + + result = bibtex_to_markdown(text) + + assert "**Authors:** Martin Luther King Jr., Food and Agriculture Organization" in result