如何使用ReStructured Text(rst2html.py)在文本中使用颜色或如何插入没有空行的HTML标签?

如何在ReStructured Text中使用颜色?例如,
**hello**
转换为
<strong>hello</strong>
。如何让ReStructure(rst2html.py)将某些内容翻译成
<font color="####">text</font>
? 我想过..raw :: html,但它引入了空白行。我想插入没有空行的HTML标签。     
已邀请:
我发现这种方法有效 首先,你有这个角色。
.. 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;
}
    
好吧,我现在是新用户,因此我无法评论其他人的答案,这要归功于stackoverflow的政策。 https://meta.stackexchange.com/questions/51926/new-users-cant-ask-for-clarifications-except-as-answers Sienkiew的答案很好,但我想纠正它的最后一句话。 有办法在RST文件中指定样式表。线索在Prosseek的原始帖子中,即.. raw ::指令。 我们可以在RST文件的开头添加以下行来指定其样式。
.. raw:: html

    <style> .red {color:red} </style>
    
这里的另一个答案暗示了我想要做的事情,但它假定了一些关于docutils中样式表的详细知识。这是一本食谱说明: 在您的RST文件中,声明角色一次,然后使用它:
    .. role:: red

    This text is :red:`colored red` and so is :red:`this`
然后你需要一个样式表文件。首先,使用Python从docutils包中复制默认样式表:
    python
    import os.path
    import shutil
    import docutils.writers.html4css1 as h
    shutil.copy(os.path.dirname(h.__file__)+"/html4css1.css","my.css")
然后编辑my.css以在最后添加自定义:
    .red {
            color: red;
    }
创建名为“docutils.conf”的docutils配置文件:
    [html4css1 writer]
    stylesheet-path: my.css
    embed-stylesheet: yes
使用rst2html.py转换您的文档:
    rst2html.py my_document.rst > my_document.html
如果您不想使用docutils.conf,则可以在每次运行rst2html时指定样式表:
    rst2html.py --stylesheet my.css my_document.rst > my_document.html
AFAIK,无法在RST文件中指定样式表。     
将@ prosseek和@ RayLuo的答案结合在一起 - 更容易找到 在RST文件的顶部,放置
.. raw:: html

    <style> .red {color:red} </style>

.. role:: red

:red:`test - this text should be red`
侧面评论: 当然,正如@sienkiew所说,许多人都希望将这种风格放在一个单独的文件中。 但不总是。 例如。我正在从我希望其他用户能够运行的脚本生成上述内容,通常来自文件URL。取决于rst2html.py是不够的 - 需要非标准的东西在配置文件中更糟糕。 如果有办法为样式创建一个弱的本地定义 - 例如“如果没有样式.red已定义使用此,但否则使用已定义的样式” - 会很好。但AFAIK本地定义更强。 这与
rst2html.py (Docutils 0.13.1 [release], Python 3.6.4, on cygwin)
一起运行,但其他RST工具被拒绝。     
这样对我有用:
.. raw:: html

    <style> .red {color:#aa0060; font-weight:bold; font-size:16px} </style>

.. role:: red

:red:`test - this text should be red``
    

要回复问题请先登录注册