[英]Include multiple RST files with Sphinx with directive .. include::
In my Sphinx project, I want to have a include folder with multiple RST files that I can reuse in other projects.在我的 Sphinx 项目中,我想要一个包含多个 RST 文件的包含文件夹,我可以在其他项目中重复使用这些文件。 My source folder looks something like:我的源文件夹如下所示:
\source
\include
links.rst # Here I have useful external links
roles.rst # Here I define custom roles
subs.rst # Here I definne common substitutions (replace directive)
... rest of my stuff
conf.py
Basically, I want to be able to write a single .. include::
in my source RST files that will account for all my files, ie the equivalent of /include/*.rst
基本上,我希望能够在我的源 RST 文件中编写一个.. include::
来解释我的所有文件,即相当于/include/*.rst
I have come up with a neat solution that I post below since it might be usefult to someone else.我想出了一个巧妙的解决方案,我在下面发布,因为它可能对其他人有用。 However, it would be nice to hear other alternatives, since my solution comes with a problem of infinite loop when using sphinx-autobuild
.但是,很高兴听到其他替代方案,因为我的解决方案在使用sphinx-autobuild
时会出现无限循环问题。
My solution consists of modifying conf.py
to include this small piece of code:我的解决方案包括修改conf.py
以包含这一小段代码:
conf.py
import os
# Code to generate include.rst
files = os.listdir('include')
with open('include.rst', 'w') as file:
for rst in files:
file.write('.. include:: /include/' + rst + '\n')
This will create a new include.rst
file in the root source directory, which will look as:这将在根源目录中创建一个新的include.rst
文件,如下所示:
\source
\include
links.rst # Here I have useful external links
roles.rst # Here I define custom roles
subs.rst # Here I definne common substitutions (replace directive)
... rest of my stuff
conf.py
include.rst
With the new file include.rst
looking like:新文件include.rst
看起来像:
.. include:: /include/links.rst
.. include:: /include/roles.rst
.. include:: /include/subs.rst
Finally, in my source files I only need to add on top of the file the line最后,在我的源文件中,我只需要在文件顶部添加行
.. include:: include.rst
to benefit from all my custom links, roles, and substitutions (or anything else you might want there).从我的所有自定义链接、角色和替换(或您可能想要的任何其他内容)中受益。
PROBLEM: My solution here presents a problem.问题:我在这里的解决方案提出了一个问题。 Since I use sphinx-autobuild
to automatically build the html output whenever a change is detected, an infinite loop is produced, since each time the execution of conf.py
is creating the file include.rst
.由于我使用sphinx-autobuild
自动构建 html output 每当检测到更改时,就会产生无限循环,因为每次执行conf.py
都会创建文件include.rst
。 Any ideas on how to solve this?关于如何解决这个问题的任何想法?
UPDATE: I have found the solution to the problem mentioned above, and which was pretty obvious, actually.更新:我已经找到了上述问题的解决方案,实际上这很明显。 Now I execute sphinx-autobuild
with the --re-ignore
option as:现在我使用--re-ignore
选项执行sphinx-autobuild
:
> sphinx-autobuild source build/html --re-ignore include.rst
and the loop stops happening.并且循环停止发生。 Now, this is OK if I change the children rst files (ie roles, links, or subs) but if the very include.rst
changes (eg a new children rst file was added) then I need to stop and re-run sphinx-autobuild
.现在,如果我更改子 rst 文件(即角色、链接或子文件),这没问题,但如果include.rst
发生更改(例如添加了新的子 rst 文件),那么我需要停止并重新运行sphinx-autobuild
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.