Skip to content

Commit 06c7abf

Browse files
author
Ang
committed
fix #30: use bibtexparser.dumps() for BibTeX output rendering
Replace hand-built string formatting in _format_bibtex() with bibtexparser.dumps() via BibDatabase + BibTexWriter. bibtexparser was already used for input parsing; this closes the inconsistency. Closes #30
1 parent d7a62c7 commit 06c7abf

1 file changed

Lines changed: 14 additions & 23 deletions

File tree

onecite/pipeline.py

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3124,33 +3124,24 @@ def format(self, completed_entries: List[CompletedEntry],
31243124
}
31253125

31263126
def _format_bibtex(self, entry: CompletedEntry) -> str:
3127-
"""Format to BibTeX format"""
3127+
"""Format to BibTeX using bibtexparser.dumps() for standards-compliant output."""
31283128
bib_data = entry['bib_data']
31293129
entry_type = bib_data.get('ENTRYTYPE', 'article')
31303130
entry_id = bib_data.get('ID', entry['bib_key'])
3131-
3132-
lines = [f"@{entry_type}{{{entry_id},"]
3133-
3131+
3132+
record = {'ENTRYTYPE': entry_type, 'ID': entry_id}
31343133
for key, value in bib_data.items():
3135-
if key not in ['ENTRYTYPE', 'ID'] and value:
3136-
# Preserve LaTeX escape sequences
3137-
# Don't strip braces blindly, they may be part of LaTeX commands
3134+
if key not in ('ENTRYTYPE', 'ID') and value:
31383135
value_str = str(value)
3139-
3140-
# Only for fields that should have LaTeX escaping (author, title, etc.)
3141-
if key in ['author', 'title', 'journal', 'publisher', 'note',
3142-
'booktitle', 'series', 'address', 'howpublished']:
3143-
# Convert Unicode characters to LaTeX escape sequences
3144-
clean_value = self._escape_latex_chars(value_str)
3136+
if key in ('author', 'title', 'journal', 'publisher', 'note',
3137+
'booktitle', 'series', 'address', 'howpublished'):
3138+
record[key] = self._escape_latex_chars(value_str)
31453139
else:
3146-
# For other fields, just use as-is but remove outer braces if they exist
3147-
clean_value = value_str.strip('{}')
3148-
3149-
if key in ['volume', 'number', 'year']:
3150-
lines.append(f" {key} = {clean_value},")
3151-
else:
3152-
lines.append(f' {key} = "{clean_value}",')
3153-
3154-
lines.append('}')
3155-
return '\n'.join(lines)
3140+
record[key] = value_str.strip('{}')
3141+
3142+
db = bibtexparser.bibdatabase.BibDatabase()
3143+
db.entries = [record]
3144+
writer = bibtexparser.bwriter.BibTexWriter()
3145+
writer.indent = ' '
3146+
return bibtexparser.dumps(db, writer).strip()
31563147

0 commit comments

Comments
 (0)