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>没有初始行的情况下获得副标题(特别是标签)?是否在子标题之上提供了两个层次结构?天真地提供页眉并没有帮助: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 回答