2015-04-12 50 views
5

在python-sphinx中有only directive,它可以用来根据它的输出有条件地影响文档。例如文本只出现在html或latex中。如何通过在python-sphinx中使用角色来使用唯一的内联指令?

它使用这样的:

 
.. 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`.

我看到原始指令类似的东西。这对于唯一的指令也是可能的吗?我试过了:

 

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

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

但是这不起作用。

+2

不要混淆指令和角色。指令对文本块(整段)进行处理;角色是内联文本(段落内)。没有与“唯一”指令相对应的内置角色。您必须为此创建您自己的自定义角色。我无法提供任何详细的说明,但这里有一篇文章可以帮助您开始:http://doughellmann.com/2010/05/09/defining-custom-roles-in-sphinx.html。 – mzjn

+0

好的,谢谢。这是有道理的。因为我认为[原始](http://docutils.sourceforge.net/docs/ref/rst/directive.net.html#raw-data-pass-through)是一个指令,所以我感到困惑。但正如[在此]所述(http://docutils.sourceforge.net/docs/ref/rst/directives.html#custom-interpreted-text-roles),这似乎是“一种特殊情况”。 –

+0

我将我的评论转换为正确的答案。 – mzjn

回答

1

指令处理文本块(整段);角色是内联文本(段落内)。

您提到了raw,并且确实有一个directive和一个带有该名称的role,用于“原始数据传递”。

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