Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

2.3 KB
Raw Blame
"""LaTeX export service.

This module provides the LaTeXExporter class that wraps the existing
LaTeXExporter implementation from text_optimization.citation_formatter.
"""

from typing import Optional

from loguru import logger

from .base import BaseExporter, ExportOptions, ExportResult
from .registry import ExporterRegistry


@ExporterRegistry.register
class LaTeXExporter(BaseExporter):
    """LaTeX exporter using the existing LaTeXExporter implementation.

    This exporter converts markdown content to LaTeX format suitable for
    compilation with pdflatex or similar LaTeX processors.
    """

    def __init__(self):
        """Initialize LaTeX exporter."""
        # Import here to avoid circular imports
        from ..text_optimization.citation_formatter import (
            LaTeXExporter as LegacyLaTeXExporter,
        )

        self._legacy = LegacyLaTeXExporter()

    @property
    def format_name(self) -> str:
        return "latex"

    @property
    def file_extension(self) -> str:
        return ".tex"

    @property
    def mimetype(self) -> str:
        return "text/plain"

    def export(
        self,
        markdown_content: str,
        options: Optional[ExportOptions] = None,
    ) -> ExportResult:
        """Convert markdown content to LaTeX.

        Args:
            markdown_content: The markdown text to convert
            options: Optional export options (title, metadata)

        Returns:
            ExportResult with LaTeX content as UTF-8 bytes, filename, and mimetype

        Raises:
            ValueError: If content exceeds maximum size limit
        """
        try:
            # Check content size limit to prevent OOM errors
            self._validate_content_size(markdown_content)

            options = options or ExportOptions()

            content = self._legacy.export_to_latex(markdown_content)
            filename = self._generate_safe_filename(options.title)

            logger.info(
                f"Generated LaTeX in memory, size: {len(content)} bytes"
            )

            return ExportResult(
                content=content.encode("utf-8"),
                filename=filename,
                mimetype=self.mimetype,
            )

        except Exception:
            logger.exception("Error generating LaTeX")
            raise