【问题标题】:Sphinx/RST: Include another RST file without adding it to the TOC?Sphinx/RST:包含另一个 RST 文件而不将其添加到 TOC?
【发布时间】:2020-03-18 22:17:04
【问题描述】:

我正在改进一些技术文档 (Sphinx),并希望在多个位置包含 RST 文件的内容;但是,这样做会多次将该 RST 文件添加到 TOC。如何将文件包含在我想要的位置,但只在 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 节”。有什么见解吗?谢谢!

【问题讨论】:

  • 是否可以从文档section4.rst 中删除部分标题,并在需要时将其移动到包含的文档中?还是实际上有比示例中显示的更多的标题?
  • 谢谢@sinoroc。这真的很有帮助。感谢您的建议,我完成了我希望做的事情。

标签: python-sphinx restructuredtext sections toctree


【解决方案1】:

感谢@sinoroc 的建议,我想出了一个解决方案:

我从 section4.rst 中删除了以下内容,只保留了表格:

 .. _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 反映了引用文件中的标题。

【讨论】:

  • 您会将此标记为答案,以便其他人可以看到它已被回答吗?谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-20
相关资源
最近更新 更多