gateway/modules/services/serviceGeneration/renderers/html_renderer.py
2025-10-06 15:39:25 +02:00

69 lines
2.9 KiB
Python

"""
HTML renderer for report generation.
"""
from .base_renderer import BaseRenderer
from typing import Dict, Any, Tuple, List
class HtmlRenderer(BaseRenderer):
"""Renders content to HTML format with format-specific extraction."""
@classmethod
def get_supported_formats(cls) -> List[str]:
"""Return supported HTML formats."""
return ['html', 'htm']
@classmethod
def get_format_aliases(cls) -> List[str]:
"""Return format aliases."""
return ['web', 'webpage']
@classmethod
def get_priority(cls) -> int:
"""Return priority for HTML renderer."""
return 100
def getExtractionPrompt(self, user_prompt: str, title: str) -> str:
"""Return only HTML-specific guidelines; global prompt is built centrally."""
return (
"HTML FORMAT GUIDELINES:\n"
"- Output a complete HTML5 document starting with <!DOCTYPE html>.\n"
"- Include <html>, <head> with <meta charset=\"UTF-8\"> and <title>, and <body>.\n"
"- Use semantic elements: <header>, <main>, <section>, <article>, <footer>.\n"
"- Provide professional CSS in a <style> block; responsive, clean typography.\n"
"- Use h1/h2/h3 for headings; tables and lists for structure.\n"
"OUTPUT: Return ONLY valid HTML (no markdown, no code fences)."
)
async def render(self, extracted_content: str, title: str) -> Tuple[str, str]:
"""Render extracted content to HTML format."""
try:
# The extracted content should already be HTML from the AI
# Just clean it up and ensure it's valid
html_content = self._clean_html_content(extracted_content, title)
return html_content, "text/html"
except Exception as e:
self.logger.error(f"Error rendering HTML: {str(e)}")
# Return minimal HTML fallback
return f"<html><head><title>{title}</title></head><body><h1>{title}</h1><p>Error rendering report: {str(e)}</p></body></html>", "text/html"
def _clean_html_content(self, content: str, title: str) -> str:
"""Clean and validate HTML content from AI."""
content = content.strip()
# Remove markdown code blocks if present
if content.startswith("```") and content.endswith("```"):
lines = content.split('\n')
if len(lines) > 2:
content = '\n'.join(lines[1:-1]).strip()
# Ensure it starts with DOCTYPE
if not content.startswith('<!DOCTYPE'):
if content.startswith('<html'):
content = '<!DOCTYPE html>\n' + content
else:
content = f'<!DOCTYPE html>\n<html>\n<head><meta charset="UTF-8"><title>{title}</title></head>\n<body>\n{content}\n</body>\n</html>'
return content