snac/scripts/parse_bibtex.py

91 lines
2.6 KiB
Python
Executable File

#!/usr/bin/env python
entries = {}
outfile = open("../lit/papers.org", "w")
def org_entry(bibtex_entry_text, bibtex_entry_dict):
if {
"key", "author", "title", "journal", "volume", "pages", "year", "doi",
} <= bibtex_entry_dict.keys():
b = {}
b["key"] = bibtex_entry_dict["key"].replace("_", ".")
b["author"] = bibtex_entry_dict["author"]
b["title"] = bibtex_entry_dict["title"]
b["journal"] = bibtex_entry_dict["journal"]
b["volume"] = bibtex_entry_dict["volume"]
b["pages"] = bibtex_entry_dict["pages"]
b["year"] = bibtex_entry_dict["year"]
b["doi"] = bibtex_entry_dict["doi"]
b_text = bibtex_entry_text.replace(bibtex_entry_dict["key"], b["key"])
return (
f"""
**** {b["key"]}
:PROPERTIES:
:CUSTOM_ID: {b["key"]}
:END:
{b["author"]},
/{b["title"]}/,
{b["journal"]} **{b["volume"]}**, {b["pages"]} ({b["year"]}), doi:[[https://doi.org/{b["doi"]}][{b["doi"]}]].
| <20> | <60> |
| Extended data | |
|------------------+------|
| Author | {b["author"]} |
| Title | {b["title"]} |
| Journal | {b["journal"]} |
| Volume | {b["volume"]} |
| Pages | {b["pages"]} |
| Year | {b["year"]} |
| doi | [[https://doi.org/{b["doi"]}][{b["doi"]}]] |
| Publication date | |
|------------------+------|
| Submission date | |
""" + """
#+begin_details
#+begin_summary
#+html: BibTeX
#+end_summary
#+begin_src bibtex
""" + f"{b_text}" + """
#+end_src
#+end_details
#+attr_html: :open true
#+begin_details
#+begin_summary
#+html: Annotations
#+end_summary
#+end_details
"""
)
else:
return ""
with open("../lit/papers.bib", errors="ignore") as infile:
while line := infile.readline():
if "@article" in line.lower():
bibtex_entry = line
key = line.partition("{")[2].rstrip(",\n")
entries[key] = {}
entries[key]["key"] = key
while line := infile.readline():
bibtex_entry += line
if line.startswith("}"):
break
field, _, value = line.partition("=")
field = field.strip()
value = value.strip().removesuffix(",")
if value.startswith("{{") and value.endswith("}}"):
value = value.removeprefix("{{").removesuffix("}}")
value = value.removeprefix("{").removesuffix("}")
value = value.removeprefix('"').removesuffix('"')
entries[key][field] = value
outfile.write(org_entry(bibtex_entry, entries[key]))