Skip to content

Commit

Permalink
Refactor bibtex_to_table function to return a tuple of markdown and h…
Browse files Browse the repository at this point in the history
…elper markdown
  • Loading branch information
XuhuiZhou committed Apr 16, 2024
1 parent ca05215 commit daadd6c
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions bibtex_to_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def preprocess_entry(entry: dict, taxonomy:dict[str, list[str]]) -> dict:
raise ValueError(f"{subcategory} is not a valid {category}, please choose from {subcategories}")
return entry

def bibtex_to_table(bibtex: str, taxonomy: dict[str, list[str]]) -> str:
def bibtex_to_table(bibtex: str, taxonomy: dict[str, list[str]]) -> tuple[str, str]:
# Parse the BibTeX string
bib_database = bibtexparser.parse_string(bibtex)

Expand All @@ -113,18 +113,23 @@ def bibtex_to_table(bibtex: str, taxonomy: dict[str, list[str]]) -> str:
if len(ids) != len(set(ids)):
repeated_entries = [entry['title'] for entry in entries if ids.count(entry["title"]) > 1]
raise ValueError(f"Repeated entries found: {repeated_entries}")
# Create a Markdown table
# Create a Markdown and helper table
headers = ["Title", "Date"] + list(taxonomy.keys()) + ["helper"]
helper_headers = ["helper"]
rows = []
helper_rows = []
for entry in entries:
row = [entry["title_w_url"], entry["date"]]+[entry[category] for category in taxonomy.keys()]
row += [f"[{row[1]}] {row[0]}, {entry['author_et_al']}, {entry['venue']}"]
helper_rows.append(f"[{row[1]}] {row[0]}, {entry['author_et_al']}, {entry['venue']}")
rows.append(row)

df = pd.DataFrame(rows, columns=headers)

# Create a helper table
df_helper = pd.DataFrame(helper_rows, columns=helper_headers)
markdown = df.to_markdown(index=False)

return markdown
helper_markdown = df_helper.to_markdown(index=False)
return markdown, helper_markdown

# Example usage:
taxonomy = parse_markdown_file('./docs/taxonomy.md')
Expand All @@ -137,11 +142,16 @@ def bibtex_to_table(bibtex: str, taxonomy: dict[str, list[str]]) -> str:
print("taxonomy:")
print(taxonomy)
output_file = "./docs/paper_table.md"
markdown_table = bibtex_to_table(bibtex_string, taxonomy=taxonomy)
helper_file = "./docs/helper.md"
markdown_table, helper_table = bibtex_to_table(bibtex_string, taxonomy=taxonomy)

# Write the Markdown table to the output file
with open(output_file, "w") as f:
f.write(markdown_table)

# Write the helper table to the helper file
with open(helper_file, "w") as f:
f.write(helper_table)



0 comments on commit daadd6c

Please sign in to comment.