""" HTML generation and file management for dev flow. """ import os from datetime import datetime from pathlib import Path from logger import logger from config import HTML_OUTPUT_DIR class HTMLHandler: """Handles HTML generation and file management.""" def __init__(self, output_dir=HTML_OUTPUT_DIR): """Initialize HTML handler.""" self.output_dir = output_dir Path(self.output_dir).mkdir(exist_ok=True) logger.debug(f"HTMLHandler initialized with output dir: {output_dir}") def generate_html(self, title, body, tags, image_url=None, original_url=None): """ Generate HTML content from article data. Args: title (str): Article title body (str): Article body (HTML) tags (list): List of tags image_url (str, optional): Featured image URL original_url (str, optional): Original article URL Returns: str: Generated HTML content """ try: # Format tags as HTML tags_html = " ".join([f'{tag}' for tag in tags]) # Build featured image section image_section = "" if image_url: image_section = ( f'" ) # Generate complete HTML html_content = f""" {title}

{title}

Generated on {datetime.now().strftime('%B %d, %Y at %I:%M %p')}
{tags_html}
{image_section}
{body}
""" logger.debug("HTML content generated successfully") return html_content except Exception as e: logger.error(f"Failed to generate HTML: {e}") raise def save_html(self, html_content, filename=None): """ Save HTML content to file. Args: html_content (str): HTML content to save filename (str, optional): Filename to use. If None, generates timestamp-based name. Returns: str: Full path to saved file """ try: if not filename: timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") filename = f"article_{timestamp}.html" file_path = os.path.join(self.output_dir, filename) with open(file_path, "w", encoding="utf-8") as f: f.write(html_content) logger.info(f"✓ HTML file saved: {file_path}") return file_path except Exception as e: logger.error(f"Failed to save HTML file: {e}") raise def generate_and_save(self, title, body, tags, image_url=None, original_url=None, filename=None): """ Generate and save HTML in one call. Args: title (str): Article title body (str): Article body (HTML) tags (list): List of tags image_url (str, optional): Featured image URL original_url (str, optional): Original article URL filename (str, optional): Custom filename Returns: str: Full path to saved file """ html_content = self.generate_html(title, body, tags, image_url, original_url) return self.save_html(html_content, filename)