Search code examples
python-sphinxrestructuredtext

Sphinx inline include


I want to use the .. include:: function inline, but I can only get it to actually include the file I want if I separate it with two new lines from the previous text.

Before anyone asks, the file I want to include is a protocol number, so no, it doesn't benefit from a new line, at all. I want to be able to change it easily so I can use it on multiple places of my documentation. I guess that an example would be "We currently use the protocol (proto.txt)." I'm new to Sphinx and rst, so maybe there is a very obvious solution I haven't found.


Solution

  • Inline includes are not possible with Sphinx.

    However, you can define global aliases (substitutions) in the rst_epilog variable of your build configuration file.

    For example, you can add the following lines to your conf.pyfile:

    rst_epilog = """
      .. |version| replace:: 4.1
      .. |protocol| replace:: httpx
    """
    

    Now, you can access the variables |version| and |protocol| from any .rst file within your project, for example like this:

    Version |version| uses the |protocol| protocol.

    becomes

    Version 4.1 uses the httpx protocol.

    If other parts of your software require protocol (or other variables) to be specified in a specific file or format, you can write a script to read it from there as a variable into the Sphinx configuration file.