adsfolio.bibtex.sanitize

Repair malformed field values in ADS BibTeX exports.

ADS sometimes escapes math inside keyword or abstract strings asymmetrically, e.g. 2001 KX\$\_\{76}. BibTeX counts every brace, escaped or not, so the \{ opens a group that never closes; BibTeX then reads the following entry as part of this one, reports an error, and latexmk stops before resolving any citation.

Only single-line field values whose braces do not balance are touched; well-formed output passes through unchanged.

adsfolio.bibtex.sanitize.BIBTEX_FORMATS = frozenset({'bibtex', 'bibtexabs'})

ADS export formats whose output is BibTeX.

adsfolio.bibtex.sanitize.brace_balanced(text)[source]

Check braces the way BibTeX does (backslash escapes are ignored).

Parameters:

text (str) – Field content without its outer delimiters.

Returns:

True if depth never drops below zero and ends at zero.

Return type:

bool

adsfolio.bibtex.sanitize.remove_fields(text, fields)[source]

Remove named fields from every BibTeX entry and keep the other text.

Parameters:
  • text (str) – BibTeX text, for example from the ADS export service.

  • fields (Iterable[str]) – Field names to remove, ignoring letter case, for example ("abstract", "adsnote").

Returns:

The same text without those fields. Other fields, their order and spacing, comments, and @string or @preamble blocks are kept. An entry whose braces do not close is kept unchanged.

Return type:

str

adsfolio.bibtex.sanitize.repair_bibtex(text)[source]

Repair brace-unbalanced field values in ADS BibTeX output.

Parameters:

text (str) – BibTeX as returned by the ADS export service (one field per line).

Returns:

The same text with each unbalanced field value repaired by repair_field_text. A warning is logged per repaired field.

Return type:

str

adsfolio.bibtex.sanitize.repair_field_text(text)[source]

Return a brace-balanced version of a field’s content.

Parameters:

text (str) – Field content without its outer delimiters.

Returns:

text unchanged if already balanced. Otherwise ADS escapes of $ _ { } ^ are undone, open groups are closed, and an unpaired $ is closed. If that still does not balance, unmatched braces are dropped instead of closed.

Return type:

str

Examples

>>> repair_field_text(r"28978 Ixion: 2001 KX\$\_\{76")
'28978 Ixion: 2001 KX$_{76}$'
adsfolio.bibtex.sanitize.split_entry(text)[source]

Return the type, key, and raw field values of the first entry.

Parameters:

text (str) – BibTeX text that starts with one entry, for example from ADS.

Returns:

(entry_type, cite_key, fields). Field names are lower case. Values keep their delimiters and macros, for example jan. None when no complete entry is found.

Return type:

tuple or None

adsfolio.bibtex.sanitize.with_cite_key(text, cite_key)[source]

Return the text with the citation key of the first entry replaced.

Parameters:
Return type:

str