ReStructuredText does not match subheadings

Here is a simple REST snippet:

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

and here is a demonstration of it:

with start line: http://rst.ninjs.org/?n=ff67380d732a33c7844f350c240804d0
without start line: http://rst.ninjs.org/?n=550ea2c1b4233affdce1d158c5dc4d99

I am processing reST using the following Python:

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

How to get subtitles (in particular, tags <h2>) without a start line? Do you have two levels of hierarchy over subheadings? Naively providing the page title does not help: http://rst.ninjs.org/?n=e874f6eaad17c8ae7fd565f9ecb2212b

+5
source share
3 answers

Do not advertise the first title of the document.

settings_overrides, publish_parts() :

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>
+8

. . :

content = publish_parts(
    rest_content,
    writer_name='html',
    settings_overrides={'initial_header_level': 2})
html = content['html_body']
+1

ReST doesn't care which character you use for each level, "=" is just a convention. Therefore, if you delete the first one, it sees "-" as the designation h1. I do not think that, unfortunately, there is a way.

0
source

All Articles