37

ReStructured Textで色を使用するにはどうすればよいですか?たとえば、に**hello**変換され<strong>hello</strong>ます。ReStructure(rst2html.py)に何か を変換させるにはどうすればよい<font color="####">text</font>ですか?

..raw :: htmlについて考えましたが、空白行が導入されています。空白行なしでHTMLタグを挿入したい。

4

7 に答える 7

51

この方法が機能していることがわかりました

まず、あなたには役割があります。

.. 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;
}
于 2011-01-12T14:24:49.990 に答える
29

さて、私は今、新しいユーザーです。したがって、ここでの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>
于 2011-04-28T07:52:26.007 に答える
10

ここでの他の答えは、私がやりたかったことを示唆していますが、それはdocutilsのスタイルシートに関するいくつかの詳細な知識を前提としています。クックブックの説明は次のとおりです。

RSTファイルで、役割を1回宣言してから、それを使用します。

    .. 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ファイルでスタイルシートを指定する方法はありません。

于 2011-03-30T17:29:28.897 に答える
3

私のためにこのように動作します:

.. raw:: html

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

.. role:: red

:red:`test - this text should be red``
于 2018-12-06T19:14:20.250 に答える
2

Sphinxは、含めることを目的とした標準定義ファイルで色をすでにサポートしています(ただし、CSSファイルはありません)。s5defs.txt

  1. 次のテキストを作成して、ファイル内のrst_epilog sphinx構成の値に追加します。docs/conf.py

    rst_prolog = """
    .. include:: <s5defs.txt>
    .. default-role::
    
    """
    
  2. Sphinxの指示に従って、色付きのcssを追加します(たとえば、@Næreenの回答hack.cssからを採用します)。

    • cssファイルをegに配置します_static/css/s4defs-roles.css;
    • shtml_css_filesそのパスをsphinx構成に追加します。

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

その後、以下を使用できます。

Some :red:`colored text` at last!

ヒント:LaTeX出力にもスタイリングを表示したい場合は、このSO をお読みください。

于 2020-04-23T14:41:54.477 に答える
1

@prosseekと@RayLuoの回答をすべて1か所にまとめて、見つけやすくします

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ツールは拒否されました。

于 2018-11-08T18:28:35.807 に答える
0

RSTファイルはdocutilsまたはSphinxでレンダリングできます(実際、Sphinxはdocutilsも使用します)。

スフィンクス

完全なドキュメントが必要な場合は、Sphinxを選択してください。スタイルを設定する必要があるのは一度だけで、それをすべての場所で使用できます。それが心配しているconfig.py、、your_cssyour_role

docutils

単純なHTMLファイルを生成したいだけの場合は、以下のRSTにCSSを埋め込む方が便利だと思います。

MyRST2html.py

これは、と非常によく似ていrst2html.pyます。スクリプトを自分で作成したいだけです。ソースをハックして(そしてソースからさらに学習して)便利です。その後、クールなスタイルにカスタマイズできます。

# MyRST2html.py
import docutils.core
from pathlib import Path

source_path = Path('demo.rst')
destination_path = Path('output.html')

if not 'save all data':
    docutils.core.publish_file(source_path=source_path, destination=destination_path, writer_name='html')
elif 'save the body data only':
    with open(source_path, 'r', encoding='utf-8') as f:
        html_bytes: bytes = docutils.core.publish_string(f.read(), source_path, writer_name='html')
    html = html_bytes.decode('utf-8')
    html_data = html[html.find('<body>'):html.find('</body>')]
    with open(destination_path, 'w', encoding='utf-8') as f:
        f.write(html_data)
        f.write('</body>')

demo.rst

.. raw:: html

    <style>
        .red {color:red; font-weight:bold;}
        .b {color:#0000FF; background-color:white;}
    </style>

.. role:: red
.. role:: b

==========
Example
==========

.. contents::

Color
==========

:red:`R`\G\ :b:`B`

click me |RGB Colors|_

.. |RGB Colors| replace:: :red:`R`\G\ :b:`B`
.. _`RGB Colors`: https://www.w3schools.com/colors/colors_rgb.asp

output.html

<body>
<div class="document" id="example">
<h1 class="title">Example</h1>

<style>
    .red {color:red; font-weight:bold;}
    .b {color:#0000FF; background-color:white;}
</style><div class="contents topic" id="contents">
<p class="topic-title">Contents</p>
<ul class="simple">
<li><a class="reference internal" href="#color" id="id1">Color</a></li>
</ul>
</div>
<div class="section" id="color">
<h1><a class="toc-backref" href="#id1">Color</a></h1>
<p><span class="red">R</span>G<span class="b">B</span></p>
<p>click me <a class="reference external" href="https://www.w3schools.com/colors/colors_rgb.asp"><span class="red">R</span>G<span class="b">B</span></a></p>
</div>
</div>
</body>

ノート

IDEがPyCharmの場合、エディターとプレビューで結果を確認しても問題ありません

ここに画像の説明を入力してください

于 2020-09-24T06:36:19.273 に答える