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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4669689/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 06:06:14  来源:igfitidea点击:

How to use color in text with ReStructured Text (rst2html.py) or how to insert HTML tags without blank lines?

htmlrestructuredtextrst2html.py

提问by prosseek

How can I use color with ReStructured Text? For example, **hello**translates into <strong>hello</strong>. How can I make ReStructure(rst2html.py) translate somethinginto <font color="####">text</font>?

如何在 ReStructured Text 中使用颜色?例如,**hello**转换为<strong>hello</strong>. 如何让 ReStructure(rst2html.py) 将某些内容翻译 成<font color="####">text</font>

I thought about ..raw:: html, but it introduces blank lines. I want to insert HTML tags without blank lines.

我想过 ..raw:: html,但它引入了空行。我想插入没有空行的 HTML 标签。

回答by prosseek

I found this method working

我发现这个方法有效

First, you have the role.

首先,你有这个角色。

.. role:: red

An example of using :red:`interpreted text`

It translates into as follows.

它翻译如下。

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

Now, you have the red class, you can use CSS for changing colors.

现在,您有了 red 类,您可以使用 CSS 来更改颜色。

.red {
    color:red;
}

回答by RayLuo

Well, I am a new user now, therefore I can not comment on others answer, thanks to stackoverflow's policy here. https://meta.stackexchange.com/questions/51926/new-users-cant-ask-for-clarifications-except-as-answers

好吧,我现在是一个新用户,因此我不能评论其他人的答案,这要归功于这里的 stackoverflow 政策。https://meta.stackexchange.com/questions/51926/new-users-cant-ask-for-clarifications-except-as-answers

Sienkiew's answer is good, but I want to make correction about its last sentence.

Sienkiew 的回答很好,但我想对它的最后一句话进行更正。

There IS way to specify the style sheet in the RST file. The clue is in Prosseek's original post, that is the .. raw:: directive.

有一种方法可以在 RST 文件中指定样式表。线索在 Prosseek 的原始帖子中,即 .. raw:: 指令。

We can put following lines at the beginning of our RST file to specify its style.

我们可以在 RST 文件的开头放置以下几行来指定其样式。

.. raw:: html

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

回答by sienkiew

The other answer here hints at what I wanted to do, but it assumes some detailed knowledge about stylesheets in docutils. Here is a a cookbook explanation:

此处的另一个答案暗示了我想要做什么,但它假定您对 docutils 中的样式表有一些详细的了解。这是一本食谱解释:

In your RST file, declare the role once, then use it:

在您的 RST 文件中,声明一次角色,然后使用它:

    .. role:: red

    This text is :red:`colored red` and so is :red:`this`

Then you need a style sheet file. First, use Python to copy the default style sheet out of the docutils package:

然后你需要一个样式表文件。首先,使用 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")

Then edit my.css to add your customizations at the end:

然后编辑 my.css 以在最后添加您的自定义:

    .red {
            color: red;
    }

Create a docutils configuration file named "docutils.conf":

创建一个名为“docutils.conf”的 docutils 配置文件:

    [html4css1 writer]
    stylesheet-path: my.css
    embed-stylesheet: yes

use rst2html.py to convert your document:

使用 rst2html.py 转换您的文档:

    rst2html.py my_document.rst > my_document.html

If you don't want to use docutils.conf, you can specify the style sheet every time you run rst2html:

如果不想使用 docutils.conf,可以在每次运行 rst2html 时指定样式表:

    rst2html.py --stylesheet my.css my_document.rst > my_document.html

AFAIK, there is no way to specify the style sheet in the RST file.

AFAIK,无法在 RST 文件中指定样式表。

回答by Fernando

Works for me like this:

像这样对我有用:

.. raw:: html

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

.. role:: red

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

回答by ankostis

Sphinx already supports colorswith the s5defs.txtstandard definition fileintended for inclusion(but is missing the CSS file):

Sphinx已经支持用于包含标准定义文件的颜色(但缺少 CSS 文件):s5defs.txt

  1. Create/append this text to the value of rst_epilogsphinx configuration, in your docs/conf.pyfile:

    rst_prolog = """
    .. include:: <s5defs.txt>
    .. default-role::
    
    """
    
  2. Follow Sphinx's instructionsto add a css with the colors (e.g. adopt the hack.cssfrom @N?reen's answer):

    • Place your css file into e.g. _static/css/s4defs-roles.css;
    • append it's path into shtml_css_filessphinx configuration:

      html_css_files = [
          'css/s4defs-roles.css',
      ]
      
  1. rst_epilog在您的docs/conf.py文件中创建/将此文本附加到sphinx 配置的值:

    rst_prolog = """
    .. include:: <s5defs.txt>
    .. default-role::
    
    """
    
  2. 按照Sphinx 的说明添加带有颜色的 css(例如采用hack.css来自@N?reen 的回答):

    • 将您的 css 文件放入 eg _static/css/s4defs-roles.css;
    • 将它的路径附加到shtml_css_filessphinx 配置中:

      html_css_files = [
          'css/s4defs-roles.css',
      ]
      

You may then use:

然后您可以使用:

Some :red:`colored text` at last!

TIP:Read this SOif you also want the styling to appear in Latexoutput.

提示:如果您还希望样式出现在Latex输出中,请 阅读此 SO

回答by Krazy Glew

Combining @prosseek's and @RayLuo's answers all in one place - to make easier to find

将@prosseek 和@RayLuo 的答案全部合并到一个地方 - 更容易找到

At the top of your RST file, place

在 RST 文件的顶部,放置

.. raw:: html

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

.. role:: red

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

SIDE COMMENT:

旁注:

Of course, many folks will want the style in a separate file, as @sienkiew says.

当然,正如@sienkiew 所说,许多人希望将样式放在一个单独的文件中。

But not always.

但不总是。

E.g. I am generating the above from a script that I want other users to be able to run, often from a file URL. Depending on rst2html.py is bad enough - requiring something nonstandard to be in a config file is worse.

例如,我从我希望其他用户能够运行的脚本生成上述内容,通常是从文件 URL。依赖 rst2html.py 已经够糟糕的了——在配置文件中需要一些非标准的东西更糟。

If there were a way to create a weak local definition for the style - e.g. "if there is no style .red already defined use this, but otherwise use the style already defined" - would be nice. But AFAIK local definitions are stronger.

如果有一种方法可以为样式创建一个弱局部定义 - 例如“如果没有样式 .red 已经定义使用这个,否则使用已经定义的样式” - 会很好。但 AFAIK 本地定义更强。

This ran with rst2html.py (Docutils 0.13.1 [release], Python 3.6.4, on cygwin), but other RST tools rejected.

这与 一起运行rst2html.py (Docutils 0.13.1 [release], Python 3.6.4, on cygwin),但其他 RST 工具被拒绝。