Skip to content

bib.bibtexparser.middlewares.block.entry_fields_sort

Classes

SortFieldsAlphabeticallyMiddleware

SortFieldsAlphabeticallyMiddleware(
    allow_inplace_modification=True,
)

Bases: BlockMiddleware

Sort the fields of an entry alphabetically by key.

Source code in pybibtexer/bib/bibtexparser/middlewares/block/entry_fields_sort.py
def __init__(self, allow_inplace_modification: bool = True):
    super().__init__(allow_inplace_modification=allow_inplace_modification, allow_parallel_execution=True)

SortFieldsCustomMiddleware

SortFieldsCustomMiddleware(
    order,
    case_sensitive=False,
    allow_inplace_modification=True,
)

Bases: BlockMiddleware

Sort the fields of an entry according to a custom order provided by user.

The order is a list of field keys. Fields not in the list are put at the end.

Source code in pybibtexer/bib/bibtexparser/middlewares/block/entry_fields_sort.py
def __init__(self, order: tuple[str, ...], case_sensitive: bool = False, allow_inplace_modification: bool = True):
    super().__init__(allow_inplace_modification=allow_inplace_modification, allow_parallel_execution=True)
    self._case_sensitive = case_sensitive
    if not case_sensitive:
        self._order = [x.lower() for x in order]
    else:
        self._order = order

    if len(self._order) != len(set(self._order)):
        duplicate_keys = {x for x in self._order if self._order.count(x) > 1}
        raise ValueError(
            "Order list must not contain duplicates. "
            "The following keys are duplicated: "
            f"{', '.join(duplicate_keys)}"
        )