Skip to content

Commit 539d0d0

Browse files
committed
change parser, fix bugs
1 parent 64abb59 commit 539d0d0

File tree

4 files changed

+261
-114
lines changed

4 files changed

+261
-114
lines changed

.github/workflows/bib2readme.py

Lines changed: 89 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,151 @@
11
from collections import defaultdict
22
from typing import Dict, List
33

4-
import bibtexparser
4+
from pybtex.database import parse_file
5+
from pybtex.scanner import PybtexSyntaxError
6+
from pylatexenc.latex2text import LatexNodes2Text
57

68
# Define the categories to be printed in the README
79
VALID_CATEGORIES = ["overview", "software", "paper", "uncategorized"]
810

9-
README_HEADER = """
11+
README_INTRO = """
1012
Welcome to the Awesome Amortized Inference repository!
1113
This is a curated list of resources, including overviews, software, papers, and other resources related to amortized inference.
1214
Feel free to explore the entries below and use the provided BibTeX information for citation purposes.
13-
Contributioons always welcome, this shall be a community-driven project.
14-
Contribution guide will follow ASAP.
15+
Contributions are always welcome, this is a community-driven project.
1516
"""
1617

1718

1819
# Define Entry class
1920
class Entry:
20-
def __init__(self, title: str, authors: str, url: str, category: str, bibtex: str):
21+
def __init__(
22+
self,
23+
title: str,
24+
authors: str,
25+
url: str,
26+
category: str,
27+
awesome_fields: Dict[str, str],
28+
bibtex: str,
29+
):
2130
self.title = title
2231
self.authors = authors
2332
self.url = url
2433
self.category = category
34+
self.awesome_fields = awesome_fields
2535
self.bibtex = bibtex
2636

2737
@classmethod
28-
def from_bibtex(cls, entry: dict) -> "Entry":
29-
title = entry.get("title", "No title").replace("{", "").replace("}", "")
30-
authors = entry.get("author", "No author").replace(" and ", ", ")
31-
url = entry.get("url", "")
32-
category = entry.get("category", "uncategorized").lower()
38+
def from_bibtex(cls, entry) -> "Entry":
39+
title = entry.fields.get("title", "No title").replace("{", "").replace("}", "")
40+
authors = (
41+
", ".join(
42+
cls.person_to_first_last(person)
43+
for person in entry.persons.get("author", [])
44+
)
45+
or "No author"
46+
)
47+
url = entry.fields.get("url", "")
48+
category = entry.fields.get("category", "uncategorized").lower()
49+
50+
# get all fields that start with 'awesome-' and save them in a separate field
51+
awesome_fields = {
52+
key[len("awesome-") :]: value
53+
for key, value in entry.fields.items()
54+
if key.startswith("awesome-")
55+
}
56+
57+
if category not in VALID_CATEGORIES:
58+
print(
59+
f"Warning: Unrecognized category '{category}' for entry '{title}'. Categorizing as 'uncategorized'."
60+
)
61+
category = "uncategorized"
3362
bibtex = cls.format_bibtex(entry)
34-
return cls(title, authors, url, category, bibtex)
63+
return cls(title, authors, url, category, awesome_fields, bibtex)
64+
65+
@staticmethod
66+
def person_to_first_last(person) -> str:
67+
name_parts = [
68+
" ".join(part)
69+
for part in [
70+
person.first_names,
71+
person.middle_names,
72+
person.prelast_names,
73+
person.last_names,
74+
person.lineage_names,
75+
]
76+
if part
77+
]
78+
name = " ".join(name_parts)
79+
80+
return LatexNodes2Text().latex_to_text(name)
3581

3682
@staticmethod
37-
def format_bibtex(entry: dict) -> str:
38-
bibtex_type = entry.get("ENTRYTYPE", "misc")
39-
bibtex_key = entry.get("ID", "unknown")
83+
def format_bibtex(entry) -> str:
84+
bibtex_type = entry.type if entry.type else "misc"
85+
bibtex_key = entry.key if entry.key else "unknown"
4086
bibtex_fields = [
41-
f" {key} = {{{value}}}"
42-
for key, value in entry.items()
43-
if key not in ["ENTRYTYPE", "ID"]
87+
f"{key} = {{{value}}}"
88+
for key, value in entry.fields.items()
89+
if not key.startswith("awesome-") # exclude awesome fields from BibTeX
4490
]
91+
if "author" in entry.persons:
92+
bibtex_fields.append(
93+
f"author = {{{' and '.join(str(person) for person in entry.persons.get('author', []))}}}"
94+
)
4595
bibtex_str = (
46-
f"@{bibtex_type}{{{bibtex_key},\n " + ",\n ".join(bibtex_fields) + "\n}"
96+
f"@{bibtex_type}{{{bibtex_key},\n  " + ",\n  ".join(bibtex_fields) + "\n}"
4797
)
4898
return bibtex_str
4999

50100
def to_string(self) -> str:
51-
entry_str = f"- **{self.title}**. {self.authors}."
52-
if self.url:
53-
entry_str += f" [[Link]]({self.url})"
54-
entry_str += f" <details><summary>Show BibTeX</summary><pre><code>{self.bibtex}</code></pre></details>"
55-
entry_str += "\n"
101+
entry_str = f"- **{self.title}**."
102+
if self.awesome_fields:
103+
entry_str += " "
104+
for key, value in self.awesome_fields.items():
105+
entry_str += f"[[{key.capitalize()}]]({value}) "
106+
entry_str += f"<br /> {self.authors}"
107+
entry_str += f"""
108+
<details>
109+
<summary>Show BibTeX</summary>
110+
<pre><code>
111+
{self.bibtex}
112+
</code>
113+
</pre></details>
114+
"""
115+
# entry_str += "<br />"
56116
return entry_str
57117

58118

59119
def organize_entries(bib_database) -> Dict[str, List[Entry]]:
60120
entries_by_category: Dict[str, List[Entry]] = defaultdict(list)
61-
for entry in bib_database.entries:
121+
for entry_key, entry in bib_database.entries.items():
62122
entry_obj = Entry.from_bibtex(entry)
63123
entries_by_category[entry_obj.category].append(entry_obj)
64124
return entries_by_category
65125

66126

67127
def create_readme(entries_by_category: Dict[str, List[Entry]]) -> str:
68128
readme_content = "# Awesome Amortized Inference\n\n"
69-
readme_content += README_HEADER
129+
readme_content += README_INTRO
70130

71131
for category in VALID_CATEGORIES:
72132
if category in entries_by_category:
73133
readme_content += f"## {category.capitalize()}\n\n"
74134
readme_content += "\n".join(
75135
[entry.to_string() for entry in entries_by_category[category]]
76136
)
77-
readme_content += "\n"
78137
return readme_content
79138

80139

81140
def main():
82141
try:
83-
with open("data.bib") as bibtex_file:
84-
bib_database = bibtexparser.load(bibtex_file)
142+
bib_database = parse_file("data.bib")
85143
except FileNotFoundError:
86144
print("The data.bib file was not found.")
87145
exit(1)
146+
except PybtexSyntaxError as e:
147+
print(f"Error parsing BibTeX file: {e}")
148+
exit(1)
88149

89150
entries_by_category = organize_entries(bib_database)
90151
readme_content = create_readme(entries_by_category)

.github/workflows/generate_readme_from_bib.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ jobs:
2525
- name: Install dependencies
2626
run: |
2727
python -m pip install --upgrade pip
28-
pip install bibtexparser
28+
pip install pybtex pylatexenc
2929
3030
# Step 4: Run the script to generate README.md
3131
- name: Run README generation script

0 commit comments

Comments
 (0)