def make_bibliography(self, aux_filename, style=None, output_encoding=None, bib_format=None, **kwargs): """ Read the given ``.aux`` file and produce a formatted bibliography using :py:meth:`~.Engine.format_from_files`. :param style: If not ``None``, use this style instead of specified in the ``.aux`` file. """ from pybtex import auxfile if bib_format is None: from pybtex.database.input.bibtex import Parser as bib_format aux_data = auxfile.parse_file(aux_filename, output_encoding) if style is None: style = aux_data.style base_filename = path.splitext(aux_filename)[0] bib_filenames = [filename + bib_format.default_suffix for filename in aux_data.data] return self.format_from_files( bib_filenames, style=aux_data.style, citations=aux_data.citations, output_encoding=output_encoding, output_filename=base_filename, add_output_suffix=True, )
def check_format_from_string(engine, filenames): filenames_by_suffix = group_by_suffix(filenames) engine_name = engine.__name__.rsplit('.', 1)[-1] if '.aux' in filenames_by_suffix: from io import StringIO from pybtex import auxfile aux_contents = StringIO(read_file(filenames_by_suffix['.aux'])) auxdata = auxfile.parse_file(aux_contents) citations = auxdata.citations style = auxdata.style else: citations = '*' style = posixpath.splitext(filenames_by_suffix['.bst'])[0] with cd_tempdir() as tempdir: copy_file(filenames_by_suffix['.bst']) bib_name = posixpath.splitext(filenames_by_suffix['.bib'])[0] bib_string = read_file(filenames_by_suffix['.bib']) with errors.capture() as captured_errors: # FIXME check error messages result = engine.format_from_string(bib_string, style=style, citations=citations) correct_result_name = '{0}_{1}.{2}.bbl'.format(bib_name, style, engine_name) correct_result = pkgutil.get_data('pybtex.tests.data', correct_result_name).decode( io.get_default_encoding()) assert result == correct_result, diff(correct_result, result)
def check_format_from_string(engine, filenames): filenames_by_suffix = group_by_suffix(filenames) engine_name = engine.__name__.rsplit('.', 1)[-1] if '.aux' in filenames_by_suffix: from io import StringIO from pybtex import auxfile aux_contents = StringIO(get_data(filenames_by_suffix['.aux'])) auxdata = auxfile.parse_file(aux_contents) citations = auxdata.citations style = auxdata.style else: citations = '*' style = posixpath.splitext(filenames_by_suffix['.bst'])[0] with cd_tempdir(): copy_file(filenames_by_suffix['.bst']) bib_name = posixpath.splitext(filenames_by_suffix['.bib'])[0] bib_string = get_data(filenames_by_suffix['.bib']) with errors.capture(): # FIXME check error messages result = engine.format_from_string(bib_string, style=style, citations=citations) correct_result_name = '{0}_{1}.{2}.bbl'.format(bib_name, style, engine_name) correct_result = get_data(correct_result_name).replace('\r\n', '\n') assert result == correct_result, diff(correct_result, result)
def make_bibliography(aux_filename, bib_format=None, bib_encoding=None, output_encoding=None, bst_encoding=None, min_crossrefs=2, **kwargs): from os import path import pybtex.io from pybtex.bibtex import bst from pybtex.bibtex.interpreter import Interpreter from pybtex import auxfile if bib_format is None: from pybtex.database.input.bibtex import Parser as bib_format aux_data = auxfile.parse_file(aux_filename, output_encoding) bst_filename = aux_data.style + path.extsep + 'bst' bst_script = bst.parse_file(bst_filename, bst_encoding) base_filename = path.splitext(aux_filename)[0] bbl_filename = base_filename + path.extsep + 'bbl' bib_filenames = [ filename + bib_format.get_default_suffix() for filename in aux_data.data ] bbl_file = pybtex.io.open_unicode(bbl_filename, 'w', encoding=output_encoding) interpreter = Interpreter(bib_format, bib_encoding) interpreter.run(bst_script, aux_data.citations, bib_filenames, bbl_file, min_crossrefs=min_crossrefs)
def make_bibliography(aux_filename, bib_format=None, bib_encoding=None, output_encoding=None, bst_encoding=None, min_crossrefs=2, **kwargs ): from os import path import pybtex.io from pybtex.bibtex import bst from pybtex.bibtex.interpreter import Interpreter from pybtex import auxfile if bib_format is None: from pybtex.database.input.bibtex import Parser as bib_format aux_data = auxfile.parse_file(aux_filename, output_encoding) bst_filename = aux_data.style + path.extsep + 'bst' bst_script = bst.parse_file(bst_filename, bst_encoding) base_filename = path.splitext(aux_filename)[0] bbl_filename = base_filename + path.extsep + 'bbl' bib_filenames = [filename + bib_format.get_default_suffix() for filename in aux_data.data] bbl_file = pybtex.io.open_unicode(bbl_filename, 'w', encoding=output_encoding) interpreter = Interpreter(bib_format, bib_encoding) interpreter.run(bst_script, aux_data.citations, bib_filenames, bbl_file, min_crossrefs=min_crossrefs)
def make_bibliography(aux_filename, bib_format=None, bib_encoding=None, output_encoding=None, output_backend=None, min_crossrefs=2, **kwargs ): """This functions extracts all nessessary information from .aux file and writes the bibliography. """ from os import path from pybtex import auxfile from pybtex.plugin import find_plugin from pybtex.style import FormattedBibliography filename = path.splitext(aux_filename)[0] aux_data = auxfile.parse_file(aux_filename, output_encoding) output_backend = find_plugin('pybtex.backends', output_backend) bib_parser = find_plugin('pybtex.database.input', bib_format) bib_data = bib_parser( encoding=bib_encoding, wanted_entries=aux_data.citations, min_crossrefs=min_crossrefs, ).parse_files(aux_data.data, bib_parser.get_default_suffix()) style_cls = find_plugin('pybtex.style.formatting', aux_data.style) style = style_cls( label_style=kwargs.get('label_style'), name_style=kwargs.get('name_style'), sorting_style=kwargs.get('sorting_style'), abbreviate_names=kwargs.get('abbreviate_names'), ) citations = bib_data.add_extra_citations(aux_data.citations, min_crossrefs) entries = (bib_data.entries[key] for key in citations) formatted_entries = style.format_entries(entries) del entries formatted_bibliography = FormattedBibliography(formatted_entries, style) output_filename = filename + output_backend.get_default_suffix() output_backend(output_encoding).write_to_file(formatted_bibliography, output_filename)
def make_bibliography(aux_filename, bib_format=None, bib_encoding=None, output_encoding=None, output_backend=None, min_crossrefs=2, **kwargs): """This functions extracts all nessessary information from .aux file and writes the bibliography. """ from os import path from pybtex import auxfile from pybtex.plugin import find_plugin from pybtex.style import FormattedBibliography filename = path.splitext(aux_filename)[0] aux_data = auxfile.parse_file(aux_filename, output_encoding) output_backend = find_plugin('pybtex.backends', output_backend) bib_parser = find_plugin('pybtex.database.input', bib_format) bib_data = bib_parser( encoding=bib_encoding, wanted_entries=aux_data.citations, min_crossrefs=min_crossrefs, ).parse_files(aux_data.data, bib_parser.get_default_suffix()) style_cls = find_plugin('pybtex.style.formatting', aux_data.style) style = style_cls( label_style=kwargs.get('label_style'), name_style=kwargs.get('name_style'), sorting_style=kwargs.get('sorting_style'), abbreviate_names=kwargs.get('abbreviate_names'), ) citations = bib_data.add_extra_citations(aux_data.citations, min_crossrefs) entries = (bib_data.entries[key] for key in citations) formatted_entries = style.format_entries(entries) del entries formatted_bibliography = FormattedBibliography(formatted_entries, style) output_filename = filename + output_backend.get_default_suffix() output_backend(output_encoding).write_to_file(formatted_bibliography, output_filename)
def make_bibliography(aux_filename, bib_format=None, bib_encoding=None, output_encoding=None, **kwargs ): """This functions extracts all nessessary information from .aux file and writes the bibliography. """ from os import path from pybtex import auxfile from pybtex.plugin import find_plugin filename = path.splitext(aux_filename)[0] aux_data = auxfile.parse_file(aux_filename, output_encoding) if bib_format is None: from pybtex.database.input import bibtex as bib_format try: output_backend = kwargs['output_backend'] except KeyError: from pybtex.backends.latex import Backend as output_backend bib_format = bib_format.Parser bib_data = bib_format(bib_encoding).parse_files(aux_data.data, bib_format.get_default_suffix()) style_cls = find_plugin('pybtex.style.formatting', aux_data.style) style = style_cls( label_style=kwargs.get('label_style'), name_style=kwargs.get('name_style'), abbreviate_names=kwargs.get('abbreviate_names', True), ) entries = ((key, bib_data.entries[key]) for key in aux_data.citations) formatted_entries = style.format_entries(entries) del entries output_filename = filename + output_backend.get_default_suffix() output_backend(output_encoding).write_bibliography(formatted_entries, output_filename)
def check_format_from_string(engine, filenames): filenames_by_suffix = group_by_suffix(filenames) engine_name = engine.__name__.rsplit('.', 1)[-1] if '.aux' in filenames_by_suffix: from io import StringIO from pybtex import auxfile aux_contents = StringIO(read_file(filenames_by_suffix['.aux'])) auxdata = auxfile.parse_file(aux_contents) citations = auxdata.citations style = auxdata.style else: citations = '*' style = posixpath.splitext(filenames_by_suffix['.bst'])[0] with cd_tempdir(): copy_file(filenames_by_suffix['.bst']) bib_name = posixpath.splitext(filenames_by_suffix['.bib'])[0] bib_string = read_file(filenames_by_suffix['.bib']) with errors.capture(): # FIXME check error messages result = engine.format_from_string(bib_string, style=style, citations=citations) correct_result_name = '{0}_{1}.{2}.bbl'.format(bib_name, style, engine_name) correct_result = pkgutil.get_data('pybtex.tests.data', correct_result_name).decode(io.get_default_encoding()) assert result == correct_result, diff(correct_result, result)