def test_align(self): bib_database = BibDatabase() bib_database.entries = [{ 'ID': 'abc123', 'ENTRYTYPE': 'book', 'author': 'test', 'thisisaverylongkey': 'longvalue' }] writer = BibTexWriter() writer.align_values = True result = bibtexparser.dumps(bib_database, writer) expected = \ """@book{abc123, author = {test}, thisisaverylongkey = {longvalue} } """ self.assertEqual(result, expected) with open('bibtexparser/tests/data/multiple_entries_and_comments.bib' ) as bibtex_file: bib_database = bibtexparser.load(bibtex_file) writer = BibTexWriter() writer.contents = ['entries'] writer.align_values = True result = bibtexparser.dumps(bib_database, writer) expected = \ """@book{Toto3000, author = {Toto, A and Titi, B}, title = {A title} } @article{Wigner1938, author = {Wigner, E.}, doi = {10.1039/TF9383400029}, issn = {0014-7672}, journal = {Trans. Faraday Soc.}, owner = {fr}, pages = {29--41}, publisher = {The Royal Society of Chemistry}, title = {The transition state method}, volume = {34}, year = {1938} } @book{Yablon2005, author = {Yablon, A.D.}, publisher = {Springer}, title = {Optical fiber fusion slicing}, year = {2005} } """ self.assertEqual(result, expected)
def test_align(self): bib_database = BibDatabase() bib_database.entries = [{'ID': 'abc123', 'ENTRYTYPE': 'book', 'author': 'test', 'thisisaverylongkey': 'longvalue'}] writer = BibTexWriter() writer.align_values = True result = bibtexparser.dumps(bib_database, writer) expected = \ """@book{abc123, author = {test}, thisisaverylongkey = {longvalue} } """ self.assertEqual(result, expected) with open('bibtexparser/tests/data/multiple_entries_and_comments.bib') as bibtex_file: bib_database = bibtexparser.load(bibtex_file) writer = BibTexWriter() writer.contents = ['entries'] writer.align_values = True result = bibtexparser.dumps(bib_database, writer) expected = \ """@book{Toto3000, author = {Toto, A and Titi, B}, title = {A title} } @article{Wigner1938, author = {Wigner, E.}, doi = {10.1039/TF9383400029}, issn = {0014-7672}, journal = {Trans. Faraday Soc.}, owner = {fr}, pages = {29--41}, publisher = {The Royal Society of Chemistry}, title = {The transition state method}, volume = {34}, year = {1938} } @book{Yablon2005, author = {Yablon, A.D.}, publisher = {Springer}, title = {Optical fiber fusion slicing}, year = {2005} } """ self.assertEqual(result, expected)
def save(self, bibfile=-1): """ save the biblist with : - the original filename without any arg or - the given file name if not empty """ if bibfile == -1: bibfile = self.name db = BibDatabase() for item in self: db.entries.append(item) writer = BibTexWriter() # this class is needed to prepare format writer.indent = ' ' # indent entries with 4 spaces instead of one writer.comma_first = False # place the comma at the beginning of the line writer.align_values = True # with a nice indentation print('') print(os.path.join(os.path.expandvars('$PYBLIO_BIB'), bibfile)) print('') with open(os.path.join(os.path.expandvars('$PYBLIO_BIB'), bibfile), 'w') as bf: bf.write('\n') bf.write(writer.write(db)) bf.write('\n')
def printCandidates(candidates: List[Dict]): writer = BibTexWriter() writer.align_values = True writer.indent = " " db = BibDatabase() db.entries = candidates print(f"{len(candidates)} suggestions:\n") output = writer.write(db) print(output)
def bibtex_entries_to_string(entries: List[Dict]): if len(entries) == 0: return "" writer = BibTexWriter() writer.align_values = True writer.indent = " " db = BibDatabase() db.entries = entries return writer.write(db)
def reformatbib(infile,outfile): with open(infile,'r') as bibfileIn: try: bib_database = bibtexparser.load(bibfileIn) writer = BibTexWriter() writer.align_values = True writer.indent = ' ' # indent entries with spaces # writer.comma_first = True # place the comma at the beginning of the line with open(outfile, 'w') as bibfileOut: bibfileOut.write(writer.write(bib_database)) print(f'Reformated {infile} written to {outfile}') except: print(f'{infile} not parsed')
def print_candidates(candidates: List[Dict]): count = len(candidates) if count == 0: print("No suggestions.") else: if count == 1: print("One suggestion:\n") else: print(f"{count} suggestions:\n") writer = BibTexWriter() writer.align_values = True writer.indent = " " db = BibDatabase() db.entries = candidates output = writer.write(db) print(output)
def main(): """Main function of the script. Loads the bib file, does the chcecking on it and prints out sorted and formated database. """ parser = argparse.ArgumentParser() parser.add_argument("--input", type=argparse.FileType('r'), default=sys.stdin, help="Input file, default is stdin.") parser.add_argument("--output", type=argparse.FileType('w'), default=sys.stdout, help="Optional output file.") parser.add_argument("--try-fix", default=False, action="store_true", help="Flag to search information to fix the dtabase.") parser.add_argument("--anthologies", type=str, nargs='+', help="List of BibTeX files with know papers.") args = parser.parse_args() if args.anthologies is not None: load_anthologies(args.anthologies) bib_database = bibtexparser.load(args.input, get_bibparser()) cache_journal_issn(bib_database) authors, journals, booktitles = check_database(bib_database, args.try_fix) look_for_misspellings(authors, 'Authors') look_for_misspellings(journals, 'Journals') look_for_misspellings(booktitles, 'Booktitles (proceedings)', threshold=0.9) writer = BibTexWriter() writer.indent = ' ' writer.order_by = ['author', 'year', 'title'] writer.display_order = ['author', 'title', 'booktitle', 'journal'] writer.align_values = True args.output.write(writer.write(bib_database))
def main(): print("Reading from stdin ...", end="", file=sys.stderr) input_records = sys.stdin.read().split("\n\n") print("done.", file=sys.stderr) bib_parser = BibTexParser(ignore_nonstandard_types=True, homogenize_fields=True, common_strings=True) writer = BibTexWriter() writer.indent = ' ' writer.order_by = ['author', 'year', 'title'] writer.display_order = ['author', 'title', 'booktitle', 'journal'] writer.align_values = True records = 0 skipped = 0 for record in input_records: if not record: continue try: parsed = bibtexparser.loads(record, bib_parser) records += 1 if records % 1000 == 0: print("Processed {} records.".format(records), file=sys.stderr) except (pyparsing.ParseException, bibtexparser.bibdatabase.UndefinedString): skipped += 1 for item in parsed.get_entry_list(): if "abstract" in item: del item["abstract"] parsed.comments = [] parsed.entries = [e for e in parsed.entries if e["ENTRYTYPE"] != "book"] parsed.entries = list(parsed.get_entry_dict().values()) print(writer.write(parsed)) print("Finished. {} records kept, {} skipped.".format(records, skipped), file=sys.stderr)
def main(): args = parser.parse_args() args.output = abspath(expanduser(args.output)) logging.info("Targeting: {}".format(args.target)) logging.info("Output to: {}".format(args.output)) #load each of the specified files target_files = retrieval.get_data_files(args.target, ".bib") dbs = [BU.parse_bib_files(x, bib_proc.nop) for x in target_files] main_db = b.bibdatabase.BibDatabase() # Load the main database if exists(args.output): BU.parse_bib_files(args.output, bib_proc.nop, database=main_db) main_set = set(main_db.get_entry_dict().keys()) total_entries = main_db.entries[:] missing_keys_main = set() # Get entries missing from the main database for db in dbs: db_dict = db.get_entry_dict() db_set = set(db_dict.keys()) missing_keys = db_set.difference(main_set) missing_keys_main.update(missing_keys) total_entries += [db_dict[x] for x in missing_keys] logging.info("{} missing entries".format(len(total_entries))) main_db.entries = total_entries # Write out the combined database logging.info("Bibtex loaded") writer = BibTexWriter() writer.align_values = True with open(join(args.output), 'a') as f: f.write(writer.write(main_db))
tags = [ i.strip() for i in re.split(',|;', entry["tags"].replace("\n", "")) ] for tag in tags: if tag not in db_dict: db_dict[tag] = BibDatabase() db_dict[tag].entries.append(entry) if args.YEARS: if 'year' not in entry: raise Exception( "Years specified, entry has no year: {}".format( entry['ID'])) if entry['year'] not in db_dict: db_dict[entry['year']] = BibDatabase() db_dict[entry['year']].entries.append(entry) if args.AUTHORS: raise Exception("not implemented AUTHORS yet") # Write out split bibtex files logging.info("Writing Bibtex") writer = BibTexWriter() writer.align_values = True for k, v in db_dict.items(): clean_name = k.replace("/", "_") with open(join(args.output, "{}.bib".format(clean_name)), 'w') as f: f.write(writer.write(v))
value = re.sub('<i>|</i>', '', value) md.write('%-10s "%s"\n' % (field + ':', value)) md.write('%-10s "%s-01-01"\n' % ('date:', bibentry.fields['year'])) md.write(FM_DELIM + '\n\n') md.write('## Abstract\n') if 'abstract' in bibentry.fields: md.write(bibentry.fields['abstract'] + '\n') else: md.write('Unavailable :(' + '\n') md.write('\n') with open(args.input, encoding="utf8") as bibtex_file: bibtex_str = bibtex_file.read() bib_database = bibtexparser.loads(bibtex_str) for entry in bib_database.entries: markdown = os.path.join(args.dir, '{}.md'.format(entry['ID'])) with open(markdown, 'a', encoding="utf8") as md: bibdb = BibDatabase() bibdb.entries = [entry] bibtw = BibTexWriter() bibtw.align_values = True # TOP CARALHO bibtw.indent = ' ' md.write('## BibTeX Citation\n') md.write('{{< highlight bibtex >}}' + '\n') # FIXME bibtex supported? md.write(bibtw.write(bibdb).rstrip() + '\n') md.write('{{< /highlight >}}' + '\n')