为具有特定标题的通用狮身人面像警告定义标记

发布于 2025-01-03 02:58:27 字数 421 浏览 1 评论 0原文

我正在使用 Sphinx 为 Python 程序生成 HTML 文档。

我想使用通用警告 指令具有特定的标题,并以我可以定义的方式对其进行标记,例如使用 note 指令生成的内容,即盒装的,但具有不同的颜色(大多数<一href="https://www.sphinx-doc.org/en/master/usage/restructedtext/basics.html#directives" rel="nofollow noreferrer">警告没有特殊样式)。

我该如何最好地解决这个问题?

I am using Sphinx to generate HTML documentation for a Python program.

I would like to use the generic admonition directive with a specific title and have it marked up in a way I can define, for example like content generated with the note directive, i.e., boxed, but with a different color (most Admonitions are not specially styled).

How do I best go about this?

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

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

发布评论

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

评论(4

我偏爱纯白色 2025-01-10 02:58:27

如果我正确理解你的问题,你想对警告应用自定义 CSS 样式。您可以使用 :class: 属性来做到这一点。

例如,以下

.. admonition:: my title goes here
   :class: myOwnStyle

   this is the admonition text

呈现为

<div class="myownstyle admonition">
  <p class="first admonition-title">my title goes here</p>
  <p class="last">this is the admonition text</p>
</div>

您然后添加您自己的样式表。例如,通过 source 目录中的 _templates 目录中的自定义 layout.html

{% extends "!layout.html" %}
{% set css_files = css_files + ["_static/customstyle.css"] %}

然后您可以在您的使用 myownstyle 类的选择器的样式表

If I understood your question correctly, you'd like to apply a custon CSS style to the admonition. You can do this with a :class: attibute.

For example, the following

.. admonition:: my title goes here
   :class: myOwnStyle

   this is the admonition text

renders as

<div class="myownstyle admonition">
  <p class="first admonition-title">my title goes here</p>
  <p class="last">this is the admonition text</p>
</div>

You then add your own style sheet. For example, by a custom layout.html in the _templates directory in your source directory:

{% extends "!layout.html" %}
{% set css_files = css_files + ["_static/customstyle.css"] %}

Then you can play around with CSS styles in your style sheet using a selector for the myownstyle class

故人如初 2025-01-10 02:58:27

要更轻松地添加 css 文件,请将它们放在 _static 文件夹中,然后将其添加到 conf.py 中:

def setup(app):
    app.add_stylesheet('custom.css')

To add css files more easily, put them in the _static folder and then add this to conf.py:

def setup(app):
    app.add_stylesheet('custom.css')
青巷忧颜 2025-01-10 02:58:27

下面是一个示例 custom.css,用于覆盖 myownstyle 警告的标题颜色和背景颜色。我在 conf.py 中使用了 app.add_stylesheet() 调用。

.rst-content .myownstyle .admonition-title {
   background: #b99976
}

.rst-content .myownstyle {
   background: #e5d3b3
}

Here is an example custom.css for overwriting the color of the title and background color of the myownstyle admonition. I used the app.add_stylesheet() call in conf.py.

.rst-content .myownstyle .admonition-title {
   background: #b99976
}

.rst-content .myownstyle {
   background: #e5d3b3
}
魂归处 2025-01-10 02:58:27

以下是我对此的看法:

  1. 添加静态文件需要满足的要求:

    • conf.py中:

      添加以下内容:

      • html_static_path = ['_static']

      • 添加 css 文件的路径:

        • 要添加到列表 html_css_files 的 CSS 文件的名称:

          <前><代码> html_css_files = [
          'css/some_file.css',
          'css/custom.css', # 创建自定义 CSS 文件并添加
          ]

      • 注意:作为推论,CSS 文件的位置将为(路径):_static/css/custom.css(或任何其他选择的文件夹名称来代替 css






  2. 将类添加到admonition(或指令) - 详细信息此处

    如@Bud 此处所述(为简洁起见而添加):

     .. 警告:: 标题
          :class: clsMyTitle
    
  3. 创建文件custom.css(如果尚未完成)并添加以下内容:

     .admonition.clsmytitle { /*小写类名*/
         背景颜色:#e8cfe7;
         /* 根据需要选择 */
      }
    
  4. 告别说明:截至今天 (2024) ,我们可以完成上述所有操作,而无需创建我们自己的(自定义layout.html

  5. Sphinx v 7.2.6Python 3.9

    中测试

  6. 对于 @Mad Physicist,人们可以随时尝试:如何将类名称添加到告诫...。 (实际上,我正在寻找在我的页面中创建浮动框的方向,并且意外(或者由于Google著名算法的仁慈)碰巧来到了这里.

Here are my take on this:

  1. Requirements to be fulfilled for adding static files:

    • In conf.py:

      Add the following:

      • html_static_path = ['_static']

      • Adding path to css files:

        • Name of the css file to be be added to list html_css_files:

           html_css_files = [
              'css/some_file.css',
              'css/custom.css',  # Create a Custom CSS file and add
           ]
          
      • Note: As a corollary, the location of the CSS file would be (path): _static/css/custom.css (or any other folder name of choice in place of css)

  2. Add a class to admonition (or a directive) - details here

    As detailed above by @Bud here (being added for brevity):

       .. admonition:: A Title
          :class: clsMyTitle
    
  3. Create file custom.css (if not done already) and add the following:

     .admonition.clsmytitle {    /*Lowercase class name*/
         background-color: #e8cfe7;
         /* options as needed */
      }
    
  4. A parting note: As of today (2024), we may do all the above without having to take recourse to creating our own (custom) layout.html

  5. Tested in Sphinx v 7.2.6, Python 3.9

  6. To @Mad Physicist, one can always take a shot at: how to add class name to an admonition to .... (Actually I was looking for some direction at creating floating box in my page/s, and by accident (or by mercy of Google's famous algo) happened to land here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文