可以使用 `ifconfig` 块(reStructuredText)有条件地定义目标 [英] Possible to define a target conditonally using `ifconfig` block (reStructuredText)
问题描述
是否可以有条件地定义目标 在 reStructuredText 文件中使用 ifconfig?>
我在 sphinx conf.py
文件中设置了一个变量,我想用它来有条件地确定文档中目标的 URI:
def setup(app):argv = ' '.join(sys.argv)如果 '-b html' 在 argv 中:app.add_config_value('buildername', 'html', 'env')别的: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
但是,如果我想根据 buildername
配置变量的值将 target1
实际定义为有条件地设置为两个选项之一,那么我有
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/...阅读资源... [100%] 支持.../index.rst:16: 警告:重复的显式目标名称:target1"..../index.rst:8: 警告:目标名称重复,不能用作唯一引用:target1"....
是否可以在 .rst
文件中定义相同的目标两次,这样每个定义都包含在 ifconfig
指令中?
不幸的是,我不认为你想要的是可能的.您可以取消警告(这可能不是一个好主意),或者您可以通过目标和链接之间的一对一关系添加更多标记来避免它们.
.. ifconfig:: buildername == 'html'测试链接到 target1_.. ifconfig:: buildername == 'html'.. _target1: https://example.com/a.. ifconfig:: buildername != 'html'测试链接到 target2_.. ifconfig:: buildername != 'html'.. _target2: https://example.com/b
Is it possible to condtionally define a target in a reStructuredText file using ifconfig?
I've set a variable in my sphinx conf.py
file that I want to use to conditionally determine the URI of a target in my documentation:
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')
And my index.rst
file has the following content:
test link to target1_
.. ifconfig:: buildername == 'html'
.. _target1: https://example.com/a
The above works as expected, and "target1" becomes a hyperlink to example.com/a
But if I want to actually define target1
to be conditionally set to one of two options based on the value of the buildername
config var, then I have
test link to target1_
.. ifconfig:: buildername == 'html'
.. _target1: https://example.com/a
.. ifconfig:: buildername != 'html'
.. _target1: https://example.com/b
Not only does the above output not work for example.com/b
, but it breaks the first link and target1
now points to nothing (actually #id3
).
Moreover, I get the following warnings in my sphinx-build
output
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".
...
Is it possible to define the same target twice within a .rst
file, such that each definition is wrapped in an ifconfig
directive?
Unfortunately, I do not think what you want is possible. You could suppress warnings (which is probably not a good idea), or you could add more markup to avoid them through a one-to-one relationship between target and link.
.. 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
这篇关于可以使用 `ifconfig` 块(reStructuredText)有条件地定义目标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!