gateway/modules/services/serviceGeneration/renderers/markdown_renderer.py
2025-10-03 01:41:50 +02:00

89 lines
3 KiB
Python

"""
Markdown renderer for report generation.
"""
from .base_renderer import BaseRenderer
from typing import Dict, Any, Tuple, List
class MarkdownRenderer(BaseRenderer):
"""Renders content to Markdown format with format-specific extraction."""
@classmethod
def get_supported_formats(cls) -> List[str]:
"""Return supported Markdown formats."""
return ['md', 'markdown']
@classmethod
def get_format_aliases(cls) -> List[str]:
"""Return format aliases."""
return ['mdown', 'mkd']
@classmethod
def get_priority(cls) -> int:
"""Return priority for markdown renderer."""
return 95
def getExtractionPrompt(self, user_prompt: str, title: str) -> str:
"""Get Markdown-specific extraction prompt."""
return f"""
{user_prompt}
Generate a comprehensive Markdown report with the title: "{title}"
MARKDOWN FORMAT REQUIREMENTS:
- Use proper Markdown syntax throughout
- Use # for main title, ## for sections, ### for subsections
- Use tables with | separators and header rows
- Use bullet points with - or *
- Use **bold** and *italic* for emphasis
- Use code blocks with ``` for code/examples
- Include emojis for visual appeal (📋, 📊, ✅, etc.)
- Use horizontal rules (---) to separate sections
- Include source document information
FORMATTING RULES:
- Title: Use # Title
- Main sections: Use ## Section Name
- Subsections: Use ### Subsection Name
- Tables: Use | Header | Header | format with separator row
- Lists: Use - for bullet points
- Emphasis: Use **bold** and *italic*
- Code: Use ```language for code blocks
- Links: Use [text](url) format
- Images: Use ![alt](url) format
OUTPUT POLICY:
- Return ONLY Markdown content
- No HTML, no plain text, no code blocks around content
- Clean, readable Markdown format
- Professional appearance with good structure
- Include all necessary information
Generate the complete Markdown report:
"""
async def render(self, extracted_content: str, title: str) -> Tuple[str, str]:
"""Render extracted content to Markdown format."""
try:
# The extracted content should already be Markdown from the AI
# Just clean it up
markdown_content = self._clean_markdown_content(extracted_content, title)
return markdown_content, "text/markdown"
except Exception as e:
self.logger.error(f"Error rendering markdown: {str(e)}")
# Return minimal markdown fallback
return f"# {title}\n\nError rendering report: {str(e)}", "text/markdown"
def _clean_markdown_content(self, content: str, title: str) -> str:
"""Clean and validate Markdown 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()
return content