Skip to content

Possible data loss with sphinx.util.parsing.nested_parse_to_nodes() #13845

@gmilde

Description

@gmilde

Describe the bug

The utility function nested_parse_to_nodes() provides the boolean parameter keep_title_context".
If this parameter is True, parsing is done by docutils.parsers.rst.state.RSTState.nested_parse() with the document-wide "title style hierarchy".

Recent testing revealed that this method can loose complete sections without warning when the rST input contains sections with a title style that matches a lower section level than the current section level.

The new section algorithm introduced in 0.22 does no longer drop "lower" sections. However, after the nested parsing is complete, the calling parser continues where it left of, messing the order of elements in the doctree (cf. https://sourceforge.net/p/docutils/code/10205/).

The most sensible simple solution for Docutils is to always use a new "title style context" with nested parsing, which overrides the "keep_title_context" argument.
It seems Sphinx itself never uses nested_parse_to_nodes() with keep_title_context=True.

If there is a convincing use case for keep_title_context=True, we might try to find a save implementation. In this case, Sphinx should define and test the desired behaviour when a nested section title uses the style of a lower section level than the section containing the directive using nested_parse_to_nodes().
Otherwise I suggest to emit a warning like "nested parsing always uses a separate title style context in Docutils >= 0.23."

How to Reproduce

Try old_nested_parsing.py with:

Docutils 0.21 for the legacy section parsing algorithm,

Docutils commit r10204 for the messed order with the new section parsing algorithm (a unit test demonstrating the issue was added in commit r10205),

Docutils commit r10206 for the fix using a new title style context with every nested parsing.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions