This commit is contained in:
Federico Rossi 2023-05-10 11:34:21 +02:00
commit d54d598e29

104
main.py Normal file
View file

@ -0,0 +1,104 @@
import pybtex.database
from pybtex import errors
import sys
def bibtex_to_html(bibtex_file_path, html_file_path):
# Load the BibTeX file
try:
bib_data = pybtex.database.parse_file(bibtex_file_path)
except errors.BibliographyDataNotFoundError as e:
print(f"ERROR: {e}")
return
# Create the HTML file
with open(html_file_path, "w") as f:
f.write("<html>\n<head>\n<title>Bibliography</title>\n</head>\n<body>\n")
# Write the journal papers section
f.write("<h2>Journals</h2>\n")
f.write("<hr>\n")
f.write("<ol>\n")
# Loop through each journal entry in the BibTeX file
for key, entry in bib_data.entries.items():
if "journal" in entry.fields:
f.write("<li>\n")
# Write the citation key
#f.write(f"<strong>{key}</strong><br/>\n")
# Write the authors
if "author" in entry.persons:
authors = entry.persons["author"]
f.write("; ".join(str(author) for author in authors))
f.write("<br/>\n")
# Write the title
if "title" in entry.fields:
f.write(f"<strong>{entry.fields['title']}</strong><br/>\n")
# Write the publication information
f.write(f"{entry.fields['journal']}, ")
if "volume" in entry.fields:
f.write(f"vol. {entry.fields['volume']}")
if "number" in entry.fields:
f.write(f"({entry.fields['number']})")
if "pages" in entry.fields:
f.write(f", pp. {entry.fields['pages']}")
if "year" in entry.fields:
f.write(f", {entry.fields['year']}")
f.write("<br/>\n")
f.write("</li>\n")
f.write("</ol>\n")
# Write the conference papers section
f.write("<h2>Conferences</h2>\n")
f.write("<hr>\n")
f.write("<ol>\n")
# Loop through each conference entry in the BibTeX file
for key, entry in bib_data.entries.items():
if "booktitle" in entry.fields:
f.write("<li>\n")
# Write the citation key
# f.write(f"<strong>{key}</strong><br/>\n")
# Write the authors
if "author" in entry.persons:
authors = entry.persons["author"]
f.write("<em>")
f.write("; ".join(str(author) for author in authors))
f.write("</em>");
f.write("<br/>\n")
# Write the title
if "title" in entry.fields:
print(entry.fields['title'])
f.write(f"<strong>{entry.fields['title']}</strong><br/>\n")
# Write the publication information
f.write(f"In <em>{entry.fields['booktitle']}</em>")
if "pages" in entry.fields:
f.write(f", pp. {entry.fields['pages']}")
if "year" in entry.fields:
f.write(f", {entry.fields['year']}")
f.write("<br/>\n")
f.write("</li>\n")
f.write("</ol>\n")
f.write("</body>\n</html>\n")
if __name__ == "__main__":
if len(sys.argv) != 3:
print("Usage: python bibtex_to_html.py input.bib output.html")
sys.exit(1)
input_file_path = sys.argv[1]
output_file_path = sys.argv[2]
bibtex_to_html(input_file_path, output_file_path)