5

簡単なreSTスニペットは次のとおりです。

deleting this line causes all subheadings to be rendered as h1 tags

I should be an h1
=================

I should be an h2
-----------------
foo            

I should also be an h2
----------------------
foo

レンダリングされたデモを次に示します。

最初の行あり:http
://rst.ninjs.org/?n = ff67380d732a33c7844f350c240804d0 最初の行なし:http: //rst.ninjs.org/?n = 550ea2c1b4233affdce1d158c5dc4d99

次のPythonを使用してreSTをレンダリングしています。

from docutils.core import publish_parts
parts = publish_parts(rest_content, writer_name="html")
html_snippet = parts['html_body']

<h2>最初の行なしで小見出し(具体的にはタグ)を取得するにはどうすればよいですか?小見出しの上に2つのレベルの階層を提供していますか?ページヘッダーを単純に提供しても役に立ちません:http://rst.ninjs.org/?n = e874f6eaad17c8ae7fd565f9ecb2212b

4

3 に答える 3

8

最初のタイトルをドキュメントタイトルに昇格させないでください。

以下の例では、publish_parts()に渡されるsettings_overridesパラメーターに注意してください。

rest_content = """
I should be an h1
=================

I should be an h2
-----------------
foo


I should also be an h2
----------------------
foo
"""

from docutils.core import publish_parts
parts = publish_parts(rest_content, writer_name="html",
        settings_overrides={'doctitle_xform':False})
html_snippet = parts['html_body']

print(html_snippet)

そして出力:

<div class="document">
<div class="section" id="i-should-be-an-h1">
<h1>I should be an h1</h1>
<div class="section" id="i-should-be-an-h2">
<h2>I should be an h2</h2>
<p>foo</p>
</div>
<div class="section" id="i-should-also-be-an-h2">
<h2>I should also be an h2</h2>
<p>foo</p>
</div>
</div>
</div>
于 2012-03-08T15:23:31.600 に答える
1

同じ問題がありました。受け入れられた解決策は私にはうまくいきませんでした。ただし、次のコードは実行しました。

content = publish_parts(
    rest_content,
    writer_name='html',
    settings_overrides={'initial_header_level': 2})
html = content['html_body']
于 2015-10-29T19:24:46.290 に答える
0

ReSTは、各レベルでどの記号を使用するかを気にしません。「=」は単なる規則です。したがって、最初のものを削除すると、「-」はh1を示していると見なされます。残念ながら、これを回避する方法はないと思います。

于 2012-03-08T14:21:35.977 に答える