dreader-application/examples/html_generation_demo.py
Duncan Tourolle 131d39080e
All checks were successful
Python CI / test (push) Successful in 3m24s
first application elements
2025-11-07 20:00:05 +01:00

217 lines
6.8 KiB
Python

"""
Demonstration of HTML generation for dreader UI.
This example shows how to:
1. Scan a book directory
2. Generate library view HTML
3. Load a book and generate reader view HTML
4. Generate overlay HTML for settings, TOC, and bookmarks
The generated HTML strings can be passed to a HAL (Hardware Abstraction Layer)
for rendering on the target device.
"""
from pathlib import Path
from dreader import create_ebook_reader
from dreader.html_generator import (
generate_library_html,
generate_reader_html,
generate_settings_overlay,
generate_toc_overlay,
generate_bookmarks_overlay
)
from dreader.book_utils import (
scan_book_directory,
get_chapter_list,
get_bookmark_list,
page_image_to_base64
)
def demo_library_view():
"""Generate and save library view HTML."""
print("Generating library view...")
# Scan books directory
books_dir = Path('tests/data')
books = scan_book_directory(books_dir)
print(f"Found {len(books)} books:")
for book in books:
print(f" - {book['title']} by {book['author']}")
# Generate HTML
library_html = generate_library_html(books)
# Save to file for inspection
output_path = Path('output/library.html')
output_path.parent.mkdir(exist_ok=True)
output_path.write_text(library_html)
print(f"Library HTML saved to: {output_path}")
print(f"HTML length: {len(library_html)} characters\n")
def demo_reader_view():
"""Generate and save reader view HTML."""
print("Generating reader view...")
# Load a test book
book_path = Path('tests/data/test.epub')
if not book_path.exists():
print(f"Test book not found at {book_path}")
return
reader = create_ebook_reader(page_size=(800, 1000))
reader.load_epub(str(book_path))
# Get current page
page_image = reader.get_current_page()
page_base64 = page_image_to_base64(page_image)
# Generate HTML
reader_html = generate_reader_html(
book_title=reader.book_title or "Unknown Title",
book_author=reader.book_author or "Unknown Author",
page_image_data=page_base64
)
# Save to file
output_path = Path('output/reader.html')
output_path.write_text(reader_html)
print(f"Reader HTML saved to: {output_path}")
print(f"HTML length: {len(reader_html)} characters")
print(f"Book: {reader.book_title} by {reader.book_author}\n")
def demo_overlays():
"""Generate and save overlay HTML."""
print("Generating overlay views...")
# Load a test book
book_path = Path('tests/data/test.epub')
if not book_path.exists():
print(f"Test book not found at {book_path}")
return
reader = create_ebook_reader(page_size=(800, 1000))
reader.load_epub(str(book_path))
# 1. Settings overlay
settings_html = generate_settings_overlay()
settings_path = Path('output/overlay_settings.html')
settings_path.write_text(settings_html)
print(f"Settings overlay saved to: {settings_path}")
# 2. TOC overlay
chapters = get_chapter_list(reader)
print(f"Found {len(chapters)} chapters")
toc_html = generate_toc_overlay(chapters)
toc_path = Path('output/overlay_toc.html')
toc_path.write_text(toc_html)
print(f"TOC overlay saved to: {toc_path}")
# 3. Bookmarks overlay (create some test bookmarks first)
reader.save_position('Chapter 1 Start')
reader.next_page()
reader.next_page()
reader.save_position('Page 3')
bookmarks = get_bookmark_list(reader)
print(f"Found {len(bookmarks)} bookmarks")
bookmarks_html = generate_bookmarks_overlay(bookmarks)
bookmarks_path = Path('output/overlay_bookmarks.html')
bookmarks_path.write_text(bookmarks_html)
print(f"Bookmarks overlay saved to: {bookmarks_path}\n")
def demo_hal_usage():
"""
Demonstrate how a HAL would use these functions.
This simulates how your Hardware Abstraction Layer would
interact with the HTML generation functions.
"""
print("=" * 60)
print("HAL Integration Example")
print("=" * 60)
# Step 1: Initialize with books directory
books_dir = Path('tests/data')
# Step 2: Show library view
books = scan_book_directory(books_dir)
library_html = generate_library_html(books)
print(f"\n[HAL] Rendering library with {len(books)} books")
print(f"[HAL] HTML size: {len(library_html)} bytes")
# Step 3: User selects a book (simulated)
if books:
selected_book = books[0]
print(f"\n[HAL] User selected: {selected_book['title']}")
# Step 4: Load book and show reader view
reader = create_ebook_reader(page_size=(800, 1000))
reader.load_epub(selected_book['path'])
page_image = reader.get_current_page()
page_base64 = page_image_to_base64(page_image)
reader_html = generate_reader_html(
book_title=reader.book_title,
book_author=reader.book_author,
page_image_data=page_base64
)
print(f"[HAL] Rendering reader view")
print(f"[HAL] HTML size: {len(reader_html)} bytes")
# Step 5: User presses "Settings" button (simulated)
print(f"\n[HAL] User pressed 'Settings' button")
settings_html = generate_settings_overlay()
print(f"[HAL] Rendering settings overlay on top of page")
print(f"[HAL] HTML size: {len(settings_html)} bytes")
# Step 6: User presses "Contents" button (simulated)
print(f"\n[HAL] User pressed 'Contents' button")
chapters = get_chapter_list(reader)
toc_html = generate_toc_overlay(chapters)
print(f"[HAL] Rendering TOC overlay with {len(chapters)} chapters")
print(f"[HAL] HTML size: {len(toc_html)} bytes")
# Step 7: User navigates pages (simulated)
print(f"\n[HAL] User pressed 'Next' button")
reader.next_page()
page_image = reader.get_current_page()
page_base64 = page_image_to_base64(page_image)
reader_html = generate_reader_html(
book_title=reader.book_title,
book_author=reader.book_author,
page_image_data=page_base64
)
print(f"[HAL] Rendering next page")
print(f"[HAL] HTML size: {len(reader_html)} bytes")
if __name__ == '__main__':
print("dreader HTML Generation Demo")
print("=" * 60)
print()
# Create output directory
Path('output').mkdir(exist_ok=True)
# Run demos
demo_library_view()
demo_reader_view()
demo_overlays()
demo_hal_usage()
print("\n" + "=" * 60)
print("Demo complete!")
print("=" * 60)
print("\nGenerated HTML files have been saved to the 'output/' directory.")
print("Open them in a browser to see how they look.")
print("\nIn a real application, these HTML strings would be passed")
print("to your HAL for rendering on the target display device.")