""" Output handling for document summaries in multiple formats. """ import os from app.config.settings import SUMMARIES_OUTPUT_DIR class SummaryOutputManager: """ Manager for saving and retrieving document summaries in multiple formats. """ def __init__(self, output_dir=SUMMARIES_OUTPUT_DIR): """ Initialize output manager with output directory. Args: output_dir (str): Directory to save summaries """ self.output_dir = output_dir self._ensure_output_dir() def _ensure_output_dir(self): """Create output directory if it doesn't exist.""" if not os.path.exists(self.output_dir): os.makedirs(self.output_dir) print(f"Created output directory: {self.output_dir}") def save_summary(self, filename, summary, formats=None): """ Save a document summary to files in specified formats. Args: filename (str): Name of the original document summary (str): Summary text formats (list): List of formats to save. Defaults to ['markdown', 'html'] Returns: dict: Paths to the saved summary files by format """ if formats is None: formats = ['markdown', 'html'] output_paths = {} # Generate and save in each requested format for fmt in formats: if fmt == 'markdown': output_paths['markdown'] = self._save_markdown(filename, summary) elif fmt == 'html': output_paths['html'] = self._save_html(filename, summary) else: print(f"Warning: Unsupported format '{fmt}' requested") return output_paths def _save_markdown(self, filename, summary): """ Save a document summary to a markdown file. Args: filename (str): Name of the original document summary (str): Summary text Returns: str: Path to the saved markdown file """ # Create markdown output markdown_content = f"" markdown_content += summary markdown_content += "\n\n---\n" # Save to file output_path = os.path.join(self.output_dir, f"{filename}.md") with open(output_path, "w") as f: f.write(markdown_content) print(f"Saved markdown summary to: {output_path}") return output_path def _save_html(self, filename, summary): """ Save a document summary to an HTML file. Args: filename (str): Name of the original document summary (str): Summary text Returns: str: Path to the saved HTML file """ # Convert summary to HTML paragraphs paragraphs = summary.split('\n\n') html_paragraphs = ''.join([f"
{p}
" for p in paragraphs if p.strip()]) # Create HTML output with basic styling html_content = f"""