import os import tempfile import uuid from datetime import datetime from pathlib import Path from typing import Literal import markdown from weasyprint import HTML from app.core.config import settings from app.models.note import Note def export_note(note: Note, format_type: Literal["markdown", "txt", "pdf"]) -> Path: """ Export a note to the specified format. Args: note: The note to export format_type: The format to export to (markdown, txt, pdf) Returns: Path to the exported file """ # Create a unique filename timestamp = datetime.now().strftime("%Y%m%d%H%M%S") unique_id = str(uuid.uuid4())[:8] if format_type == "markdown": return _export_markdown(note, timestamp, unique_id) elif format_type == "txt": return _export_txt(note, timestamp, unique_id) elif format_type == "pdf": return _export_pdf(note, timestamp, unique_id) else: raise ValueError(f"Unsupported format: {format_type}") def _export_markdown(note: Note, timestamp: str, unique_id: str) -> Path: """Export note to Markdown format.""" filename = f"{note.title.replace(' ', '_')}_{timestamp}_{unique_id}.md" export_path = settings.EXPORT_DIR / filename with open(export_path, "w") as f: f.write(f"# {note.title}\n\n") f.write(note.content) return export_path def _export_txt(note: Note, timestamp: str, unique_id: str) -> Path: """Export note to plain text format.""" filename = f"{note.title.replace(' ', '_')}_{timestamp}_{unique_id}.txt" export_path = settings.EXPORT_DIR / filename with open(export_path, "w") as f: f.write(f"Title: {note.title}\n\n") f.write(note.content) return export_path def _export_pdf(note: Note, timestamp: str, unique_id: str) -> Path: """Export note to PDF format.""" filename = f"{note.title.replace(' ', '_')}_{timestamp}_{unique_id}.pdf" export_path = settings.EXPORT_DIR / filename # Convert markdown to HTML html_content = f"