""" HTML Converter for Rich Console Output This module converts Rich console output to clean HTML suitable for PDF generation. """ from rich.console import Console from rich.text import Text import re from typing import Dict, Any, Optional class RichToHTMLConverter: """Converts Rich console output to clean HTML for PDF generation.""" def __init__(self): self.console = Console(record=True, width=120) def rich_to_html(self, console_output: str) -> str: """ Convert Rich console output to clean HTML. Args: console_output: Raw console output with Rich formatting Returns: Clean HTML string suitable for PDF generation """ # Create a console and export to HTML html_content = self.console.export_html( inline_styles=True, code_format=None ) # Clean and optimize for PDF return self.clean_html_for_pdf(html_content) def clean_html_for_pdf(self, html_content: str) -> str: """ Clean HTML content for better PDF rendering. Args: html_content: Raw HTML content from Rich export Returns: Cleaned HTML content """ # Remove background colors that don't work well in PDF html_content = re.sub(r'background-color:\s*#[0-9a-fA-F]{6};?', '', html_content) # Ensure good contrast for text html_content = re.sub(r'color:\s*#[0-9a-fA-F]{6};?', 'color: #333333;', html_content) # Remove excessive margins and padding html_content = re.sub(r'margin:\s*\d+px;?', 'margin: 5px;', html_content) html_content = re.sub(r'padding:\s*\d+px;?', 'padding: 3px;', html_content) return html_content def apply_pdf_styles(self, html_content: str) -> str: """ Apply PDF-specific styles to HTML content. Args: html_content: HTML content to style Returns: HTML content with PDF-optimized styles """ pdf_styles = """ """ # Insert styles into HTML head if '' in html_content: html_content = html_content.replace('', f'{pdf_styles}') else: html_content = f'{pdf_styles}{html_content}' return html_content