def write_bibtex(db, filename): ''' Writes the database into the file named filename ''' with open(filename, 'w', encoding='utf-8') as bibtex_file: writer = BibTexWriter() writer.add_trailing_comma = True writer.indent = '' bibtexparser.dump(db, bibtex_file, writer)
def test_trailing_comma(self): with io.open(_data_path('article.bib'), 'r') as bibfile: bib = BibTexParser(bibfile.read()) with io.open(_data_path('article_trailing_comma_output.bib'), 'r') as bibfile: expected = bibfile.read() writer = BibTexWriter() writer.add_trailing_comma = True result = writer.write(bib) self.maxDiff = None self.assertEqual(expected, result)
def test_trailing_comma(self): with io.open(_data_path('article.bib'), 'r') as bibfile: bib = BibTexParser(bibfile.read()) with io.open(_data_path('article_trailing_comma_output.bib'), 'r') as bibfile: expected = bibfile.read() writer = BibTexWriter() writer.add_trailing_comma = True result = writer.write(bib) self.maxDiff = None self.assertEqual(expected, result)
def proc_bib(input_io: TextIOWrapper, output_io: TextIOWrapper, jdb: JournalDB, silent: bool = False, output_format: str = "bib", abbrev_type="iso4"): if not hasattr(Journal, abbrev_type): raise ValueError(f"Invalid abbreviation type `{abbrev_type}`") bib_db = bibtexparser.load(input_io) for entry in bib_db.entries: journaltitle = entry.get("journaltitle") if journaltitle is None: continue journaltitle = braces_regex.sub("", journaltitle) name_pattern = re.compile(fr"^{re.escape(journaltitle)}(:?.*)$", RegexFlag.IGNORECASE) # TODO: query using lambdas? # TODO: normalize names (just in index?). res = jdb.journals.query_one(Journal.names_key, name_pattern) if res: _, journal = res abbrev = getattr(journal, abbrev_type) if output_format == "bib": entry["journaltitle"] = f"{{{abbrev or journaltitle}}}" elif output_format == "sourcemap": gen_sourcemap_map(journal, journaltitle, abbrev, output_io) abbrev_msg = f"abbreviating to '{abbrev}'" if res else f"no abbreviation found" if not silent: info(f"found journal name '{journaltitle}'; {abbrev_msg}.") if output_format == "bib": bib_writer = BibTexWriter() bib_writer.add_trailing_comma = True bib_writer.display_order = None bib_writer.indent = "\t" bib_writer.order_entries_by = None bibtex_code = bib_writer.write(bib_db) output_io.write(bibtex_code) elif output_format == "sourcemap": pass
def generate_bib_from_arxiv(arxiv_item, value, field="id"): if field == "ti": article_id = arxiv_item["id"].split("http://arxiv.org/abs/")[1] else: article_id = value key = "arxiv:" + article_id title = arxiv_item.title authors = arxiv_item.authors if len(authors) > 0: authors = " and ".join([author["name"] for author in authors]) published = arxiv_item.published.split("-") year = '' if len(published) > 1: year = published[0] bib = BibDatabase() bib.entries = [{ "title": title, "author": authors, "year": year, "eprinttype": "arxiv", "eprint": article_id, "keywords": "", "abstract": arxiv_item.summary, "ID": key, "ENTRYTYPE": "article" }] writer = BibTexWriter() writer.add_trailing_comma = True writer.display_order = [ 'title', 'author', 'year', 'eprinttype', 'eprint', 'keywords', 'abstract' ] writer.indent = " " bib = writer.write(bib) return bib
else: print(f'I detected a duplicate based on the key {id}. ' f'I will augment it with a letter and try again. ' f'Please double-check, if this is correct.. ' f'my duplicate detection algorithm is pretty bad.\n\n') id = id_orig + letters[i] i += 1 if not duplicate: bib = re.sub(r'(@[a-z]*{)(.*),', r'\1' + id + ',', bib) bib_db = bibtexparser.loads(bib) db.entries.extend(bib_db.get_entry_list()) else: bib_db = None if id_list: writer = BibTexWriter() writer.indent = '\t' writer.order_entries_by = ('year', 'ID') writer.add_trailing_comma = True with open('../_bibliography/pint.bib', 'w') as bibfile: bibfile.write(writer.write(db)) for line in fileinput.input('../_bibliography/pint.bib', inplace=True): if '@comment{' in line: line = line.replace('@comment{', '') if re.match(r'%}+', line) is not None: line = re.sub(r'%}+', '%', line) line = line.rstrip('\r\n') print(line)