问题 在Sphinx中为文本块应用CSS和角色而不是内联跨度


上一个问题 这解释了如何为某些reStructuredText添加颜色范围。

回顾一下这个程序:

首先,你有这个角色。

.. role:: red

An example of using :red:`interpreted text`

它转化为如下。

<p>An example of using <span class="red">interpreted text</span></p>

现在,你有了红色类,你可以使用CSS来改变颜色。

.red {
    color:red;
}

如果您想要跨越多行的文本,您如何做到这一点?例如:

.. role:: red

:red:`paragraph 1

      paragraph 2

      paragraph 3`

第1,2和3段都是“红色”。如果我尝试这样做,我会收到警告消息:

警告:内联解释的文本或短语引用start-string没有end-string。

它不会创建跨度并在文本中插入“:red:”。它只是不将其解释为字符串(如警告所示)。

基本上,这可以在reStructuredText中完成,如果可以,怎么做?

我正在使用Sphinx 1.1.3。


10109
2017-10-15 17:10


起源



答案:


有很多方法可以做到这一点,但其中一种方法是使用 class 指示:

.. class:: red

    This is a paragraph.

    This is another paragraph.

大多数docutils HTML编写者会将其作为类html属性放入html输出中,然后您可以使用CSS设置样式。

但是,在Sphinx中,您可能需要使用 rst-class 代替 class 至少在某些情况下。看到: http://sphinx.pocoo.org/rest.html#id3

此外,RestructuredText中的许多块级元素都需要 :class: 参数几乎完全相同。


10
2017-10-23 20:58



你是正确的使用 rst-class 在斯芬克斯。我不得不用它来使它工作,但它现在像我想要的那样工作。谢谢。 - jmq


答案:


有很多方法可以做到这一点,但其中一种方法是使用 class 指示:

.. class:: red

    This is a paragraph.

    This is another paragraph.

大多数docutils HTML编写者会将其作为类html属性放入html输出中,然后您可以使用CSS设置样式。

但是,在Sphinx中,您可能需要使用 rst-class 代替 class 至少在某些情况下。看到: http://sphinx.pocoo.org/rest.html#id3

此外,RestructuredText中的许多块级元素都需要 :class: 参数几乎完全相同。


10
2017-10-23 20:58



你是正确的使用 rst-class 在斯芬克斯。我不得不用它来使它工作,但它现在像我想要的那样工作。谢谢。 - jmq