Sphinx docs / RST include file from dynamic path?

心已入冬 提交于 2019-12-12 19:03:53

问题


I'm wondering if it's possible to use a dynamic path in a Sphinx and/or RST ..include:: directive?

My reason for this is I have some developer documentation generated by Sphinx in one repo, but we have a bunch of unit tests in another repo that we want to include in the docs. If I know the path to the file in the other repo, it's pretty standard, like this:

Some text in my RST file

.. include:: ../path/to/other/repo/file.py
:code: python

Some more text

The problem is the relative path to the other repo is not always the same, depending on how things are cloned and installed. For example, on Read The Docs, the other repo is installed in editable mode via requirements.txt into a /src subfolder, locally the repo is in a git folder, etc.

I can add logic into the conf.py file to find the other repo and set a pointer which I can use in the RST files, but I can't figure out if it's possible to have a dynamic path in the ..include::?

So far the only workaround I can think of is to have my conf.py find the other repo and create a symlink which I reference in the RST files, which is fine, but I wonder if there's a better way?


回答1:


After playing with this more for a bit, I decided that creating a soft link is the way to go. (At first I was going to use a hard link, but creating that fails on Read The Docs. Soft link works on RTD and in Sphinx.)

So I have code in conf.py that walks the folder structure to find the other repo I need to include files from, then it creates the link (after first removing the old one and checking the version of the repo it found to make sure it's the right one). Then I just do the ..include:: with the soft link and everything is fine.

So overall not a bad solution, and most importantly it works locally and on RTD, and it works regardless of the location of the other repo.



来源:https://stackoverflow.com/questions/45095529/sphinx-docs-rst-include-file-from-dynamic-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!