我正在改进一些技术文档(Sphinx),并希望将RST文件的内容包含在多个位置;但是,这样做是将RST文件多次添加到TOC中。如何在所需的位置包含文件,但只在目录中引用该部分一次?
这是index.rst文件的外观:
Documentation
=================
.. toctree::
:maxdepth: 4
../path/to/releasenotes.rst
../path/to/general_api_usage.rst
main_content.rst
接下来,这是main_content.rst:
============
Main Content
============
These docs reference:
- :ref:`section-1`.
- :ref:`section-2`.
- :ref:`section-3`.
- :ref:`section-4`.
.. include:: section1.rst
.. include:: section2.rst
.. include:: section3.rst
.. include:: section4.rst
“第4节”是参考表;例如,我想将其包含在“第2节”中,但也要像附录一样将其保留在文档的底部。
这是section2.rst的样子:
.. _section-2:
This is Section 2
********************
.. include: section4.rst
Some other Section 2 content.
最后,section4.rst可能看起来像:
.. _section-4:
This is Section 4
********************
+------------------+-------------------------+
| Heading | Heading 2 |
+==================+=========================+
| This is what I | want to reference |
+------------------+-------------------------+
| in other rst | files. |
+------------------+-------------------------+
[执行此操作时,我的目录两次包含“第4节”。有什么见解吗?谢谢!
.. _section-4:
This is Section 4
********************
我将其添加到新文件appendix.rst的顶部,并添加了对section4.rst的引用:
.. _section-4:
This is Section 4
********************
.. include:: section4.rst
然后我修改了main_content.rst,使其引用了appendix.rst,而不是section4.rst。
我的主要学习点:TOC反映了引用文件中的标题。