Skip to content

bib.core.convert_library_to_str

Classes

ConvertLibrayToStr

ConvertLibrayToStr(options)

Convert library to str.

Parameters:

Name Type Description Default
options dict[str, Any]

Options. Default is {}.

required

Attributes:

Name Type Description
is_standardize_library bool

Is standardize library. Default is False.

empty_entry_cite_keys bool

Empty entry cite keys. Default is False.

add_index_to_entries bool

Add index to entries. Default is False.

entries_necessary bool

Is the entries are necessary in the bib file.

Source code in pybibtexer/bib/core/convert_library_to_str.py
def __init__(self, options: dict[str, Any]):
    self.is_standardize_library = options.get("is_standardize_library", False)
    self.empty_entry_cite_keys = options.get("empty_entry_cite_keys", False)
    self.add_index_to_entries = options.get("add_index_to_entries", False)
    self.entries_necessary = options.get("entries_necessary", True)

    self.options = options

Functions

generate_str
generate_str(library, bibtex_format=None)

Serialize a BibTeX database.

:param library: BibTeX database to serialize. :param bibtex_format: Customized BibTeX format to use (optional).

Source code in pybibtexer/bib/core/convert_library_to_str.py
def generate_str(self, library: Library | list[Block], bibtex_format: BibtexFormat | None = None) -> list[str]:
    """Serialize a BibTeX database.

    :param library: BibTeX database to serialize.
    :param bibtex_format: Customized BibTeX format to use (optional).
    """
    # --------- --------- --------- #
    if not isinstance(library, Library):
        library = Library(library)

    # standardizer
    if self.is_standardize_library:
        library = ConvertLibrayToLibrary(self.options).generate_single_library(library)

    # --------- --------- --------- #
    library = MiddlewaresLibraryToStr(self.options).functions(library)

    # --------- --------- --------- #
    if bibtex_format is None:
        bibtex_format = BibtexFormat()

    if bibtex_format.value_column == "auto":
        auto_val: int = self._calculate_auto_value_align(library)
        # Copy the format instance to avoid modifying the original
        # (which would be bad if the format is used for multiple libraries)
        bibtex_format = deepcopy(bibtex_format)
        bibtex_format.value_column = auto_val

    # --------- --------- --------- #
    if self.entries_necessary:
        if not library.entries:
            return []

    data_list = []
    j = 0
    for i, block in enumerate(library.blocks):
        if self.add_index_to_entries and isinstance(block, Entry):
            data_list.append(f"% {j + 1}\n")
            j += 1

        # Get str representation of block
        pieces = self._treat_block(bibtex_format, block)
        data_list.extend(pieces)

        # Separate Blocks
        if i < len(library.blocks) - 1:
            data_list.append(bibtex_format.block_separator)
    return data_list