如何配置 Sphinx 有条件地排除某些页面?

发布于 2024-12-18 20:29:51 字数 149 浏览 1 评论 0原文

使用 Sphinx 生成文档时,我希望能够生成文档的两个版本:一个包含所有内容,另一个仅包含一组特定的页面。实现这一目标的最佳方法是什么?

我可以编写一个构建脚本来移动文件来实现此目的,但如果有一种方法告诉 sphinx 在特定构建期间排除或包含特定文档,那就太好了。

When generating documentation using Sphinx, I would like to be able to generate two versions of my documentation: one including everything, and one with only a particular set of pages. What's the best way of achieving that?

I could write a build script that moves files around to achieve this but it would be really nice if there was a way to tell sphinx to exclude or include particular documents during a particular build.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

再见回来 2024-12-25 20:29:51

也许我的答案来得有点晚了,但我设法通过 排除中的模式来使用 Sphinx 来做到这一点配置文件

我的文档部分供用户使用,部分供管理员使用。
有些页面的文件名包含单词 admin ,和你一样,我想构建两个版本:一个包含所有内容(管理文档),另一个包含所有“管理”页面(用户文档) )。

要排除所有子文件夹中的所有“管理”页面,您必须将此行添加到配置文件 conf.py 中:

exclude_patterns = ['**/*admin*']

这是简单的部分。

我的问题是,我不知道如何在不使用两个不同的配置文件的情况下运行构建两次,一次使用排除模式,一次不使用排除模式。

我自己没有找到解决方案,所以我在这里问了一个问题并得到了答案

  • 配置文件只是一个Python文件,可以包含Python代码,它将在构建时执行。
  • 您可以通过命令行传递参数(“标签”)可以在配置文件中查询。

所以我的配置文件中有这个排除模式:

exclude_patterns = ['**/*admin*']
if tags.has('adminmode'):
    exclude_patterns = []

现在我可以运行构建而不传递任何内容,这将排除“admin”文件:

make clean
make html

⇒这是我的用户文档

......我可以设置“adminmode”标签,它不会排除任何东西:
(Windows 命令行语法)

set SPHINXOPTS=-t adminmode
make clean
make html

⇒ 这是我的管理文档。


奖励:

我可以使用相同的标签来忽略页面上的某些特定内容,通过包含基于标签的内容

例子:

regular documentation
=====================

This paragraph and its headline will always be visible.

.. only:: adminmode

        secret admin stuff
        ------------------

        This paragraph will be visible in the admin docs only.


This will (again) always be visible.

Maybe my answer comes a bit late, but I managed to do this with Sphinx via exclude patterns in the config file.

My documentation is partly for users and partly for admins.
Some pages have file names that contain the word admin, and like you, I wanted to build two versions: one with everything (the admin docs) and one with all "admin" pages excluded (the user docs).

To exclude all "admin" pages in all subfolders, you have to add this line to the config file conf.py:

exclude_patterns = ['**/*admin*']

That was the easy part.

My problem was that I didn't know how to run the build two times, one with and one without the exclude patterns without using two different config files.

I didn't find a solution by myself, so I asked a question here on SO and got an answer:

  • The config file is just a Python file and can contain Python code, which will be executed on build.
  • You can pass parameters ("tags") via the command line which can be queried in the config file.

So I have this exclude pattern in my config file:

exclude_patterns = ['**/*admin*']
if tags.has('adminmode'):
    exclude_patterns = []

Now I can run the build without passing anything, which will exclude the "admin" files:

make clean
make html

⇒ this is my user documentation

...and I can set the "adminmode" tag, which will not exclude anything:
(Windows command line syntax)

set SPHINXOPTS=-t adminmode
make clean
make html

⇒ this is my admin documentation.


Bonus:

I can use the same tag to ignore some specific content on a page, by Including content based on tags.

Example:

regular documentation
=====================

This paragraph and its headline will always be visible.

.. only:: adminmode

        secret admin stuff
        ------------------

        This paragraph will be visible in the admin docs only.


This will (again) always be visible.
ζ澈沫 2024-12-25 20:29:51

< /a> 和 ifconfig 指令可用于在页面内应用条件。

似乎没有任何简单的方法可以使用条件来完全排除整个页面(.rst 文件)。

以下内容(在index.rst中)在生成HTML输出时排除了对index.html中toctree中doc2.html的引用:

.. toctree::
   doc1.rst

.. only:: latex

   .. toctree::
      doc2.rst

但这实际上不起作用。 doc2.html 文件仍然会生成,并且当 doc1.html 是当前主题时,可以通过“下一个主题”链接访问该文件。

The only and ifconfig directives can be used to apply conditions within pages.

There does not seem to be any simple way to use conditions to completely exclude entire pages (.rst files).

The following (in index.rst) excludes the reference to doc2.html in the toctree in index.html when generating HTML output:

.. toctree::
   doc1.rst

.. only:: latex

   .. toctree::
      doc2.rst

But this does not really work. The doc2.html file is still generated, and it is reachable via the "Next topic" link when doc1.html is the current topic.

茶色山野 2024-12-25 20:29:51

sphinx.ext.ifconfig 怎么样?您可以在 conf.py 文件中设置配置值。由于这是一个常规的 Python 文件,因此如果需要,您可以使包含标准变得智能且自动。

How about sphinx.ext.ifconfig? You set config values in your conf.py file. As that is a regular Python file, you can make your inclusion criteria smart and automatic if you need to.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文