Skip to content

ddocs.markdown.pandoc_utils

Locate, verify, and install the Pandoc executable that the conversion pipeline shells out to. Confirms that pandoc is callable, downloads a prebuilt binary via pypandoc when it is missing, and prepends its directory to PATH for the current process.

ddocs.markdown.pandoc_utils

Locate and verify the Pandoc executable for the ddocs pipeline.

The ddocs Markdown-to-LaTeX conversion shells out to the pandoc command, so pandoc must be reachable on PATH. The pandoc binary ships bundled with the pypandoc-binary dependency, so nothing is downloaded at runtime: this module verifies that pandoc is callable and, when it is not already on PATH, makes the bundled binary discoverable by prepending its directory to PATH for the current process (and any subprocess it spawns).

sanity_check

sanity_check() -> bool

Check whether a pandoc executable is callable from the command line.

Runs pandoc --version as a subprocess. On success the detected version is printed and True is returned; if pandoc is missing from PATH or the call fails, False is returned. This only inspects what is already reachable -- it never downloads or installs anything.

Returns:

Type Description
bool

True if pandoc --version runs successfully, False otherwise.

Examples:

  • Branch on whether pandoc is available:
    >>> from ddocs.markdown.pandoc_utils import sanity_check
    >>> if sanity_check():  # doctest: +SKIP
    ...     print("ready to convert")
    ... else:
    ...     print("pandoc missing")
    ready to convert
    
  • Use the boolean result to pick a fallback message:
    >>> from ddocs.markdown.pandoc_utils import sanity_check
    >>> available = sanity_check()  # doctest: +SKIP
    >>> "ok" if available else "install pandoc first"  # doctest: +SKIP
    'ok'
    
See Also

check_pandoc_installed: Verify pandoc and download it when it is missing.

Source code in src\ddocs\markdown\pandoc_utils.py
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
def sanity_check() -> bool:
    """Check whether a ``pandoc`` executable is callable from the command line.

    Runs ``pandoc --version`` as a subprocess. On success the detected version is
    printed and ``True`` is returned; if pandoc is missing from ``PATH`` or the
    call fails, ``False`` is returned. This only inspects what is already
    reachable -- it never downloads or installs anything.

    Returns:
        True if ``pandoc --version`` runs successfully, False otherwise.

    Examples:
        - Branch on whether pandoc is available:
            ```python
            >>> from ddocs.markdown.pandoc_utils import sanity_check
            >>> if sanity_check():  # doctest: +SKIP
            ...     print("ready to convert")
            ... else:
            ...     print("pandoc missing")
            ready to convert

            ```
        - Use the boolean result to pick a fallback message:
            ```python
            >>> from ddocs.markdown.pandoc_utils import sanity_check
            >>> available = sanity_check()  # doctest: +SKIP
            >>> "ok" if available else "install pandoc first"  # doctest: +SKIP
            'ok'

            ```

    See Also:
        check_pandoc_installed: Verify pandoc and download it when it is missing.
    """
    try:
        result = subprocess.run(
            ['pandoc', '--version'],
            capture_output=True,
            text=True,
            check=True
        )
        print(f"Found Pandoc: {result.stdout.split()[1]}")
        found = True
    except (subprocess.CalledProcessError, FileNotFoundError):
        found = False
    return found

check_pandoc_installed

check_pandoc_installed() -> bool

Ensure pandoc is callable from the command line.

If pandoc is already on PATH this returns immediately. Otherwise the pandoc binary bundled with pypandoc-binary is located and its directory is prepended to this process's PATH so the binary (and any subprocess spawned from this process) can invoke pandoc directly. No network download happens; the PATH change affects only the current process, not the parent shell.

Returns:

Type Description
bool

True if pandoc is accessible after the call, False if the bundled binary

bool

could not be located on PATH.

Examples:

  • Guard a conversion step on pandoc being available:
    >>> from ddocs.markdown.pandoc_utils import check_pandoc_installed
    >>> if check_pandoc_installed():  # doctest: +SKIP
    ...     print("converting")
    converting
    
  • Abort early when pandoc cannot be made available:
    >>> from ddocs.markdown.pandoc_utils import check_pandoc_installed
    >>> ready = check_pandoc_installed()  # doctest: +SKIP
    >>> ready or "could not install pandoc"  # doctest: +SKIP
    True
    
See Also

sanity_check: The PATH-only check this function builds on. check_pandoc_cli: Thin wrapper that maps the result to an exit code.

