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
|