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.

13.1 KB
Raw Blame
"""ODT export service using pypandoc.

This module provides the ODTExporter class for converting markdown content
to OpenDocument Text (ODT) format using pypandoc (Pandoc wrapper).

Pandoc is the industry standard for document conversion and handles all
markdown features natively. Note: Requires Pandoc to be installed on the
system or use pypandoc_binary which bundles it.
"""

import functools
import subprocess
from typing import Optional

from loguru import logger

# pypandoc is optional - used only to locate the bundled pandoc binary
try:
    import pypandoc

    PYPANDOC_AVAILABLE = True
except ImportError:
    pypandoc = None
    PYPANDOC_AVAILABLE = False

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


@ExporterRegistry.register
class ODTExporter(BaseExporter):
    """Service for converting markdown to ODT using pypandoc.

    This exporter uses Pandoc (via pypandoc) to convert markdown content
    to OpenDocument Text format, which can be opened in LibreOffice Writer,
    Microsoft Word, and other office applications.

    Pandoc is the industry standard for document conversion and handles
    all markdown features (tables, code blocks, lists, etc.) natively.

    The conversion is performed entirely in memory by piping markdown
    to pandoc's stdin and capturing ODT output from stdout.
    """

    #: Conversion wall clock; a pathological report must not pin an
    #: export worker indefinitely.
    DEFAULT_CONVERSION_TIMEOUT_S = 120
    #: Output-size ceiling, checked after pandoc's stdout has been
    #: captured in full, so it rejects oversized output rather than
    #: bounding capture memory. The ODT of a maximum-size report stays
    #: far below this.
    DEFAULT_MAX_OUTPUT_BYTES = 512 * 1024 * 1024
    #: The bundled pandoc accepts GHC RTS heap limits. Other builds that
    #: reject RTS flags fail the conversion rather than running uncapped.
    DEFAULT_MAX_HEAP_MB = 1024
    #: Pandoc's markdown reader with raw passthrough switched off. Report
    #: text is LLM- and web-derived. With these extensions on (pandoc's
    #: default), a ``{=opendocument}`` raw block or inline is copied into
    #: the document as-is, while raw HTML and TeX are silently dropped from
    #: the ODT. ``<div>`` and ``<span>`` tags are read as document
    #: structure even with raw HTML off: a ``custom-style`` attribute
    #: becomes an ODT style name, a div's ``id`` a section, and a ``<div>``
    #: whose closing tag is on the same line stays open to the end of the
    #: document. A ``---`` line directly followed by text opens a YAML
    #: metadata block, which moves report text into document properties,
    #: or fails the export when that text is not YAML (#7821). With
    #: ``yaml_metadata_block`` off, a ``---`` line directly followed by
    #: text, with the next ``---`` rule after a blank line (the footer's
    #: ``---`` rule, or ``topic_organization_strategy.py``'s
    #: ``\\n---\\n**Total Sources Organized:**``), would be read by
    #: ``multiline_tables`` as a one-column table wrapping the intervening
    #: report section and headings into a single cell, and an indented
    #: rule would cut the leading characters off every following line.
    #: With ``multiline_tables`` off, that shape stays a horizontal rule.
    #: A ``---`` line, lines of text and another ``---`` line with no
    #: blank line between them are still a simple table, and pipe tables
    #: are unaffected. Genuine Pandoc multiline-table syntax in a report
    #: is no longer read as a multiline table: body rows render as
    #: paragraphs, and a headed table's header lines may render as a
    #: one-column table. With all
    #: of these off, report text reaches the document as text, consistent
    #: with the PDF export and the on-screen report view, which escape
    #: raw HTML. Ordinary markdown converts identically, and the title,
    #: author and date still come from the ``--metadata`` arguments.
    #: Pandoc rejects an extension name it does not know, so a future
    #: rename fails the export instead of quietly restoring passthrough;
    #: every name here exists in every pandoc that supports ``--sandbox``
    #: (2.15+), which export requires.
    READER_FORMAT = (
        "markdown-raw_attribute-raw_html-raw_tex"
        "-native_divs-native_spans-yaml_metadata_block-multiline_tables"
    )

    def __init__(
        self,
        *,
        conversion_timeout_s: int = DEFAULT_CONVERSION_TIMEOUT_S,
        max_output_bytes: int = DEFAULT_MAX_OUTPUT_BYTES,
        max_heap_mb: int = DEFAULT_MAX_HEAP_MB,
    ):
        if not isinstance(max_heap_mb, int) or not (
            0 < max_heap_mb <= self.DEFAULT_MAX_HEAP_MB
        ):
            raise ValueError(
                f"max_heap_mb must be between 1 and {self.DEFAULT_MAX_HEAP_MB}"
            )
        self._conversion_timeout_s = conversion_timeout_s
        self._max_output_bytes = max_output_bytes
        self._max_heap_mb = max_heap_mb

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

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

    @property
    def mimetype(self) -> str:
        return "application/vnd.oasis.opendocument.text"

    @staticmethod
    @functools.lru_cache(maxsize=1)
    def _pandoc_supports_sandbox() -> bool:
        """Whether pandoc knows ``--sandbox`` (added in pandoc 2.15, 2021).

        Cached: the check spawns a pandoc --version subprocess, and
        exports are rare enough that one probe per process is plenty.
        Raises if the version cannot be determined; lru_cache does not
        cache exceptions, so a transient probe failure is retried on the
        next export instead of disabling the sandbox for the process.
        """
        version = str(pypandoc.get_pandoc_version()).strip()
        parts = tuple(int(x) for x in version.split(".")[:2])
        return parts >= (2, 15)

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

        The conversion runs entirely in memory: markdown is piped to
        pandoc's stdin and ODT bytes are captured from stdout, avoiding
        any temporary files on disk.

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

        Returns:
            ExportResult with ODT content as bytes, filename, and mimetype

        Raises:
            ValueError: If content exceeds maximum size limit
            RuntimeError: If Pandoc conversion fails
        """
        # Check if pypandoc is available
        if not PYPANDOC_AVAILABLE:
            raise RuntimeError(
                "ODT export requires pypandoc. Install with: pip install pypandoc-binary"
            )

        try:
            # Check content size limit to prevent OOM errors
            self._validate_content_size(markdown_content)

            options = options or ExportOptions()

            # Prepend title if needed (for document formats like ODT)
            markdown_content = self._prepend_title_if_needed(
                markdown_content, options.title
            )

            # Add LDR attribution footer
            content_with_footer = self._add_footer(markdown_content)

            # Build pandoc args for metadata (sanitized)
            extra_args = []
            if options.title:
                safe_title = self._sanitize_metadata(options.title)
                extra_args.append(f"--metadata=title:{safe_title}")
            if options.metadata:
                if options.metadata.get("author"):
                    safe_author = self._sanitize_metadata(
                        options.metadata["author"]
                    )
                    extra_args.append(f"--metadata=author:{safe_author}")
                if options.metadata.get("date"):
                    safe_date = self._sanitize_metadata(
                        options.metadata["date"]
                    )
                    extra_args.append(f"--metadata=date:{safe_date}")

            # Convert in memory: pipe markdown via stdin, capture ODT from stdout
            pandoc_path = pypandoc.get_pandoc_path()
            # --sandbox keeps pandoc from reading local files or fetching
            # network resources referenced in the (web-influenced) report
            # markdown. Refuse the export if its support cannot be verified:
            # running without the flag would restore arbitrary file reads
            # and SSRF through markdown image references.
            try:
                supports_sandbox = self._pandoc_supports_sandbox()
            except Exception as exc:
                logger.warning(
                    "Could not verify pandoc sandbox support ({})",
                    type(exc).__name__,
                )
                raise RuntimeError(
                    "ODT export requires pandoc 2.15 or newer with --sandbox"
                ) from None
            if not supports_sandbox:
                raise RuntimeError(  # noqa: TRY301 — fail closed before subprocess
                    "ODT export requires pandoc 2.15 or newer with --sandbox"
                )
            # A report far below BaseExporter.MAX_CONTENT_SIZE can make
            # pandoc consume several GiB. The shipped GHC build honours
            # -M; builds without RTS support reject the command before
            # processing markdown, and we never retry without the cap.
            cmd = [
                pandoc_path,
                "+RTS",
                f"-M{self._max_heap_mb}m",
                "-RTS",
                "--sandbox",
            ]
            cmd.extend(["-f", self.READER_FORMAT, "-t", "odt", "-o", "-"])
            cmd.extend(extra_args)

            try:
                result = subprocess.run(
                    cmd,
                    input=content_with_footer.encode("utf-8"),
                    capture_output=True,
                    check=True,
                    timeout=self._conversion_timeout_s,
                )
            except subprocess.TimeoutExpired as exc:
                raise RuntimeError(
                    f"ODT export timed out after {self._conversion_timeout_s}s"
                ) from exc

            odt_bytes = result.stdout
            if len(odt_bytes) > self._max_output_bytes:
                raise RuntimeError(  # noqa: TRY301 — same pattern as the no-output raise below
                    "ODT export output is larger than "
                    f"{self._max_output_bytes} bytes"
                )

            if not odt_bytes:
                raise RuntimeError(  # noqa: TRY301 — except only adds logging before re-raise
                    "Pandoc conversion failed - no output produced"
                )

            filename = self._generate_safe_filename(options.title)

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

            return ExportResult(
                content=odt_bytes,
                filename=filename,
                mimetype=self.mimetype,
            )

        except subprocess.CalledProcessError as e:
            # pandoc's stderr quotes input lines — i.e. report content.
            # Bound what reaches logs and error surfaces.
            stderr_full = (
                e.stderr.decode("utf-8", errors="replace")
                if e.stderr
                else "unknown error"
            )
            stderr = stderr_full[-2000:]
            logger.exception("Pandoc conversion failed: {}", stderr)
            raise RuntimeError(f"Pandoc conversion failed: {stderr}") from e
        except Exception:
            logger.exception("Error generating ODT")
            raise

    def _add_footer(self, markdown_content: str) -> str:
        """Add LDR attribution footer to markdown content.

        Args:
            markdown_content: The original markdown text

        Returns:
            Markdown with footer appended
        """
        footer = (
            "\n\n---\n\n"
            "*Generated by [LDR - Local Deep Research]"
            "(https://github.com/LearningCircuit/local-deep-research) | "
            "Open Source AI Research Assistant*"
        )
        return markdown_content + footer

    def _sanitize_metadata(self, value: str) -> str:
        """Sanitize metadata value to prevent argument injection.

        Removes potential pandoc argument injection patterns from user-supplied
        metadata values.

        Also strips NUL characters (U+0000): Python's subprocess rejects any
        argv element containing NUL with ``ValueError: embedded null byte``
        before Pandoc starts, so a NUL in the title, author, or date would
        otherwise fail the whole export (#5995).

        Args:
            value: The metadata value to sanitize

        Returns:
            Sanitized metadata value safe for pandoc arguments
        """
        # Remove potential argument injection patterns. NUL is stripped
        # first so it cannot join two dashes into a fresh "--" pair after
        # the injection-pattern pass has already run.
        return value.replace("\x00", "").replace("--", "").replace("\n", " ")