如何在python-sphinx中使用角色来使用唯一的内联指令?

时间:2015-04-12 12:39:11

标签: inline python-sphinx restructuredtext role

在python-sphinx中有only directive,可用于根据输出有条件地影响文档。例如,文本仅出现在html或latex中。

这样使用:

.. only:: not latex

   Here there is some Text, that does not appear in latex output.

.. only:: html

   Here there is some Text, that only appears in html output.

如何以正确的方式使用角色指令来使用唯一类内联,让我们这样说:

Here there is some Text, that :only-notlatex:`does not appear in latex`
:only-html:`only appears in html`.

我看到了raw指令的类似内容。这也是唯一的指令吗?我试过了:


.. role:: only-html(only)
   :format: html

.. role:: only-notlatex(only)
   :format: not latex 

但这不起作用。

1 个答案:

答案 0 :(得分:2)

指令适用于文本块(整段);角色用于内联文本(在段落中)。

您提到raw,确实有directiverole这个名称,用于"原始数据传递"。

但是没有内置角色是only指令的内联等价物。您必须为此目的创建自己的自定义角色。我无法提供任何详细说明,但这篇文章可以帮助您入门:http://doughellmann.com/2010/05/09/defining-custom-roles-in-sphinx.html