Skip to content

Commit 0ac4e21

Browse files
committed
implement initial structure
1 parent 583bc35 commit 0ac4e21

File tree

6 files changed

+474
-1
lines changed

6 files changed

+474
-1
lines changed

.github/workflows/bib2readme.py

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
from collections import defaultdict
2+
from typing import Dict, List
3+
4+
import bibtexparser
5+
6+
# Define the categories to be printed in the README
7+
VALID_CATEGORIES = ["overview", "software", "paper", "uncategorized"]
8+
9+
README_HEADER = """
10+
Welcome to the Awesome Amortized Inference repository!
11+
This is a curated list of resources, including overviews, software, papers, and other resources related to amortized inference.
12+
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+
"""
16+
17+
18+
# Define Entry class
19+
class Entry:
20+
def __init__(self, title: str, authors: str, url: str, category: str, bibtex: str):
21+
self.title = title
22+
self.authors = authors
23+
self.url = url
24+
self.category = category
25+
self.bibtex = bibtex
26+
27+
@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()
33+
bibtex = cls.format_bibtex(entry)
34+
return cls(title, authors, url, category, bibtex)
35+
36+
@staticmethod
37+
def format_bibtex(entry: dict) -> str:
38+
bibtex_type = entry.get("ENTRYTYPE", "misc")
39+
bibtex_key = entry.get("ID", "unknown")
40+
bibtex_fields = [
41+
f" {key} = {{{value}}}"
42+
for key, value in entry.items()
43+
if key not in ["ENTRYTYPE", "ID"]
44+
]
45+
bibtex_str = (
46+
f"@{bibtex_type}{{{bibtex_key},\n " + ",\n ".join(bibtex_fields) + "\n}"
47+
)
48+
return bibtex_str
49+
50+
def to_string(self) -> str:
51+
entry_str = f"- **{self.title}**\n {self.authors}\n"
52+
if self.url:
53+
entry_str += f" [Link]({self.url})\n"
54+
entry_str += (
55+
f" <details>\n"
56+
f" <summary>Show BibTeX</summary>\n"
57+
f""" <button onclick="var btn=this; navigator.clipboard.writeText(`{self.bibtex}`).then(function() {{
58+
btn.textContent='Copied!';
59+
setTimeout(function() {{
60+
btn.textContent='Copy BibTeX to Clipboard';
61+
}}, 2000);
62+
}});">Copy BibTeX</button>\n"""
63+
f"<pre><code>\n"
64+
f"{self.bibtex}\n"
65+
f"</code></pre>\n"
66+
f"</details>\n"
67+
)
68+
entry_str += "\n"
69+
return entry_str
70+
71+
72+
def organize_entries(bib_database) -> Dict[str, List[Entry]]:
73+
entries_by_category: Dict[str, List[Entry]] = defaultdict(list)
74+
for entry in bib_database.entries:
75+
entry_obj = Entry.from_bibtex(entry)
76+
entries_by_category[entry_obj.category].append(entry_obj)
77+
return entries_by_category
78+
79+
80+
def create_readme(entries_by_category: Dict[str, List[Entry]]) -> str:
81+
readme_content = "# Awesome Amortized Inference\n\n"
82+
readme_content += README_HEADER
83+
84+
for category in VALID_CATEGORIES:
85+
if category in entries_by_category:
86+
readme_content += f"## {category.capitalize()}\n\n"
87+
readme_content += "\n".join(
88+
[entry.to_string() for entry in entries_by_category[category]]
89+
)
90+
readme_content += "\n"
91+
return readme_content
92+
93+
94+
def main():
95+
try:
96+
with open("data.bib") as bibtex_file:
97+
bib_database = bibtexparser.load(bibtex_file)
98+
except FileNotFoundError:
99+
print("The data.bib file was not found.")
100+
exit(1)
101+
102+
entries_by_category = organize_entries(bib_database)
103+
readme_content = create_readme(entries_by_category)
104+
105+
with open("README.md", "w") as readme_file:
106+
readme_file.write(readme_content)
107+
108+
109+
if __name__ == "__main__":
110+
main()
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
name: Generate README
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
workflow_dispatch:
8+
9+
jobs:
10+
build:
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
# Step 1: Check out the repository code
15+
- name: Checkout repository
16+
uses: actions/checkout@v3
17+
18+
# Step 2: Set up Python environment
19+
- name: Set up Python
20+
uses: actions/setup-python@v4
21+
with:
22+
python-version: "3.11" # Adjust the Python version if necessary
23+
24+
# Step 3: Install dependencies
25+
- name: Install dependencies
26+
run: |
27+
python -m pip install --upgrade pip
28+
pip install bibtexparser
29+
30+
# Step 4: Run the script to generate README.md
31+
- name: Run README generation script
32+
run: python .github/workflows/bib2readme.py
33+
34+
# Step 5: Commit and push changes if README.md was updated
35+
- name: Commit and push changes
36+
run: |
37+
git config --global user.name 'github-actions[bot]'
38+
git config --global user.email 'github-actions[bot]@users.noreply.github.com'
39+
git add README.md
40+
git diff-index --quiet HEAD || git commit -m "Update README.md"
41+
git push

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
*.html
2+
.DS_Store
3+
README_files/

.nojekyll

Whitespace-only changes.

0 commit comments

Comments
 (0)