sphinx,restructedtext:为单个单词设置颜色

发布于 2024-09-18 13:30:55 字数 138 浏览 3 评论 0 原文

有没有办法在 sphinx 中设置单个单词(或字符)的颜色?我很确定应该有一些标记标签,例如 HTML 的 font 标签。

Is there a way to set the color of single words (or characters) in sphinx? I'm pretty sure there should be some markup tag, like HTML's font tag.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

甜中书 2024-09-25 13:30:55

在我的 Sphinx 支持的 网站 上,我使用了以下组合:

  • 包含角色定义的重构文本文件,其中一个用于每种颜色 - 请参阅 .special.rst (BitBucket 链接)
  • 包含每个角色的颜色规则的 CSS 文件 - 请参阅 hacks.css (BitBucket 链接)

然后,在每个需要颜色的 rST 文件中,我首先在顶部手动导入 .special.rst

.. include:: .special.rst

或者使用 Sphinx 的 conf.py 文件中的 rst_epilog 配置变量:

rst_epilog = "\n.. include:: .special.rst\n"

然后每个角色都可以在纯 rST 语法中轻松使用:

This is :red:`red !` And :blue:`this part is blue`.

更多详细信息参见 此页面(法语,抱歉)。

它非常适合 html 输出(和类似 html),但不适用于 PDF。请参阅上面的第一个答案以生成带颜色的 PDF。

On my Sphinx-powered website, I use a combination of:

  • A restructuredText file containing roles definitions, one for each color - see .special.rst (BitBucket link)
  • A CSS file containing color rules for each role - see the first lines of hacks.css (BitBucket link)

Then, in every rST file where I need colors, I first import .special.rst at the top, either manually:

.. include:: .special.rst

Or with the rst_epilog configuration variable in Sphinx's conf.py file:

rst_epilog = "\n.. include:: .special.rst\n"

And then each role can be used easily in pure rST syntax:

This is :red:`red !` And :blue:`this part is blue`.

More details are given on this page (in French, sorry).

It works perfectly well for html output (and html-like), but not for PDF. Refer to the first answer above for producing a PDF with colors.

疯狂的代价 2024-09-25 13:30:55

Sphinx 已经通过 支持颜色 s5defs.txt 标准定义文件< /em> 用于 包含(但缺少 CSS 文件):

  1. 创建/附加此文本到 rst_prolog
    sphinx 配置,在您的 docs/conf.py 文件中:

    <前><代码> rst_prolog = """
    .. 包括::

    ”“”

  2. 按照Sphinx 的说明 添加带有颜色的 css
    (例如采用 hack.css来自@Næreen的回答):

  • 将你的css文件放入例如_static/css中/s4defs-roles.css;

  • 将其路径附加到 shtml_css_files< /code> sphinx 配置:

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

然后您可以使用:

Some :red:`colored text` at last!

提示:
阅读这个如果您还希望样式出现在 Latex 输出中。

Sphinx already supports colors with the s5defs.txt standard definition file intended for inclusion (but is missing the CSS file):

  1. Create/append this text to the value of rst_prolog
    sphinx configuration, in your docs/conf.py file:

     rst_prolog = """
     .. include:: <s5defs.txt>
    
     """
    
  2. Follow Sphinx's instructions to add a css with the colors
    (e.g. adopt the hack.css from @Næreen's answer):

  • Place your css file into e.g. _static/css/s4defs-roles.css;

  • append it's path into shtml_css_files sphinx configuration:

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

You may then use:

Some :red:`colored text` at last!

TIP:
Read this SO if you also want the styling to appear in Latex output.

偏闹i 2024-09-25 13:30:55

如果您想在不依赖 html 的情况下执行此操作,请尝试对您的单词应用与普通正文文本不同的样式。

在这个改编自 rst2pdf 手册的示例中,我应用了现有的 rubric 样式,该样式在我正在使用的后端中为红色:

Before red.

.. role:: rubric

I like color :rubric:`rubric`.

After red.

单词的实际外观将取决于您选择的样式在生成时使用的样式表中的定义方式你的文件。
如果您想要蓝色文本,请创建蓝色文本样式并从普通文本样式派生它。
样式表是特定于后端的,您可能使用默认值。
要打印 rst2pdf.py 的默认值,请执行以下操作(来自 rst2pdf 手册):

rst2pdf --print-stylesheet

继续 rst2pdf 样式表的示例,将其添加到样式表中以获得蓝色文本样式:

bluetext:
  parent: bodytext
  textColor: blue

在文档中,您可以引用此样式以获得蓝色单词。
请注意,此位是通用的,如果您在 html 或任何后端样式表中定义蓝色样式,则应生成蓝色文本。

Before blue.

.. role:: bluetext

I like color :bluetext:`blue`.

After blue.

生成的 pdf 包含彩色单词:
在此处输入图像描述

If you want to do this without being tied to html, try applying a different style than normal body text to your word.

In this example adapted from the rst2pdf manual, I apply the existing rubric style which is red in the backend that I am using:

Before red.

.. role:: rubric

I like color :rubric:`rubric`.

After red.

The actual look of the word will depend on how the style you choose is defined in the stylesheet that you use when generating your document.
If you want blue text, make a blue text style and derive it from the normal text style.
The stylsheet is backend-specific and you may be using the default.
To print the default for rst2pdf.py, do this (from the rst2pdf manual):

rst2pdf --print-stylesheet

Continuing the example for a rst2pdf stylesheet, add this to your stylesheet to have a blue text style:

bluetext:
  parent: bodytext
  textColor: blue

In the document you can reference this style to get a blue word.
Note this bit is generic, and should make blue text if you define a blue style in your html or whatever backend's stylesheet.

Before blue.

.. role:: bluetext

I like color :bluetext:`blue`.

After blue.

The generated pdf has the coloured words:
enter image description here

乖乖 2024-09-25 13:30:55

这可行,但会将 HTML 保留在单独的段落中。

.. raw:: html

    <font color="blue">Blue word,</font>

And a word without color

如果有人有更好的答案,我会接受。

This works, but leaves the HTML in a separate paragraph.

.. raw:: html

    <font color="blue">Blue word,</font>

And a word without color

If anyone has a better answer, I will accept it.

混浊又暗下来 2024-09-25 13:30:55

只是简单说明一下,因为我来到这里寻找类似 html 的内容。

这对我来说适用于 Sphinx v2.0.1。这使用了@adam-matan 报告的概念,但不会导致任何格式问题(即段落问题)。

参考:reStructuredText 指令

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

:raw-html:`<font color="blue">Blue word,</font>` And a word without color

Just a quick note because I landed here looking for something similar for html.

This works on Sphinx v2.0.1 for me. This uses the concept reported by @adam-matan but doesn't cause any formatting issues (i.e. the paragraph problem).

reference: reStructuredText Directives

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

:raw-html:`<font color="blue">Blue word,</font>` And a word without color
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文