BibtexParser v2 uses a middleware-based architecture for advanced transformations. The core parser only handles splitting input into blocks (entries, strings, etc.) and fields. For operations like sorting, encoding, or name splitting, you use middleware layers.
Middleware layers are helper classes that take a library object and return a new, transformed version of it. You can add these layers to your parse or write stack to customize how Bibtex data is processed.
There are two main types of middleware:
- BlockMiddleware: Operates on individual entries/blocks (e.g., transforming a specific field value).
- LibraryMiddleware: Operates on the entire library (e.g., sorting all entries).
To use middleware, you pass them to bibtexparser.parse(), bibtexparser.parse_file(), bibtexparser.write(), or bibtexparser.write_file() using specific arguments to control whether they are added to or replace the default stack.
import bibtexparser.middlewares as m
# Adding layers to the parse stack
layers = [
m.MonthIntMiddleware(),
m.SeparateCoAuthors(),
m.SplitNameParts()
]
library = bibtexparser.parse_file('bibtex.bib', append_middleware=layers)