""" HTML generation functions for dreader UI. Generates HTML strings programmatically for library view, reader view, and various overlays (settings, TOC, etc.) that can be passed to a HAL for rendering. """ from pathlib import Path from typing import List, Dict, Optional from dreader import create_ebook_reader import base64 from io import BytesIO def generate_library_html(books: List[Dict[str, str]]) -> str: """ Generate HTML for the library view showing all books in a grid. Args: books: List of book dictionaries with keys: - title: Book title - author: Book author - filename: EPUB filename - cover_data: Optional base64 encoded cover image Returns: Complete HTML string for library view """ books_html = [] for book in books: cover_img = '' if book.get('cover_data'): cover_img = f'Cover' else: # Placeholder if no cover cover_img = f'
{book["title"][:1]}
' books_html.append(f'''
{cover_img}
{book['title']}
{book['author']}
''') # Arrange books in rows of 3 rows = [] for i in range(0, len(books_html), 3): row_books = books_html[i:i+3] # Pad with empty cells if needed while len(row_books) < 3: row_books.append('') rows.append(f'{"".join(row_books)}') html = f''' Library

My Library

{len(books)} books

{"".join(rows)}
''' return html def generate_reader_html(book_title: str, book_author: str, page_image_data: str) -> str: """ Generate HTML for the reader view with page display. Args: book_title: Title of current book book_author: Author of current book page_image_data: Base64 encoded page image Returns: Complete HTML string for reader view (page layer only) """ html = f''' {book_title}
{book_title}
{book_author}
Page
''' return html def generate_settings_overlay() -> str: """ Generate HTML for the settings overlay. Returns: HTML string for settings overlay """ html = ''' Settings
Settings
Font Size
Line Spacing
Brightness
WiFi
''' return html def generate_toc_overlay(chapters: List[Dict]) -> str: """ Generate HTML for the table of contents overlay. Args: chapters: List of chapter dictionaries with keys: - index: Chapter index - title: Chapter title Returns: HTML string for TOC overlay """ chapter_rows = [] for chapter in chapters: chapter_rows.append(f''' {chapter['title']} ''') html = f''' Table of Contents
Table of Contents
{"".join(chapter_rows)}
''' return html def generate_bookmarks_overlay(bookmarks: List[Dict]) -> str: """ Generate HTML for the bookmarks overlay. Args: bookmarks: List of bookmark dictionaries with keys: - name: Bookmark name - position: Position info Returns: HTML string for bookmarks overlay """ bookmark_rows = [] for bookmark in bookmarks: bookmark_rows.append(f'''
{bookmark['name']}
{bookmark.get('position', '')}
''') html = f''' Bookmarks
Bookmarks
{"".join(bookmark_rows)}
''' return html