""" Export operations for CodiMD CLI. """ import re from typing import Optional from pathlib import Path from .client import CodiMDClient class ExportManager: """Manager for export operations.""" def __init__(self, client: CodiMDClient): self.client = client def export_markdown(self, note_id: str, output_file: Optional[Path] = None) -> str: """Export note as markdown.""" response = self.client.get(f"/{note_id}/download") if response.status_code == 404: raise FileNotFoundError(f"Note not found: {note_id}") if response.status_code == 403: raise PermissionError("No permission to export this note") if response.status_code != 200: raise Exception(f"Failed to export markdown: {response.status_code}") content = response.text if output_file: output_file.write_text(content) return str(output_file) return content def export_pdf(self, note_id: str, output_file: Optional[Path] = None) -> bytes: """Export note as PDF.""" response = self.client.get(f"/{note_id}/pdf") if response.status_code == 403: raise PermissionError("PDF export disabled or no permission") if response.status_code == 404: raise FileNotFoundError(f"Note not found: {note_id}") if response.status_code != 200: raise Exception(f"Failed to export PDF: {response.status_code}") content = response.content if output_file: output_file.write_bytes(content) return str(output_file) return content def export_html(self, note_id: str, output_file: Optional[Path] = None) -> str: """Export note as HTML (published view).""" # First publish the note publish_response = self.client.get(f"/{note_id}/publish") if publish_response.status_code != 200: raise Exception(f"Failed to publish note: {publish_response.status_code}") # Extract the short ID from the response url = publish_response.url short_id = url.rstrip("/").split("/")[-1] # Get the published view response = self.client.get(f"/s/{short_id}") if response.status_code != 200: raise Exception(f"Failed to export HTML: {response.status_code}") content = response.text if output_file: output_file.write_text(content) return str(output_file) return content def export_slide(self, note_id: str, output_file: Optional[Path] = None) -> str: """Export note as reveal.js slides.""" response = self.client.get(f"/{note_id}/slide") if response.status_code == 404: raise FileNotFoundError(f"Note not found: {note_id}") if response.status_code != 200: raise Exception(f"Failed to export slides: {response.status_code}") content = response.text if output_file: output_file.write_text(content) return str(output_file) return content