GNU TeXmacsUser manualReferenceAboutEnglish
Parsing and serializing .bib files

1.The parser

tree parse_bib (string s) (

Data/Convert/BibTeX/parsebib.cpp

, glue parse-bib) parses the text of a .bib file. It is a hand written recursive descent parser which never fails: errors are reported on the convert-error debug channel (with the key of the entry being parsed, bib_current_tag) and the parser resynchronizes as well as it can. It returns an empty tree if the input or the result is empty.

1.1.Accepted syntax

1.2.Abbreviations

All @string definitions of the file are collected first and turned into a dictionary by bib_strings_dict (

Plugins/Bibtex/bibtex_functions.cpp

). The dictionary is pre-filled with the twenty journal abbreviations of the standard BibTEX styles (acmcs, cacm, jacm, tcs, …); the month abbreviations jan, …, dec, which BibTEX styles normally define, are not included, so month = jan yields the string jan. Names are compared case-insensitively when a field is expanded (bib_subst_vars). An unknown abbreviation which is the whole value is replaced by its own name, but inside a # concatenation it is replaced by nothing. The dictionary is built in the order of the definitions, so a definition may refer to earlier ones, but only with the same case (see the pitfalls). After the substitution, the @string and @preamble items are dropped from the result: parse_bib only returns entries and comments.

1.3.The resulting tree

(document
  (bib-entry "article" "knuth84"
    (document
      (bib-field "author" (bib-names (bib-name "Donald E." "" "Knuth" "")))
      (bib-field "title" "Literate programming")
      (bib-field "pages" (bib-pages "97" "111"))
      (bib-field "url" (slink "https://…"))))
  (bib-comment (document (bib-line " a comment")))
  …)

The type and the key are strings; every field value is a TeXmacs tree. The same format is consumed by bib_entries and the Scheme styles, and produced from database entries by db->bib (see the database chapter).

2.Conversion of field values

2.1.LaTeX to TeXmacs

Field values are written in LaTeX. Converting each of them separately with the LaTeX parser would be slow, so bib_parse_fields converts all fields of the file at once:

  1. bib_get_fields walks through all entries (also inside comments) and concatenates every field value into one LaTeX string, each preceded by the separator \nextbib{}. Each value is first rewritten by bib_to_latex, which turns BibTEX braces into \keepcase{…} (see below), escapes % and protects [ and ]. Author and editor fields are split into names at this point (next subsection); pages is skipped.

  2. The string is parsed once with parse_latex and converted with latex_to_tree; bib_latex_array cuts the result back into pieces at the nextbib separators.

  3. If the number of pieces is one less than expected and the string ends with an empty field, the conversion is redone with a sentinel {xyzyx} appended (the parser drops a trailing empty argument).

  4. Atomic values which look like URLs (no space, http://, https:// or ftp://) become slink.

  5. Only if the number of pieces equals the number of fields are the values put back (bib_set_fields). Otherwise no field of the file is converted, and all values stay raw LaTeX strings.

Braces and case.
In BibTEX, a brace group at depth one protects its contents against case changes, unless it starts with a backslash (a “special character” such as {\"o}). bib_to_latex renders such groups as keepcase; braces of special characters and math ($…$, which is passed through unchanged) are kept for the LaTeX parser. The built-in case functions then respect keepcase (see the built-in functions).

2.2.Person names

bib_names splits an author or editor field at the word and at brace depth zero (search_and_keyword) and parses each name with get_first_von_last, following the three BibTEX forms:

First von Last

(no comma, get_fvl). If all words start with an upper case letter, the last word is the last name and the others are the first names. Otherwise the last name is the longest run of capitalized words at the end, the first names are the capitalized words at the beginning, and the von part is what lies in between. Jean de La Fontaine gives first Jean, von de, last La Fontaine.

von Last, First

(one comma, get_vl_f). The part before the comma is split into von and last in the same way.

von Last, Jr, First

(two commas, get_vl_j_f).

A word “starts with a lower case letter” if its first ordinary character is lower case at brace depth zero, or inside a special character (first_is_locase). Each name becomes (bib-name first von last jr), with empty strings for missing parts, and the list is wrapped in bib-names.

2.3.Page ranges

bib_field_pages reads the first integer of the value and, after skipping non-digits, a second one, and returns (bib-pages from to) or (bib-pages from). Only decimal digits are understood: a value which does not start with a digit becomes (bib-pages "0"), see the pitfalls.

3.Selection of the cited entries

For the internal styles without the database tool, generate_bibliography appends texmacs.bib to the .bib file, parses the whole text and calls tree bib_entries (tree t, tree bib_t). This function (bib_select_entries) indexes the entries by key, warns about duplicate keys (the first one wins) and about cited keys which are not found (on the bibtex-warning channel), and returns the cited entries in the order of the citations. An entry with a crossref field adds the referenced key at the end of the list of keys to process, so that it is included once, after the cited ones. The fields of the referenced entry are not merged into the citing one; as in the standard BibTEX styles, the shipped styles instead write “in” followed by a citation of the referenced entry when an entry has a crossref field. A citation * (from nocite*) is expanded beforehand into all keys of the .bib file.

4.Serialization

The format bibtex (hidden, suffix rawbib) and its converters are registered in

convert/bibtex/init-bibtex.scm

: bibtex-document and bibtex-snippet are parsed with parse-bib, and the inverse is serialize-bibtex (

convert/bibtex/bibtexout.scm

). The user visible format tmbib (suffix bib) goes through the database representation (

database/bib-db.scm

) but uses the same parser and serializer.

serialize-bibtex writes one item per entry, with field names padded to twelve characters. A value is written as follows:

Note that atomic strings are not converted: special characters such as % and non-ASCII Cork characters are written as they are. When a .bib file is edited as a document, the conservative export (see the database chapter) keeps the original text of the unchanged entries, so this only affects new and modified entries.

5.The external program

The way bibtex_run drives the external program is described in the database chapter. A few additional details:

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; with no Invariant Sections, with no Front-Cover Texts, and with no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License".