Source code in src\ddocs\markdown\pandoc_utils.py
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
def check_pandoc_installed() -> bool:
    """Ensure ``pandoc`` is callable from the command line.

    If pandoc is already on ``PATH`` this returns immediately. Otherwise the pandoc
    binary bundled with ``pypandoc-binary`` is located and its directory is prepended
    to this process's ``PATH`` so the binary (and any subprocess spawned from this
    process) can invoke ``pandoc`` directly. No network download happens; the ``PATH``
    change affects only the current process, not the parent shell.

    Returns:
        True if pandoc is accessible after the call, False if the bundled binary
        could not be located on ``PATH``.

    Examples:
        - Guard a conversion step on pandoc being available:
            ```python
            >>> from ddocs.markdown.pandoc_utils import check_pandoc_installed
            >>> if check_pandoc_installed():  # doctest: +SKIP
            ...     print("converting")
            converting

            ```
        - Abort early when pandoc cannot be made available:
            ```python
            >>> from ddocs.markdown.pandoc_utils import check_pandoc_installed
            >>> ready = check_pandoc_installed()  # doctest: +SKIP
            >>> ready or "could not install pandoc"  # doctest: +SKIP
            True

            ```

    See Also:
        sanity_check: The PATH-only check this function builds on.
        check_pandoc_cli: Thin wrapper that maps the result to an exit code.
    """
    installed = sanity_check()
    if not installed:
        pandoc_dir = _get_pandoc_dir()
        if pandoc_dir and os.path.exists(pandoc_dir):
            if pandoc_dir not in os.environ.get("PATH", "").split(os.pathsep):
                os.environ["PATH"] = pandoc_dir + os.pathsep + os.environ.get("PATH", "")
                print(f"Added pandoc to PATH: {pandoc_dir}")

            installed = sanity_check()
            if installed:
                print("Pandoc is now accessible!")

        if not installed:
            print("Warning: bundled Pandoc could not be located on the command line.")

    return installed

check_pandoc_cli

check_pandoc_cli(args: Namespace | None = None) -> int

CLI handler for the check-pandoc command.

Ensures pandoc is available (downloading it if necessary) and maps the result to a process exit code suitable for sys.exit.

Parameters:

Name Type Description Default
args Namespace | None

Parsed CLI arguments from argparse. Unused -- accepted only so the handler matches the calling convention of the other ddocs subcommand handlers. Defaults to None.

None

Returns:

Type Description
int

0 if pandoc is accessible, 1 otherwise.

Examples:

  • Run the check and use the result as a process exit code:
    >>> import sys
    >>> from ddocs.markdown.pandoc_utils import check_pandoc_cli
    >>> exit_code = check_pandoc_cli()  # doctest: +SKIP
    >>> exit_code  # doctest: +SKIP
    0
    
  • The argparse namespace is accepted but ignored:
    >>> import argparse
    >>> from ddocs.markdown.pandoc_utils import check_pandoc_cli
    >>> ns = argparse.Namespace(command="check-pandoc")
    >>> check_pandoc_cli(ns)  # doctest: +SKIP
    0
    
See Also

check_pandoc_installed: The underlying check-and-install routine.

Source code in src\ddocs\markdown\pandoc_utils.py
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
def check_pandoc_cli(args: argparse.Namespace | None = None) -> int:
    """CLI handler for the ``check-pandoc`` command.

    Ensures pandoc is available (downloading it if necessary) and maps the
    result to a process exit code suitable for ``sys.exit``.

    Args:
        args: Parsed CLI arguments from argparse. Unused -- accepted only so the
            handler matches the calling convention of the other ``ddocs``
            subcommand handlers. Defaults to None.

    Returns:
        0 if pandoc is accessible, 1 otherwise.

    Examples:
        - Run the check and use the result as a process exit code:
            ```python
            >>> import sys
            >>> from ddocs.markdown.pandoc_utils import check_pandoc_cli
            >>> exit_code = check_pandoc_cli()  # doctest: +SKIP
            >>> exit_code  # doctest: +SKIP
            0

            ```
        - The argparse namespace is accepted but ignored:
            ```python
            >>> import argparse
            >>> from ddocs.markdown.pandoc_utils import check_pandoc_cli
            >>> ns = argparse.Namespace(command="check-pandoc")
            >>> check_pandoc_cli(ns)  # doctest: +SKIP
            0

            ```

    See Also:
        check_pandoc_installed: The underlying check-and-install routine.
    """
    return 0 if check_pandoc_installed() else 1