65 lines
2.6 KiB
Python
65 lines
2.6 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:
|
|
"""Return only Markdown-specific guidelines; global prompt is built centrally."""
|
|
return (
|
|
"MARKDOWN FORMAT GUIDELINES:\n"
|
|
"- Use proper Markdown syntax only (no HTML wrappers).\n"
|
|
"- # for main title, ## for sections, ### for subsections.\n"
|
|
"- Tables with | separators and a header row.\n"
|
|
"- Bullet lists with - or *.\n"
|
|
"- Emphasis with **bold** and *italic*.\n"
|
|
"- Code blocks with ```language.\n"
|
|
"- Horizontal rules (---) to separate major sections when helpful.\n"
|
|
"- Include links [text](url) and images  when referenced by sources.\n"
|
|
"OUTPUT: Return ONLY raw Markdown content without code fences."
|
|
)
|
|
|
|
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
|