【问题标题】:Get current_version in rst file of readthedocs theme在 readthedocs 主题的 rst 文件中获取 current_version
【发布时间】:2022-06-30 05:17:19
【问题描述】:

在 readthedocs / Sphinx 项目中,我需要在 rst 文件中显示 current_version 并生成与 current_version 的链接。

截图:

我在模板中看到过 (versions.html):

v: {{ current_version }}

是否可以在 rst 中访问 {{ current_version }} ?我试过了

|current_version|

但结果是

WARNING: Undefined substitution referenced: "current_version".

精度:

它适用于在conf.py 中声明的|version|。但我不知道如何显示current_version

编辑 2:

对不起,我的意思不清楚。在一个 readthedocs 项目中,我有几个文档分支。在我的第一个文件中,我需要知道构建特殊链接http://xxx.xx/**branch**/ 的分支,因为我必须将数据存储在另一个存储库中。在conf.py 中有版本字段,但是我想自动获取分支。 我看到在 RTD 模板中,在文件version.html 中,有{{ current_version }} 显示在屏幕截图中。

【问题讨论】:

  • thx,它回答了部分问题:如何在 rst 中使用变量。但它不适用于 current_version
  • 您对您提到的current_version 有任何参考吗?因为它既不在 Sphinx 也不在 RTD 文档中!
  • 我放了一张截图。我没有找到文档。它位于 version.html 中的 RTD 主题中。
  • 如果您无法提供源代码链接,我们无法知道 current_version 是什么,因为它未包含在任何官方文档中,请编辑问题以包含"Minimal, Reproducible, Example."
  • @CyrilJ 干得好编辑帖子,我投票重新打开。

标签: jinja2 python-sphinx restructuredtext read-the-docs


【解决方案1】:

阅读文档在构建时注入额外的html_context(请参阅https://github.com/readthedocs/readthedocs.org/blob/725e21813313ce67ca0048d72fcfdce019ee8182/readthedocs/doc_builder/templates/doc_builder/conf.py.tmpl#L75-L125)。该额外上下文包含您正在寻找的 current_version 变量。

这个上下文被传递给模板引擎。所以,从jinja2模板引擎,你可以使用{{ current_version }}

但是,请注意这在本地不起作用,因为您本地构建的 Sphinx 不会注入这个额外的上下文。如果您想模拟 Read the Docs 文档以在本地进行测试,您可以将以下内容添加到您的 conf.py

html_context = {
 'current_version': 'my-current-version',
}

并在本地构建文档以检查其行为。

另外,请查看阅读文档官方文档中的此常见问题解答条目:https://docs.readthedocs.io/en/stable/faq.html#how-do-i-change-behavior-when-building-with-read-the-docs

【讨论】:

    猜你喜欢
    • 2018-06-18
    • 2021-08-05
    • 1970-01-01
    • 2021-07-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-14
    • 1970-01-01
    • 2023-03-22
    相关资源
    最近更新 更多