Paragraphs around items in a list?

15 views
Skip to first unread message

Yves Chevallier

unread,
Aug 8, 2020, 8:37:20 AM8/8/20
to sphinx-users
I am wondering why Sphinx/Docutils add a paragraph around minor text: 

The list: 

```rst
- foo
- bar
- baz
```

Will be translated in HTML as:

```html
<li><p>foo</p></li>
<li><p>bar</p></li>
<li><p>baz</p></li>
```

In some case it gives bad formatting especially if `<p>` has some margins. 

By default docutils removes these `<p>`: 

```python
>>> from docutils import core
>>> print(core.publish_parts('''
- a
- b
- c
''', writer_name='html')['html_body']))
<div class="document">
<ul class="simple">
<li>a</li>
<li>b</li>
<li>c</li>
</ul>
</div>
```

But behind the scenes they are still there... 

```python
>>> from docutils import core
>>> print(core.publish_parts('''
- a
- b
- c
''')['whole'])
<document source="<string>">
    <bullet_list bullet="-">
        <list_item>
            <paragraph>
                a
        <list_item>
            <paragraph>
                b
        <list_item>
            <paragraph>
                c
```

Why is sphinx acting differently? Is there a way to tell Sphinx to not add paragraphs in such cases?

Komiya Takeshi

unread,
Aug 8, 2020, 10:36:28 AM8/8/20
to sphinx...@googlegroups.com
Hi,

This is default behavior of docutils' reST parser. It always generates
paragraph node.

>By default docutils removes these `<p>`:

Please try again with html5 writer. It seems you're using old HTML4
writer. It suppresses <p> tag on generating HTML. But HTML5 writer
does not do that.

Thanks,
Takeshi KOMIYA

2020年8月8日(土) 21:37 Yves Chevallier <cana...@gmail.com>:
> --
> You received this message because you are subscribed to the Google Groups "sphinx-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sphinx-users...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sphinx-users/f85679d1-395c-4f05-b342-d844c0225e19n%40googlegroups.com.

Yves Chevallier

unread,
Aug 8, 2020, 10:46:40 AM8/8/20
to sphinx-users
Not really sure how to use the HTML5 writer which I think I am using... 

```
$ docker-compose run latex sphinx-build --version
sphinx-build 2.2.0
```

And I do use `html5.py` because I always place my breakpoints there and it works. 

Komiya Takeshi

unread,
Aug 8, 2020, 11:12:56 AM8/8/20
to sphinx...@googlegroups.com
Please try this on your local. We can see <p> tag on "html5_polyglot" writer.
```
>>> from docutils import core
>>> print(core.publish_parts('''
... - a
... - b
... - c
... ''', writer_name='html5_polyglot')['html_body'])
<div class="document">
<ul class="simple">
<li><p>a</p></li>
<li><p>b</p></li>
<li><p>c</p></li>
</ul>
</div>

>>>
```

2020年8月8日(土) 23:46 Yves Chevallier <cana...@gmail.com>:
> To view this discussion on the web visit https://groups.google.com/d/msgid/sphinx-users/029a0cf7-8ce9-4e1f-9af9-af5fd2c90d4dn%40googlegroups.com.

Yves Chevallier

unread,
Aug 8, 2020, 11:32:17 AM8/8/20
to sphinx-users
Yes I am getting the same result as you. But this is an incorrect html output I think and I would like to get rid of that behavior. The only way I found is to visit all the `<li>` nodes afterwards and do replace node with the text content. But I think it is ugly.

Komiya Takeshi

unread,
Aug 8, 2020, 12:00:47 PM8/8/20
to sphinx...@googlegroups.com
I don't know why docutils determined this approach. But they surely
chose this way on moving to HTML5.
Please post a comment to the docutils project if you really want to
change this. At present, sphinx simply follows the docutils' behavior.

2020年8月9日(日) 0:32 Yves Chevallier <cana...@gmail.com>:
> To view this discussion on the web visit https://groups.google.com/d/msgid/sphinx-users/4c755b51-4e6a-4a3c-a219-c68bdf792880n%40googlegroups.com.

Matthias Geier

unread,
Aug 9, 2020, 4:33:16 AM8/9/20
to sphinx...@googlegroups.com
For reference, this has recently been discussed on the issue tracker:
https://github.com/sphinx-doc/sphinx/issues/7838.

It looks like docutils made the decision for the HTML5 writer to
always keep <p> elements and handle "compact" lists with CSS (instead
of removing <p> elements).

At the time, this wasn't handled quite correctly in Sphinx, so I made
some changes to the "basic" theme:
https://github.com/sphinx-doc/sphinx/pull/7852.

This should handle "compact" lists correctly (starting with Sphinx
version 3.1.2), as long as the theme you are using doesn't break it.

cheers,
Matthias
> To view this discussion on the web visit https://groups.google.com/d/msgid/sphinx-users/CAFmkQAPchm5uWQ%2Bta%2BM6w7n%2BodP%2BTbADi9_nRHa1kApZweaP6Q%40mail.gmail.com.

Yves Chevallier

unread,
Aug 9, 2020, 3:30:09 PM8/9/20
to sphinx-users
Thanks for this! I have a better understanding now.

Daniel Scott

unread,
Nov 25, 2020, 10:35:40 PM11/25/20
to sphinx...@googlegroups.com
--
Reply all
Reply to author
Forward
0 new messages