BibTeX to HTML Converter

Here is a Python script that does the same job: bib_to_html.py

Initializing Python...

1. Paste BibTeX Here

2. Raw HTML Code

3. Live Preview

packages = ["bibtexparser"] import bibtexparser from bibtexparser.customization import convert_to_unicode from bibtexparser.bparser import BibTexParser from js import document from pyodide.ffi import create_proxy # --- 1. LOGIC --- def get_author_string(entry): if 'author' in entry: authors = entry['author'].split(' and ') formatted_authors = [] for author in authors: author = author.strip() if "," in author: parts = [p.strip() for p in author.split(",")] last = parts[0] else: parts = author.split() last = parts[-1] formatted_authors.append(f"{last}") if len(formatted_authors) > 1: return ", ".join(formatted_authors[:-1]) + f" & {formatted_authors[-1]}" elif formatted_authors: return formatted_authors[0] return "" def format_bibtex_entry(entry): title = entry.get('title', 'Unknown title') author_str = get_author_string(entry) year = entry.get('year', 'n.d.') link = entry.get('url', entry.get('doi', '#')) if link != '#' and not (link.startswith('http') or link.startswith('ftp')): if 'doi' in entry: link = f"https://doi.org/{entry['doi']}" else: link = '#' custom_note = "" if entry.get('note', '').startswith('(') and entry.get('note', '').endswith(')'): custom_note = entry['note'].strip() container = "" volume = "" pages = "" if 'journal' in entry: container = f"{entry['journal']}" elif 'booktitle' in entry: container = f"In {entry['booktitle']}" elif 'publisher' in entry: container = f"{entry['publisher']}" if 'volume' in entry: volume = f", Vol. {entry['volume']}" if 'pages' in entry: pages_val = entry['pages'].replace('--', '–') pages = f", pp. {pages_val}" # Using triple quotes for the block, but f-strings inside use single quotes for attributes if needed html = f"""
  • {author_str} ({year}).
    {title}""" for a in range(2,31): key = 'url_'+str(a) if key not in entry: break note_key = 'url_note_'+str(a) if note_key not in entry: url_note = 'link '+str(a) else: url_note = entry[note_key] # CORRECT QUOTING: Outer single ', Inner double " html += f' [{url_note}]' if custom_note: html += f" {custom_note}." else: html += "." if container or volume or pages: html += f" {container}{volume}{pages}." html += "

    \n
  • " return html # --- 2. HANDLER --- def convert_bibtex(event): input_el = document.getElementById("bib-input") output_el = document.getElementById("html-output") status_el = document.getElementById("status") preview_frame = document.getElementById("preview-frame") content = input_el.value if not content: status_el.innerHTML = 'Error: Please paste BibTeX content first.' return status_el.innerHTML = "Processing..." try: parser = BibTexParser() parser.customization = convert_to_unicode bib_database = bibtexparser.loads(content, parser=parser) if not bib_database.entries: status_el.innerHTML = "No entries found." return sorted_entries_1 = sorted( bib_database.entries, key=lambda entry: entry.get('author', '').split(' and ')[0].split(',')[0].split()[-1].lower() ) sorted_entries = sorted( sorted_entries_1, key=lambda entry: entry.get('year', '') ) list_content = "" for entry in sorted_entries: list_content += format_bibtex_entry(entry) # Full HTML Template full_html = f""" Converted bibliography

    References

    """ # 1. Show Code output_el.value = full_html # 2. Show Preview (Render the HTML in the iframe) preview_frame.srcdoc = full_html status_el.innerHTML = 'Done! Check the Code and Preview below.' except Exception as e: # CORRECT QUOTING: Outer single ', Inner double " status_el.innerHTML = f'Python Error: {str(e)}' # --- 3. BIND --- proxy = create_proxy(convert_bibtex) document.getElementById("btn-convert").addEventListener("click", proxy) document.getElementById("status").innerHTML = "Ready."