Metadata-Version: 2.4
Name: sphinx-exec-to-file
Version: 1.0.0
Summary: Sphinx extension to include images and data files created by commands or code
Author-email: Gatien Bouyer <gatien.bouyer.dev+sphinx@gmail.com>
License-Expression: MIT
Project-URL: Source, https://github.com/GatienBouyer/sphinx-exec-to-file
Keywords: sphinx,sphinx-extension,generation
Classifier: Development Status :: 5 - Production/Stable
Classifier: Framework :: Sphinx
Classifier: Framework :: Sphinx :: Extension
Classifier: Topic :: Documentation :: Sphinx
Classifier: Topic :: Software Development :: Documentation
Requires-Python: >=3.11
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: sphinx
Provides-Extra: dev
Requires-Dist: mypy; extra == "dev"
Requires-Dist: types-docutils; extra == "dev"
Requires-Dist: isort; extra == "dev"
Requires-Dist: autopep8; extra == "dev"
Requires-Dist: pylint; extra == "dev"
Requires-Dist: pytest; extra == "dev"
Requires-Dist: coverage; extra == "dev"
Dynamic: license-file

# Sphinx extension exec-to-file

This Sphinx extension aims to generate images or data to be included in Sphinx documents,
such as CSV files generated by a script calling an API.

> [!TIP]
> We recommend the usage of specialized directives when they exists.
> For example, use the `.. plot::` directive - included in matplotlib - to create graphes.


## Installation

We recommend the use of environment (a.k.a. "venv").
```sh
pip install sphinx-exec-to-file
```

Add the extension in your Sphinx configuration file `conf.py`:
```python
extensions = [
	"sphinxcontrib.exec_to_file",
]
```


## Presentation

This extension introduces 2 directives: `exec-code` and `exec-file`.

The `exec-code` directive allows to run a shell command or a code snippet.
The language is specified as argument of the directive.
To run the code as a shell command the language must be `sh`, which is the default.

The `exec-file` directive allows to run a file.
The path is given as argument of the directive, relative to the Sphinx build directory.
The language is specified as option of the directive.
Use python to execute a python script file.

The command/code/file is executed in the Sphinx build directory,
i.e. where the 'conf.py' file is located.

The files listed in the capture options `capture-file`, `capture-stdout`, and `capture-stderr`
are replaced in the executed command/code/file, so they can be generated at the correct location.
E.g., for the capture of `assets/file.csv`, the replacement search for `<file.csv>` in the text,
and write `sphinx-build-dir/assets/file.csv` instead, with the actual sphinx-build-dir,
so other directives can use the file.

The intended usage for captured files is to be used with other Sphinx directives like
`csv-table`, `image`, `literalinclude`.


## Usage example

### Execute a command
```rst
.. exec:code::
	:capture-file:
		assets/file.csv
		assets/second file.csv
	:hide-code:

	prog.exe <file.csv> <second file.csv>
```
The prog.exe program shall be accessible using the PATH environment variable,
or be in the Sphinx build directory, where the command is executed.

### Execute a code snippet
```rst
.. exec:code:: python
	:capture-stdout: assets/file1.txt
	:capture-stderr: assets/file2.txt

	import sys
	print("hello world")
	print("hello errors", file=sys.stderr)
```

### Execute a file
```rst
.. exec:file:: scripts/generate_image.py
	:capture-file: assets/file.png
	:language: python
```

### Show the generated table
```rst
.. csv-table::
	:file: assert/file.csv
```

### Show the generated text
```rst
.. literalinclude:: assets/file1.txt
```

### Show the generated image
```rst
.. image:: assets/file.png
```

### Show how it was generated
`exec:code` and `exec:file` inherit from
[code-block](https://www.sphinx-doc.org/en/master/usage/restructuredtext/directives.html#directive-code-block)
and [literalinclude](https://www.sphinx-doc.org/en/master/usage/restructuredtext/directives.html#directive-literalinclude).
You can display how the file was generated using their directive options.
If you don't want it in your output, use the option `:hide-code:`.


## Extension configuration

You can configure the extension with the following option in your `conf.py`.
The configuration values shown are the default ones.

### exec_code_runners
```python
exec_code_runners = {
    None: ["sh", "-c"],  # the default language
    "sh": ["sh", "-c"],
    "python": [executable, "-c"],
}
```

### exec_file_runners
```python
exec_file_runners = {
    None: ["sh"],  # the default language
    "sh": ["sh"],
    "python": [executable],
}
```

### exec_code_file_markers
```python
exec_code_file_markers = "<>"
```
To use other characters to identify the files to be captured,
instead of the default `<file.png>`.
E.g. "{}" would allow to use `{file.png}`.


## Directive options

The `exec:code` and `exec-file` directives supports the same options.

They also inherits the options of the
[code-block](https://www.sphinx-doc.org/en/master/usage/restructuredtext/directives.html#directive-code-block)
and [literalinclude](https://www.sphinx-doc.org/en/master/usage/restructuredtext/directives.html#directive-literalinclude) directives.

### capture-file
The capture-file identifies the list of files to replace in the code/command/file.

The option takes one file path per line.
They are replace in the executed text where the same file path is written
surrounded by the markers (as configured by exec_code_file_markers).

### capture-stdout
Similar to the `capture-file` option, but capture the standard output of the executed text.

### capture-stderr
Similar to the `capture-file` option, but capture the standard error output of the executed text.

It can have the same value as the `capture-stdout` option, to capture both outputs in the same file.
The apparition order of the outputs is preserved in the captured file.

### ignore-exit
By default, the executed code shall return the exit-code status of 0.
This option disable this check.

### timeout
By default, the executed code can run indefinitely.
This option set a timer on the execution of the directive.

### hide-code
By default, the code/command/file is shown using Sphinx's `code-block` and `literalinclude` directives.
THis option disable this, so the directive shows nothing in the built documentation.


## Development

Install necessary tools with `pip install -e .[dev]`.

Run tests with `pytest` or `coverage`. Run code analysis with `autopep8`, `isort`, `mypy` and `pylint`.
These tools are pre-configured, no options are required to run them,
you just have to give the path to analyse.

You can trigger the run of all these tools with `precommit`:

```sh
pip install pre-commit && pre-commit install
```

To build and distribute the package use:
```sh
pip install build twine
python -m build
twine upload dist/*
```
