258 lines
10 KiB
Python
258 lines
10 KiB
Python
# Copyright (c) 2025 Patrick Motsch
|
|
# All rights reserved.
|
|
"""
|
|
Text renderer for report generation.
|
|
"""
|
|
|
|
from .rendererBaseTemplate import BaseRenderer
|
|
from typing import Dict, Any, Tuple, List
|
|
|
|
class RendererText(BaseRenderer):
|
|
"""Renders content to plain text format with format-specific extraction."""
|
|
|
|
@classmethod
|
|
def getSupportedFormats(cls) -> List[str]:
|
|
"""Return supported text formats (excluding formats with dedicated renderers)."""
|
|
return [
|
|
'txt', 'text', 'plain',
|
|
# Programming languages
|
|
'js', 'javascript', 'ts', 'typescript', 'jsx', 'tsx',
|
|
'py', 'python', 'java', 'cpp', 'c', 'h', 'hpp',
|
|
'cs', 'csharp', 'php', 'rb', 'ruby', 'go', 'rs', 'rust',
|
|
'swift', 'kt', 'kotlin', 'scala', 'r', 'm', 'objc',
|
|
'sh', 'bash', 'zsh', 'fish', 'ps1', 'bat', 'cmd',
|
|
# Web technologies (excluding html/htm which have dedicated renderer)
|
|
'css', 'scss', 'sass', 'less', 'xml', 'yaml', 'yml', 'toml', 'ini', 'cfg',
|
|
# Data formats (excluding csv, md/markdown which have dedicated renderers)
|
|
'tsv', 'log', 'rst', 'sql', 'dockerfile', 'dockerignore', 'gitignore',
|
|
# Configuration files
|
|
'env', 'properties', 'conf', 'config', 'rc',
|
|
'gitattributes', 'editorconfig', 'eslintrc',
|
|
# Documentation
|
|
'readme', 'changelog', 'license', 'authors',
|
|
'contributing', 'todo', 'notes', 'docs'
|
|
]
|
|
|
|
@classmethod
|
|
def getFormatAliases(cls) -> List[str]:
|
|
"""Return format aliases."""
|
|
return [
|
|
'ascii', 'utf8', 'utf-8', 'code', 'source',
|
|
'script', 'program', 'file', 'document',
|
|
'raw', 'unformatted', 'plaintext'
|
|
]
|
|
|
|
@classmethod
|
|
def getPriority(cls) -> int:
|
|
"""Return priority for text renderer."""
|
|
return 90
|
|
|
|
async def render(self, extractedContent: Dict[str, Any], title: str, userPrompt: str = None, aiService=None) -> Tuple[str, str]:
|
|
"""Render extracted JSON content to plain text format."""
|
|
try:
|
|
# Generate text from JSON structure
|
|
textContent = self._generateTextFromJson(extractedContent, title)
|
|
|
|
return textContent, "text/plain"
|
|
|
|
except Exception as e:
|
|
self.logger.error(f"Error rendering text: {str(e)}")
|
|
# Return minimal text fallback
|
|
return f"{title}\n\nError rendering report: {str(e)}", "text/plain"
|
|
|
|
def _generateTextFromJson(self, jsonContent: Dict[str, Any], title: str) -> str:
|
|
"""Generate text content from structured JSON document."""
|
|
try:
|
|
# Validate JSON structure
|
|
if not isinstance(jsonContent, dict):
|
|
raise ValueError("JSON content must be a dictionary")
|
|
|
|
if "sections" not in jsonContent:
|
|
raise ValueError("JSON content must contain 'sections' field")
|
|
|
|
# Use title from JSON metadata if available, otherwise use provided title
|
|
documentTitle = jsonContent.get("metadata", {}).get("title", title)
|
|
|
|
# Build text content
|
|
textParts = []
|
|
|
|
# Document title
|
|
textParts.append(documentTitle)
|
|
textParts.append("=" * len(documentTitle))
|
|
textParts.append("")
|
|
|
|
# Process each section
|
|
sections = jsonContent.get("sections", [])
|
|
for section in sections:
|
|
sectionText = self._renderJsonSection(section)
|
|
if sectionText:
|
|
textParts.append(sectionText)
|
|
textParts.append("") # Add spacing between sections
|
|
|
|
# Add generation info
|
|
textParts.append("")
|
|
textParts.append(f"Generated: {self._formatTimestamp()}")
|
|
|
|
return '\n'.join(textParts)
|
|
|
|
except Exception as e:
|
|
self.logger.error(f"Error generating text from JSON: {str(e)}")
|
|
raise Exception(f"Text generation failed: {str(e)}")
|
|
|
|
def _renderJsonSection(self, section: Dict[str, Any]) -> str:
|
|
"""Render a single JSON section to text."""
|
|
try:
|
|
sectionType = self._getSectionType(section)
|
|
sectionData = self._getSectionData(section)
|
|
|
|
if sectionType == "table":
|
|
# Process the section data to extract table structure
|
|
processedData = self._processSectionByType(section)
|
|
return self._renderJsonTable(processedData)
|
|
elif sectionType == "bullet_list":
|
|
# Process the section data to extract bullet list structure
|
|
processedData = self._processSectionByType(section)
|
|
return self._renderJsonBulletList(processedData)
|
|
elif sectionType == "heading":
|
|
# Render each heading element in the elements array
|
|
# sectionData is already the elements array from _getSectionData
|
|
renderedElements = []
|
|
for element in sectionData:
|
|
renderedElements.append(self._renderJsonHeading(element))
|
|
return "\n".join(renderedElements)
|
|
elif sectionType == "paragraph":
|
|
# Render each paragraph element in the elements array
|
|
# sectionData is already the elements array from _getSectionData
|
|
renderedElements = []
|
|
for element in sectionData:
|
|
renderedElements.append(self._renderJsonParagraph(element))
|
|
return "\n".join(renderedElements)
|
|
elif sectionType == "code_block":
|
|
# Process the section data to extract code block structure
|
|
processedData = self._processSectionByType(section)
|
|
return self._renderJsonCodeBlock(processedData)
|
|
elif sectionType == "image":
|
|
# Process the section data to extract image structure
|
|
processedData = self._processSectionByType(section)
|
|
return self._renderJsonImage(processedData)
|
|
else:
|
|
# Fallback to paragraph for unknown types - render each element
|
|
# sectionData is already the elements array from _getSectionData
|
|
renderedElements = []
|
|
for element in sectionData:
|
|
renderedElements.append(self._renderJsonParagraph(element))
|
|
return "\n".join(renderedElements)
|
|
|
|
except Exception as e:
|
|
self.logger.warning(f"Error rendering section {self._getSectionId(section)}: {str(e)}")
|
|
return f"[Error rendering section: {str(e)}]"
|
|
|
|
def _renderJsonTable(self, tableData: Dict[str, Any]) -> str:
|
|
"""Render a JSON table to text."""
|
|
try:
|
|
headers = tableData.get("headers", [])
|
|
rows = tableData.get("rows", [])
|
|
|
|
if not headers or not rows:
|
|
return ""
|
|
|
|
textParts = []
|
|
|
|
# Create table header
|
|
headerLine = " | ".join(str(header) for header in headers)
|
|
textParts.append(headerLine)
|
|
|
|
# Add separator line
|
|
separatorLine = " | ".join("-" * len(str(header)) for header in headers)
|
|
textParts.append(separatorLine)
|
|
|
|
# Add data rows
|
|
for row in rows:
|
|
rowLine = " | ".join(str(cellData) for cellData in row)
|
|
textParts.append(rowLine)
|
|
|
|
return '\n'.join(textParts)
|
|
|
|
except Exception as e:
|
|
self.logger.warning(f"Error rendering table: {str(e)}")
|
|
return ""
|
|
|
|
def _renderJsonBulletList(self, listData: Dict[str, Any]) -> str:
|
|
"""Render a JSON bullet list to text."""
|
|
try:
|
|
items = listData.get("items", [])
|
|
|
|
if not items:
|
|
return ""
|
|
|
|
textParts = []
|
|
for item in items:
|
|
if isinstance(item, str):
|
|
textParts.append(f"- {item}")
|
|
elif isinstance(item, dict) and "text" in item:
|
|
textParts.append(f"- {item['text']}")
|
|
|
|
return '\n'.join(textParts)
|
|
|
|
except Exception as e:
|
|
self.logger.warning(f"Error rendering bullet list: {str(e)}")
|
|
return ""
|
|
|
|
def _renderJsonHeading(self, headingData: Dict[str, Any]) -> str:
|
|
"""Render a JSON heading to text."""
|
|
try:
|
|
level = headingData.get("level", 1)
|
|
text = headingData.get("text", "")
|
|
|
|
if text:
|
|
level = max(1, min(6, level))
|
|
if level == 1:
|
|
return f"{text}\n{'=' * len(text)}"
|
|
elif level == 2:
|
|
return f"{text}\n{'-' * len(text)}"
|
|
else:
|
|
return f"{'#' * level} {text}"
|
|
|
|
return ""
|
|
|
|
except Exception as e:
|
|
self.logger.warning(f"Error rendering heading: {str(e)}")
|
|
return ""
|
|
|
|
def _renderJsonParagraph(self, paragraphData: Dict[str, Any]) -> str:
|
|
"""Render a JSON paragraph to text."""
|
|
try:
|
|
text = paragraphData.get("text", "")
|
|
return text if text else ""
|
|
|
|
except Exception as e:
|
|
self.logger.warning(f"Error rendering paragraph: {str(e)}")
|
|
return ""
|
|
|
|
def _renderJsonCodeBlock(self, codeData: Dict[str, Any]) -> str:
|
|
"""Render a JSON code block to text."""
|
|
try:
|
|
code = codeData.get("code", "")
|
|
language = codeData.get("language", "")
|
|
|
|
if code:
|
|
if language:
|
|
return f"Code ({language}):\n{code}"
|
|
else:
|
|
return code
|
|
|
|
return ""
|
|
|
|
except Exception as e:
|
|
self.logger.warning(f"Error rendering code block: {str(e)}")
|
|
return ""
|
|
|
|
def _renderJsonImage(self, imageData: Dict[str, Any]) -> str:
|
|
"""Render a JSON image to text."""
|
|
try:
|
|
altText = imageData.get("altText", "Image")
|
|
return f"[Image: {altText}]"
|
|
|
|
except Exception as e:
|
|
self.logger.warning(f"Error rendering image: {str(e)}")
|
|
return f"[Image: {imageData.get('altText', 'Image')}]"
|