python-sphinx - 可以使用 `ifconfig` 块 (reStructuredText) 有条件地定义目标
问题描述
是否可以使用ifconfig有条件地在 reStructuredText 文件中定义目标?
我在我的 sphinxconf.py
文件中设置了一个变量,我想用它来有条件地确定文档中目标的 URI:
def setup(app):
argv = ' '.join(sys.argv)
if '-b html' in argv:
app.add_config_value('buildername', 'html', 'env')
else:
app.add_config_value('buildername', 'not-html', 'env')
我的index.rst
文件有以下内容:
test link to target1_
.. ifconfig:: buildername == 'html'
.. _target1: https://example.com/a
上述工作按预期工作,“target1”成为一个超链接example.com/a
但是,如果我想target1
根据配置变量的值实际定义为有条件地设置为两个选项之一buildername
,那么我有
test link to target1_
.. ifconfig:: buildername == 'html'
.. _target1: https://example.com/a
.. ifconfig:: buildername != 'html'
.. _target1: https://example.com/b
不仅上述输出不适用于example.com/b
,而且它破坏了第一个链接,target1
现在指向任何内容(实际上#id3
)。
此外,我在sphinx-build
输出中收到以下警告
user@host:~$ make clean && sphinx-build -b html . _build/html/
...
reading sources... [100%] support
.../index.rst:16: WARNING: Duplicate explicit target name: "target1".
.../index.rst:8: WARNING: Duplicate target name, cannot be used as a unique reference: "target1".
...
是否可以在一个.rst
文件中定义相同的目标两次,这样每个定义都包含在一个ifconfig
指令中?
解决方案
不幸的是,我不认为你想要什么是可能的。您可以抑制警告(这可能不是一个好主意),或者您可以添加更多标记以通过目标和链接之间的一对一关系来避免它们。
.. ifconfig:: buildername == 'html'
test link to target1_
.. ifconfig:: buildername == 'html'
.. _target1: https://example.com/a
.. ifconfig:: buildername != 'html'
test link to target2_
.. ifconfig:: buildername != 'html'
.. _target2: https://example.com/b
推荐阅读
- matlab - 如何使用源文件名自动保存绘制的图形?
- python - 字符串到列表的转换 - 分隔符问题
- apache-kafka - 以下订阅的主题未分配给任何成员
- angular - Storybook 在角度库中给出“无法解析 AppComponent 的所有参数”错误
- phpunit - 运行单个测试方法时,PhpStorm 无法获取文件夹的配置
- php - WooCommerce ajax 获取订单状态并重定向到 url
- javascript - React 函数高阶组件
- python - 如何加快熊猫 drop() 方法?
- magento - 如何解决未定义索引的问题:在 magento 2.3.1 中自定义导入期间的 sku
- python - 二进制编码或填充神经网络中的输入序列?